Arrays of structures are very easy to implement and use once you know how to imp
ID: 3597158 • Letter: A
Question
Arrays of structures are very easy to implement and use once you know how to implement as simple static array of integers. The operators all work the same. However, you must pay close attention to the subtle differences in the syntax needed.
Definitions
We will define several terms that you need to know to understand arrays. They are as follows:
A structure is a heterogeneous data type.
The dot “.” operator is used to access the fields/members of a structure.
The name of a static array is a constant pointer to the first element in the array.
The size/capacity is the number of memory cells allocated to an array.
An index /subscript is used to access the memory cells in an array.
[ ] is called the subscript operator.
The index is a non-negative integer.
The range of an index is between 0 and the size-1.
Declaration Syntax
a. To declare a structure:
struct structure_name
{
field_type_1 field_name_1;
. . . .
field_type_n field_name_n;
};
Examples allocating memory for two Dynamic Arrays:
struct student_record
{
string firstname, lastname;
double age, income;
int number_of_children;
char sex;
};
b. To declare an array of structures:
structure_name array_of_structures[size];
More information on dynamic arrays can be found in your course textbook and on the web.
Experiments
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?
C++. PLEASE only answer Q1 to Q7 thanks, spam answers will be reported otherwise 5 stars
Explanation / Answer
Question 1:
student_record Student_DB[3] = {"Lofton",53,'M',"Thomas",55,'M',
"Tami",25,'F'};
Student_DB[3] is an array of 3 structures and it is initialized statically with the above statement.
Question 2:
for (int i=0; i<3; i++)
The for loops are used to display the 3 structure variables in the array of Structure Student_DB[3].
Question 3:
The difference between the two programs is the static and dynamic memory allocation for array of structures.
Question 4:
Difference between the static and dynamic memory allocation :
1. In static allocation the size of array of structures is fixed.
2. There is no need to allocate memory with new operator in static allocation but it is required in dynamic memory allocation which uses heap storage.
3. There is no need to deallocate memory with delete in static allocation but it is required in dynamic allocation to free the heap memory. It is automatically managed by stack in case of static allocation.
4. There is no need to use pointers in static memory allocation but are required for dynamic allocation of memory.
Question 5:
In the given program, the static_Array and dynamic_Array arrays are initialized for 5 elements with different values in both arrays. Then by only assigning the name of static_Array(name denotes base address) to dynamic_Array , all the elements of static_Array are copied in dynamic_Array.
Question 6:
1. Static Arrays are fixed and are allocated memory at compile time whereas dynamic Arrays are allocated memory at run time.
2. Static Arrays need not managed by programmer, but dynamic arrays need to be managed for allocation using new and deallocation using delete.
Question 7
static_Array = dynamic_Array;
In this statement ,code is trying to assign the pointer to int to array of int which generates compilation error. Both are incompatible types.
dynamic_Array = static_Array
is correct statement as the base address of static_Array is assigned to dynamic_array which is address to int. Both are compatible types.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.