Arrays and Structures in CPP
Arrays and Structures in CPP
int n = 2;
score[n + 1] = 99;
In this example, variable score[3] is assigned 99
No warning is given!
Multi-Dimensional Arrays
int grade[4][3];
The first array index refers to the number of a student
The second array index refers to a quiz number
The solution in the next slides is done using functions to compute averages of students and quizzes. Since
we have not covered functions yet , just take the logic in the program to solve the problem stated above.
The grading program uses one-dimensional
arrays to store…
Each student's average score
Each quiz's average score
The functions that calculate these averages
use global constants for the size of the arrays
This was done because the
functions seem to be
particular to this program
struct StudentStruct
structure name
{
int studentID;
string name; structure members
short year;
double gpa;
}; Note the
required
Chapter 1- Array and Structures in C++
;
struct Declaration Notes
gpa
getline(cin, s1.name);
cin >> s1.studentID;
s1.gpa = 3.75;
Recall that arrays had no whole array operations (except passing reference to parameter)
Structures DO have aggregate operators
assignment statement =
parameter (value or reference)
return a structure as a function type
no arithmetic operations
struct variables must be initialized, compared, written one member at a time.
Correct:
cout << s1.studentID << endl;
cout << s1.name << endl;
cout << s1.year << endl;
cout << s1.gpa;
Chapter 1- Array and Structures in C++
Comparing struct Members
Example
struct LISTTYPE
{
int list [ARRAYSIZE]; //array containing the list
int listLength; //length of the list
}
struct InventoryStruct {
Defn => structs where at least one of the PartStruct
components is, itself, apart;
struct
Example: int qty_sold,
re_order_qty;
VendorStruct
vendor; };