Arrays & Structures II Chapter 9
2 Organizing Heterogeneous Data Arrays allow a programmer to organize values of the same type Homogeneous data What about logically related data values of differing types? Heterogeneous data Parallel arrays Array of structs Origin DestinationMilesTime Blacksburg, VA Knoxville, TN2443:25 Knoxville, TN Nashville, TN1782:35 Nashville, TN Memphis, TN2103:17 Memphis, TN Little Rock, AR1372:05 Little Rock, AR Texarkana, TX1412:10
3 Organizing Heterogeneous Data A better design would be to have a new data type, Trip, such that a variable of type Trip would contain all the related values for a single trip. A struct allows C++ programmers to do just that… Like enum, struct is used to define a new data type The statement below does not declare a variable, it defines a type struct Trip { string Origin; // starting point of trip string Destination; // ending point of trip int Miles; // distance traveled int Minutes; // time required, in min double MPH; // average speed };
4 Structures Structure: a heterogeneous collection of data values called members or fields each member has a type and a unique name individual members are accessed by name Definitions of data types are usually global, since they are required throughout a program. As long as a type definition is in scope we may declare variables of that type in the usual way: Trip firstLeg; const int MAXLEGS = 100; Trip Itinerary[MAXLEGS]; // array of Trips
5 struct Variables A variable of a struct type will contain the values for all of the specified members for that type Those members must be individually initialized To reference a particular member of a struct variable, state the variable name followed by a period followed by the member name The period is the member selector operator Trip firstLeg; firstLeg.Origin = "Blacksburg, VA"; firstLeg.Destination = "Knoxville, TN"; firstLeg.Miles = 244; firstLeg.Minutes = 205; firstLeg.MPH = 71.2;
6 struct Variables Accessing struct array elements requires use of both [] and. Operators First specify the array index Then the struct member const int MAXLEGS = 100; Trip Itinerary[MAXLEGS]; // array of Trips Itinerary[0].Origin = "Milwaukee, WI"; Itinerary[0].Destination = "Chicago, IL"; Itinerary[0].Miles = 88; Itinerary[0].Minutes = 88; Itinerary[0].MPH = 60.0;
7 Member Access Example struct Rectangle { int color; int xNW, yNW; int Side[2]; };... Rectangle R; cin >> R.xNW >> R.yNW; cin >> R.Side[0] >> R.Side[1]; R.color = 0; int Area = R.Side[0] * R.Side[1]; The members of a struct variable may be used just as if they were simple variables of the specified type
8 Aggregate Operations An aggregate operation is an operation that is performed on a data structure, such as an structured variable, as a whole rather than performed on an individual member Which of the following are supported for structs? Trip X, Y; X = Y; // _____ assigning one struct to another X == Y; // _____ comparing two structs cout << X; // _____ inserting an struct to a stream X = X + Y; // _____ performing arithmetic with structs return X; // _____ using a struct as the return value Foo(X); // _____ passing an entire struct
9 struct Variables as Parameters Entire struct variables can be passed to functions as parameters. By default is struct is passed by value Since struct variables tend to be large, it is generally better to pass them by constant reference void printTrip(Trip& aTrip) { cout << aTrip.Origin << endl << aTrip.Destination << endl << aTrip.Miles << endl << (aTrip.Minutes / MINSPERHOUR) << ':' << (aTrip.Minutes % MINSPERHOUR) << endl << aTrip.MPH << endl; }
10 Problem In a certain class (<=50 students) 3 programming assignments (worth 40%) and 3 tests (worth 60%) are given All the programming and test scores must be in the range and should be checked for errors An erroneous score means that student's data is invalid Compute the average test and programming scores, and overall grades (A through F, based on 90, 80, 70, 60 scale) for each student, as well as class averages Assume that the data for each student consists of a SSN followed by 6 integers representing the programming and test scores.
11 Processing Student Grades const int MaxStudents=50; struct StudentType { string ssn; int P1, P2, P3, T1, T2, T3; float Pavg, Tavg, Tot; char grade; bool valid; }; StudentType students[MaxStudents]; SSNP1P2P3PavgT1T2T3TavgTotGradeValid
Multidimensional Arrays
13 Multidimensional Arrays Multidimensional arrays Have more than one index Rows & Columns for 2-D arrays Syntax for array declaration: [ ][ ] …[ ]; int twoDimArray[4][2]; ? ? ? ? ? ? ? ?
14 Initialization Multidimensional arrays can be initialized like 1-D arrays List elements in row major order int twoDimArray[4][2] = {3, 25, 9, 16, -4, 4, 44, 2};
15 for Loops When accessing or performing operations on multidimensional arrays, it is often necessary to use nested for loops. Consider the problem of adding arrays x and y, and placing the result in z. const int NRow = 4; const int NColumn = 2; int x[NRow][NColumn] ={ 3, 25, 9, 16, -4, 4, 44, 2}; int y[NRow][NColumn] ={ 8, 5, 12, -3, 43, 8, -7, 4}; int z[NRow][NColumn];
16 for Loops int iRow; int iColumn; for (iRow = 0; iRow < NRow; iRow++) { for (iColumn = 0; iColumn < NColumn; iColumn++) { z[iRow][iColumn] = x[iRow][iColumn] + y[iRow][iColumn]; } ? ? ? ? ? ? ? ? ? iRow iColumn xyz
17 for Loops int iRow; int iColumn; for (iRow = 0; iRow < NRow; iRow++) { for (iColumn = 0; iColumn < NColumn; iColumn++) { z[iRow][iColumn] = x[iRow][iColumn] + y[iRow][iColumn]; } ? ? ? ? ? ? ? ? iRow iColumn xyz
18 for Loops int iRow; int iColumn; for (iRow = 0; iRow < NRow; iRow++) { for (iColumn = 0; iColumn < NColumn; iColumn++) { z[iRow][iColumn] = x[iRow][iColumn] + y[iRow][iColumn]; } ? ? ? ? ? ? ? iRow iColumn xyz
19 for Loops int iRow; int iColumn; for (iRow = 0; iRow < NRow; iRow++) { for (iColumn = 0; iColumn < NColumn; iColumn++) { z[iRow][iColumn] = x[iRow][iColumn] + y[iRow][iColumn]; } ? ? ? ? ? ? ? iRow iColumn xyz
20 for Loops int iRow; int iColumn; for (iRow = 0; iRow < NRow; iRow++) { for (iColumn = 0; iColumn < NColumn; iColumn++) { z[iRow][iColumn] = x[iRow][iColumn] + y[iRow][iColumn]; } ? ? ? 30 ? ? ? iRow iColumn xyz
21 for Loops int iRow; int iColumn; for (iRow = 0; iRow < NRow; iRow++) { for (iColumn = 0; iColumn < NColumn; iColumn++) { z[iRow][iColumn] = x[iRow][iColumn] + y[iRow][iColumn]; } ? ? ? 30 ? ? ? iRow iColumn xyz
22 for Loops int iRow; int iColumn; for (iRow = 0; iRow < NRow; iRow++) { for (iColumn = 0; iColumn < NColumn; iColumn++) { z[iRow][iColumn] = x[iRow][iColumn] + y[iRow][iColumn]; } ? ? ? 30 ? ? ? iRow iColumn xyz
23 for Loops int iRow; int iColumn; for (iRow = 0; iRow < NRow; iRow++) { for (iColumn = 0; iColumn < NColumn; iColumn++) { z[iRow][iColumn] = x[iRow][iColumn] + y[iRow][iColumn]; } ? ? 30 ? ? ? iRow iColumn xyz
24 Multidimensional Arrays & Functions Recall, a function using a 1-D array uses pass-by reference as follows: a_function(int x[]) With multidimensional arrays, the array is still passed using pass-by reference, but there is less flexibility. With the 1-D array we do not have to explicitly state the size of the array With multidimensional arrays, we do not have to explicitly state the size of the first dimensional, but must do so for remaining dimensions a_function(int x[][N])