Step 1: #include <iostream> #include <string> using namespace std; struct studen
ID: 3558931 • Letter: S
Question
Step 1:
#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
Explanation / Answer
Step 1 :
Answer 1 : The Array has been initialized as a static array of structures. It assings the three input of structures into indices 0, 1 and 2 as complete records.
Answer 2 : The first For loop Prints the record of all the three students one by one . as :
"Lofton",53,'M',
"Thomas",55,'M',
"Tami",25,'F'
the second for loop only prints one record as the array definedd for it does not hold different records but only different elements of the single record..
Student_DB[0].name = "William";
Student_DB[2].age = 100;
Student_DB[1].sex = 'F';
the output wil be "William","100","F".
Step 2:
Answer 3 : In Step 2 the array initialization is dynamic. whereas the array in step 1 it was static. In step 2 the records are entered one by one individually in indice 0 1 and 2.
Answer 4:
tatic arrays are created on the stack, and necessarily have a fixed size (the size of the stack needs to be known going into a function):
student_record Student_DB[3] = {"Lofton",53,'M',"Thomas",55,'M',"Tami",25,'F'};
Dynamic arrays are created on the heap. They can have any size, but you need to allocate and free them yourself since they're not part of the stack frame:
You don't need to deal with the memory management of a static array, but they get destroyed when the function they're in ends
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.