Step 1: In this experiment you will learn how to declare and use an array of str
ID: 3679279 • Letter: S
Question
Step 1: In this experiment you will learn how to declare and use an array of structures.
Enter, save, compile and execute the following program in MSVS. Call the new project “StaticArraysExp1” and the program “StaticArrays1.cpp”. Answer the questions that follow.
#include <iostream>
#include <string>
using namespace std;
struct student_record
{
string name;
double age;
char sex;
};
int main()
{
student_record Student_DB[3] = {"Lofton",53,'M',"Thomas",55,'M',
"Tami",25,'F'};
cout<<endl;
for (int i=0; i<3; i++)
{
cout<<Student_DB[i].name<<" "
<<Student_DB[i].age<<" "
<<Student_DB[i].sex<<endl<<endl;
}
Student_DB[0].name = "William";
Student_DB[2].age = 100;
Student_DB[1].sex = 'F';
cout<<"+++++++++++++++++++++++++++++++ ";
cout<<endl;
for (int i=0; i<3; i++)
{
cout<<Student_DB[i].name<<" "
<<Student_DB[i].age<<" "
<<Student_DB[i].sex<<endl<<endl;
}
return 0;
}
Question 1:Please explain how the array “Student_DB” was initialized?
Question 2:What is the purpose of the two “for” loops in the program (StaticArray1s.cpp) in Step 1? Explain your answer.
Step 2: In this experiment you will explain the output of a program that uses a dynamic array of structures. Enter, save, compile and execute the following program in MSVS. Call the new project “StaticArraysExp2” and the program “StaticArrays2.cpp”. Answer the questions that follow.
#include <iostream>
#include <string>
using namespace std;
struct student_record
{
string name;
double age;
char sex;
};
int main()
{
student_record *Student_DB = new student_record[3];
Student_DB[0].name = "Lofton";
Student_DB[0].age = 53;
Student_DB[0].sex = 'M';
Student_DB[1].name = "Thomas";
Student_DB[1].age = 55;
Student_DB[1].sex = 'M';
Student_DB[2].name = "Tami";
Student_DB[2].age = 25;
Student_DB[2].sex = 'F';
cout<<endl;
for (int i=0; i<3; i++)
{
cout<<Student_DB[i].name<<" "
<<Student_DB[i].age<<" "
<<Student_DB[i].sex<<endl<<endl;
}
Student_DB[0].name = "William";
Student_DB[2].age = 100;
Student_DB[1].sex = 'F';
cout<<"+++++++++++++++++++++++++++++++ ";
cout<<endl;
for (int i=0; i<3; i++)
{
cout<<Student_DB[i].name<<" "
<<Student_DB[i].age<<" "
<<Student_DB[i].sex<<endl<<endl;
}
delete [ ] Student_DB;
return 0;
}
Question 3:Please state and explain differences in the source code of the program(StaticArrays1.cpp) in Step 1 and the program (StaticArrays2.cpp) in Step 2?
Question 4:What differences between static and dynamic arrays can you conclude from your observations?
Step 3: In this experiment you will investigate the output of a program that uses static and dynamic arrays. Enter, save, compile and execute the following program in MSVS. Call the new project “StaticArraysExp3” and the program “StaticArrays3.cpp”. Answer the question that follows.
#include <iostream>
using namespace std;
int main()
{
int static_Array[5];
int *dynamic_Array;
dynamic_Array = new int[5];
int i;
for(i=0; i<5; i++)
{
static_Array[i]=i;
dynamic_Array[i]=5;
}
for (i=0; i<5; i++)
{
cout<<"static_Array["<<i<<"] = "<<static_Array[i]<<endl;
cout<<"dynamic_Array["<<i<<"] = "<<dynamic_Array[i]<<endl;
}
cout<<endl<<endl<<endl;
dynamic_Array = static_Array;
for (i=0; i<5; i++)
{
cout<<"static_Array["<<i<<"] = "<<static_Array[i]<<endl;
cout<<"dynamic_Array["<<i<<"] = "<<dynamic_Array[i]<<endl;
}
return 0;
}
Question 5:What action(s) does the program (StaticArrays3.cpp) in Step 3 perform?
Step 4: In this experiment you will explain the output of a program that uses static and dynamic arrays. Enter, save, compile and execute the following program in MSVS. Call the new project “StaticArraysExp4” and the program “StaticArrays4.cpp”. Answer the question that follows.
#include <iostream>
using namespace std;
int main()
{
int *static_Array = new int[5];
int *dynamic_Array;
dynamic_Array = new int[5];
int i;
for(i=0; i<5; i++)
{
static_Array[i]=i;
dynamic_Array[i]=5;
}
for (i=0; i<5; i++)
{
cout<<"static_Array["<<i<<"] = "<<static_Array[i]<<endl;
cout<<"dynamic_Array["<<i<<"] = "<<dynamic_Array[i]<<endl;
}
cout<<endl<<endl<<endl;
static_Array = dynamic_Array;
for (i=0; i<5; i++)
{
cout<<"static_Array["<<i<<"] = "<<static_Array[i]<<endl;
cout<<"dynamic_Array["<<i<<"] = "<<dynamic_Array[i]<<endl;
}
return 0;
}
Question 6:Compare the programs that were presented in Steps 1, 2, 3 and 4. What are your observations?
Step 5: In this experiment you will explain the output of a program that uses static and dynamic arrays. Enter, save, compile and execute the following program in MSVS. Call the new project “StaticArraysExp5” and the program “StaticArrays5.cpp”. Answer the question that follows.
#include <iostream>
using namespace std;
int main()
{
int static_Array[5];
int *dynamic_Array;
dynamic_Array = new int[5];
int i;
for(i=0; i<5; i++)
{
static_Array[i]=i;
dynamic_Array[i]=5;
}
for (i=0; i<5; i++)
{
cout<<"static_Array["<<i<<"] = "<<static_Array[i]<<endl;
cout<<"dynamic_Array["<<i<<"] = "<<dynamic_Array[i]<<endl;
}
cout<<endl<<endl<<endl;
static_Array = dynamic_Array;
for (i=0; i<5; i++)
{
cout<<"static_Array["<<i<<"] = "<<static_Array[i]<<endl;
cout<<"dynamic_Array["<<i<<"] = "<<dynamic_Array[i]<<endl;
}
return 0;
}
Question 7:Why does the program in Step 5 produce compilation errors?
Explanation / Answer
Multiple Questions: Answering 1st
The Student_DB is initialized explicitly during the declaration of structure variable itself.
student_record Student_DB[3] = {"Lofton",53,'M',"Thomas",55,'M',
"Tami",25,'F'};
The Student_DB is array of structures and is initialized with 3 set of values. one for each element of array of structures . 1st element is initialized with {"Lofton",53,'M'} and so on.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.