240 likes | 382 Views
Arrays & Structures II. Chapter 9. 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 Destination Miles Time
E N D
Arrays & Structures II Chapter 9
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 Destination Miles Time ---------------------------------------------------------- Blacksburg, VA Knoxville, TN 244 3:25 Knoxville, TN Nashville, TN 178 2:35 Nashville, TN Memphis, TN 210 3:17 Memphis, TN Little Rock, AR 137 2:05 Little Rock, AR Texarkana, TX 141 2:10
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 };
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
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;
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;
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
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
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; }
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 0-100 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.
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];
Multidimensional Arrays • Multidimensional arrays • Have more than one index • Rows & Columns for 2-D arrays • Syntax for array declaration: <data type> <var name>[<# elements dim 1>][<# elements dim 2>] …[<# elements dim N>]; int twoDimArray[4][2]; 0 1 0 ? ? ? ? 1 ? ? 2 ? ? 3
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}; 0 1 0 3 25 9 16 1 -4 4 2 44 2 3
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];
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]; } } x 0 1 y 0 1 z 0 1 0 3 25 0 8 5 0 ? ? 9 16 12 -3 ? ? 1 1 1 0 -4 4 43 8 ? ? iRow 2 2 2 ? 44 2 -7 4 ? ? iColumn 3 3 3
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]; } } x 0 1 y 0 1 z 0 1 0 3 25 0 8 5 0 ? ? 9 16 12 -3 ? ? 1 1 1 0 -4 4 43 8 ? ? iRow 2 2 2 0 44 2 -7 4 ? ? iColumn 3 3 3
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]; } } x 0 1 y 0 1 z 0 1 0 3 25 0 8 5 0 11 ? 9 16 12 -3 ? ? 1 1 1 0 -4 4 43 8 ? ? iRow 2 2 2 0 44 2 -7 4 ? ? iColumn 3 3 3
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]; } } x 0 1 y 0 1 z 0 1 0 3 25 0 8 5 0 11 ? 9 16 12 -3 ? ? 1 1 1 0 -4 4 43 8 ? ? iRow 2 2 2 1 44 2 -7 4 ? ? iColumn 3 3 3
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]; } } x 0 1 y 0 1 z 0 1 0 3 25 0 8 5 0 11 30 9 16 12 -3 ? ? 1 1 1 0 -4 4 43 8 ? ? iRow 2 2 2 2 44 2 -7 4 ? ? iColumn 3 3 3
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]; } } x 0 1 y 0 1 z 0 1 0 3 25 0 8 5 0 11 30 9 16 12 -3 ? ? 1 1 1 1 -4 4 43 8 ? ? iRow 2 2 2 2 44 2 -7 4 ? ? iColumn 3 3 3
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]; } } x 0 1 y 0 1 z 0 1 0 3 25 0 8 5 0 11 30 9 16 12 -3 ? ? 1 1 1 1 -4 4 43 8 ? ? iRow 2 2 2 0 44 2 -7 4 ? ? iColumn 3 3 3
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]; } } x 0 1 y 0 1 z 0 1 0 3 25 0 8 5 0 11 30 9 16 12 -3 21 ? 1 1 1 1 -4 4 43 8 ? ? iRow 2 2 2 0 44 2 -7 4 ? ? iColumn 3 3 3
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])