Arrays & Structures II

24
Arrays & Structures II Chapter 9

description

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 DestinationMilesTime - PowerPoint PPT Presentation

Transcript of Arrays & Structures II

Page 1: Arrays & Structures II

Arrays & Structures II

Chapter 9

Page 2: Arrays & Structures II

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 Destination Miles Time----------------------------------------------------------Blacksburg, VA Knoxville, TN 244 3:25Knoxville, TN Nashville, TN 178 2:35Nashville, TN Memphis, TN 210 3:17Memphis, TN Little Rock, AR 137 2:05Little Rock, AR Texarkana, TX 141 2:10

Page 3: Arrays & Structures II

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};

Page 4: Arrays & Structures II

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

Page 5: Arrays & Structures II

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;

Page 6: Arrays & Structures II

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;

Page 7: Arrays & Structures II

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

Page 8: Arrays & Structures II

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 anotherX == Y; // _____ comparing two structscout << X; // _____ inserting an struct to a streamX = X + Y; // _____ performing arithmetic with structsreturn X; // _____ using a struct as the return valueFoo(X); // _____ passing an entire struct

Page 9: Arrays & Structures II

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;}

Page 10: Arrays & Structures II

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 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.

Page 11: Arrays & Structures II

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];

SSN P1 P2 P3 Pavg T1 T2 T3 Tavg Tot Grade Valid

Page 12: Arrays & Structures II

Multidimensional Arrays

Page 13: Arrays & Structures II

13

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

2

3

0 1

Page 14: Arrays & Structures II

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};

3

9

-4

44

25

16

4

2

0

1

2

3

0 1

Page 15: Arrays & Structures II

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];

Page 16: Arrays & Structures II

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];

}

}

3

9

-4

44

25

16

4

2

0

1

2

3

0 1

8

12

43

-7

5

-3

8

4

0

1

2

3

0 1

?

?

?

?

?

?

?

?

0

1

2

3

0 1

0

?

iRow

iColumn

x y z

Page 17: Arrays & Structures II

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];

}

}

3

9

-4

44

25

16

4

2

0

1

2

3

0 1

8

12

43

-7

5

-3

8

4

0

1

2

3

0 1

?

?

?

?

?

?

?

?

0

1

2

3

0 1

0

0

iRow

iColumn

x y z

Page 18: Arrays & Structures II

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];

}

}

3

9

-4

44

25

16

4

2

0

1

2

3

0 1

8

12

43

-7

5

-3

8

4

0

1

2

3

0 1

11

?

?

?

?

?

?

?

0

1

2

3

0 1

0

0

iRow

iColumn

x y z

Page 19: Arrays & Structures II

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];

}

}

3

9

-4

44

25

16

4

2

0

1

2

3

0 1

8

12

43

-7

5

-3

8

4

0

1

2

3

0 1

11

?

?

?

?

?

?

?

0

1

2

3

0 1

0

1

iRow

iColumn

x y z

Page 20: Arrays & Structures II

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];

}

}

3

9

-4

44

25

16

4

2

0

1

2

3

0 1

8

12

43

-7

5

-3

8

4

0

1

2

3

0 1

11

?

?

?

30

?

?

?

0

1

2

3

0 1

0

2

iRow

iColumn

x y z

Page 21: Arrays & Structures II

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];

}

}

3

9

-4

44

25

16

4

2

0

1

2

3

0 1

8

12

43

-7

5

-3

8

4

0

1

2

3

0 1

11

?

?

?

30

?

?

?

0

1

2

3

0 1

1

2

iRow

iColumn

x y z

Page 22: Arrays & Structures II

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];

}

}

3

9

-4

44

25

16

4

2

0

1

2

3

0 1

8

12

43

-7

5

-3

8

4

0

1

2

3

0 1

11

?

?

?

30

?

?

?

0

1

2

3

0 1

1

0

iRow

iColumn

x y z

Page 23: Arrays & Structures II

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];

}

}

3

9

-4

44

25

16

4

2

0

1

2

3

0 1

8

12

43

-7

5

-3

8

4

0

1

2

3

0 1

11

21

?

?

30

?

?

?

0

1

2

3

0 1

1

0

iRow

iColumn

x y z

Page 24: Arrays & Structures II

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])