Chapter 7.4 Multidimensional Arrays Goals: To examine the basics of multidimensional arrays in C++...

11
Chapter 7.4 Chapter 7.4 Multidimensional Arrays Multidimensional Arrays Goals: Goals: To examine the basics of multidimensional arrays in C++ To examine the basics of multidimensional arrays in C++

Transcript of Chapter 7.4 Multidimensional Arrays Goals: To examine the basics of multidimensional arrays in C++...

Page 1: Chapter 7.4 Multidimensional Arrays Goals: To examine the basics of multidimensional arrays in C++ To examine the basics of multidimensional arrays in.

Chapter 7.4Chapter 7.4Multidimensional ArraysMultidimensional Arrays

Goals:Goals:

• To examine the basics of multidimensional arrays in To examine the basics of multidimensional arrays in C++C++

Page 2: Chapter 7.4 Multidimensional Arrays Goals: To examine the basics of multidimensional arrays in C++ To examine the basics of multidimensional arrays in.

Chapter 11Chapter 11CS 140CS 140 Page Page 22

Multidimensional ArraysMultidimensional Arrays

It is possible to declare arrays of more than one It is possible to declare arrays of more than one dimension in C++. These arrays are essentially dimension in C++. These arrays are essentially arrays of arrays...arrays of arrays...

double hrsWorked[52][7];double hrsWorked[52][7];52 rows & 7 52 rows & 7

columns, for a columns, for a total of 364 double total of 364 double

values!values!

int second[24][60][60];int second[24][60][60];24 rows, 60 columns, 24 rows, 60 columns, & 60 layers for a total & 60 layers for a total

of 79200 integer of 79200 integer values!values!

Page 3: Chapter 7.4 Multidimensional Arrays Goals: To examine the basics of multidimensional arrays in C++ To examine the basics of multidimensional arrays in.

Chapter 11Chapter 11CS 140CS 140 Page Page 33

A Simple ExampleA Simple Example

#include <iostream>using namespace std;void main(){ int dozen[3][4] = {11,12,13,14,35,36,37,38,60,70,80,90}; int row; int col;

for (row = 0; row < 3; row++) for (col = 0; col < 4; col++) { cout << dozen[row][col]; if (col != 3) cout << " | "; else if (row != 2) cout << endl << " | | | " << endl << "---+----+----+---" << endl << " | | | " << endl; } cout << endl << endl;

return;}

The The initialization of initialization of

the two-the two-dimensional dimensional

array loads the array loads the array one row array one row

at a time.at a time.

Page 4: Chapter 7.4 Multidimensional Arrays Goals: To examine the basics of multidimensional arrays in C++ To examine the basics of multidimensional arrays in.

Chapter 11Chapter 11CS 140CS 140 Page Page 44

////////////////////////////////////////////////////////////// This program reads data from an input file regarding a //// list of scheduled flights, and then outputs this data //// in a readable format to the monitor. //////////////////////////////////////////////////////////////

#include <iostream>#include <iomanip>#include <fstream>#include <string>using namespace std;const int MAX_NBR_FLIGHTS = 10;

enum CityCode { Atlanta = 1, Chicago, Dallas, Denver, LosAngeles, NewYork, Seattle };

void loadData(int flightData[][6], int &nbrOfRows);void computeTimes(const int data[][6], int time[], int nbrFlights);int timeChange(CityCode departCity, CityCode arriveCity);void outputChart(const int flyTable[][6], const int minutes[], int nbrRows);void outputFlightInfo(const int flightInfo[], const int minutes);void convertCodeToCity(CityCode city, string cityString);void computeTime(int time, int &hour, int &min, string AMorPM);double computePrice(int nbrMinutes, int nbrMiles);

A Large Example: Flight SchedulesA Large Example: Flight SchedulesThe prototypes may The prototypes may neglect to specify neglect to specify at at

most onemost one of the of the dimensions of a dimensions of a

multidimensional multidimensional array parameter.array parameter.

Otherwise, the Otherwise, the compiler will be compiler will be

unable to distinguish unable to distinguish the dimensions.the dimensions.

Page 5: Chapter 7.4 Multidimensional Arrays Goals: To examine the basics of multidimensional arrays in C++ To examine the basics of multidimensional arrays in.

Chapter 11Chapter 11CS 140CS 140 Page Page 55

// The main function coordinates the retrieval of the flight //// data, the calculation of each flight's time in the air, //// and the output of the flight info in a readable format. //void main(){ int numberOfFlights; int flightData[MAX_NBR_FLIGHTS][6]; int elapsedTime[MAX_NBR_FLIGHTS]; loadData(flightData, numberOfFlights); computeTimes(flightData, elapsedTime, numberOfFlights); outputChart(flightData, elapsedTime, numberOfFlights); return;}

// This function loads a two-dimensional array of integers //// with a prepackaged set of data about various flights. //void loadData(int flightData[][6], int &nbrOfRows){ ifstream flightFile; int flightNbr; nbrOfRows = 0; flightFile.open("flightData.txt"); flightFile >> flightNbr; while (!flightFile.eof()) { flightData[nbrOfRows][0] = flightNbr; // Flight Number flightFile >> flightData[nbrOfRows][1]; // Source City Code flightFile >> flightData[nbrOfRows][2]; // Destination City Code flightFile >> flightData[nbrOfRows][3]; // Departure Time (Military) flightFile >> flightData[nbrOfRows][4]; // Arrival Time (Military) flightFile >> flightData[nbrOfRows][5]; // Distance In Miles nbrOfRows++; flightFile >> flightNbr; } return;}

The main function The main function “knows” that the “knows” that the flightData array flightData array

has 10 rows and 6 has 10 rows and 6 columns.columns.

The loadData The loadData function “knows” function “knows” that the array has that the array has 60 entries and 6 60 entries and 6 columns, so it columns, so it

“deduces” that “deduces” that there are 10 there are 10

rows!rows!

Page 6: Chapter 7.4 Multidimensional Arrays Goals: To examine the basics of multidimensional arrays in C++ To examine the basics of multidimensional arrays in.

Chapter 11Chapter 11CS 140CS 140 Page Page 66

// This function calculates the total amount of time, in minutes, //// between each flight's takeoff time and its arrival time. This //// data is stored in the parameterized array named time. //void computeTimes(const int data[][6], int time[], int nbrFlights){ int i, hours, minutes;

for (i = 0; i < nbrFlights; i++) { hours = (data[i][4] / 100) - (data[i][3] / 100); if (hours < 0) hours += 24; hours += timeChange(CityCode(data[i][1]), CityCode(data[i][2])); minutes = (data[i][4] % 100) - (data[i][3] % 100); if (minutes < 0) { minutes += 60; hours--; } time[i] = 60 * hours + minutes; } return;}

The integer data in columns 1 The integer data in columns 1 & 2 of that& 2 of that data data array must be array must be

typecast to the enumeratedtypecast to the enumerated CityCode CityCode type before being type before being passed to thepassed to the timeChange timeChange

function.function.

Page 7: Chapter 7.4 Multidimensional Arrays Goals: To examine the basics of multidimensional arrays in C++ To examine the basics of multidimensional arrays in.

Chapter 11Chapter 11CS 140CS 140 Page Page 77

// This function uses a seven-city numerical code to determine the //// time difference between two cities, with positive numbers used //// for east-to-west flights, and negative numbers for west-to-east //// flights. Thus, for example, a NY-to-LA flight yields a result //// of 3, while an LA-to-NY flight yields a result of -3. //int timeChange(CityCode departCity, CityCode arriveCity){ int departTimeZone, arriveTimeZone;

switch(departCity) { case(Atlanta): case(NewYork): {departTimeZone = 3; break;} case(Chicago): case(Dallas): {departTimeZone = 2; break;} case(Denver): {departTimeZone = 1; break;} case(LosAngeles): case(Seattle): {departTimeZone = 0; break;} }

switch(arriveCity) { case(Atlanta): case(NewYork): {arriveTimeZone = 3; break;} case(Chicago): case(Dallas): {arriveTimeZone = 2; break;} case(Denver): {arriveTimeZone = 1; break;} case(LosAngeles): case(Seattle): {arriveTimeZone = 0; break;} }

return (departTimeZone - arriveTimeZone);}

Page 8: Chapter 7.4 Multidimensional Arrays Goals: To examine the basics of multidimensional arrays in C++ To examine the basics of multidimensional arrays in.

Chapter 11Chapter 11CS 140CS 140 Page Page 88

// This function outputs the readable table of flight data to the monitor. //void outputChart(const int flyTable[][6], const int minutes[], int nbrRows){ int flight;

cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2);

cout << " ICARUS AIRLINES FLIGHT SCHEDULE" << endl << endl << "Flight Departure Departure Arrival Arrival Ticket" << endl << "Number City Time City Time Price" << endl << "------ --------- --------- ------- ------- ------" << endl;

for (flight = 0; flight < nbrRows; flight++) outputFlightInfo(flyTable[flight], minutes[flight]); cout << endl << endl;

return;}

TheThe outputFlightInfo outputFlightInfo function is function is being sent a being sent a single rowsingle row of the of the flyTable flyTable 2-D array, as well as a 2-D array, as well as a single entrysingle entry of the of the minutes minutes 1-D 1-D

array!array!

Page 9: Chapter 7.4 Multidimensional Arrays Goals: To examine the basics of multidimensional arrays in C++ To examine the basics of multidimensional arrays in.

Chapter 11Chapter 11CS 140CS 140 Page Page 99

// This function outputs the flight information in the //// parameterized array, using the parameterized integer //// value to assist in computing the ticket price. //void outputFlightInfo(const int flightInfo[], const int minutes){ int hour; int min; string city; string AMorPM[3]; double ticketPrice;

cout << ' ' << flightInfo[0] << " ";

convertCodeToCity(CityCode(flightInfo[1]), city); cout << city << setw(14-strlen(city)) << ' ';

computeTime(flightInfo[3], hour, min, AMorPM); cout << setw(2) << hour << ':'; (min < 10) ? (cout << '0' << min) : (cout << min); cout << AMorPM;

convertCodeToCity(CityCode(flightInfo[2]), city); cout << " " << city << setw(15-strlen(city)) << ' ';

computeTime(flightInfo[4], hour, min, AMorPM); cout << setw(2) << hour << ':'; (min < 10) ? (cout << '0' << min) : (cout << min); cout << AMorPM;

ticketPrice = computePrice(minutes, flightInfo[5]); cout << " $" << ticketPrice << endl;

return;}

This function is This function is unaware of the unaware of the

fact that its first fact that its first parameter is parameter is

actually a row of a actually a row of a 2-D array, and 2-D array, and that its second that its second parameter is parameter is

actually an entry actually an entry in a 1-D array!in a 1-D array!

Page 10: Chapter 7.4 Multidimensional Arrays Goals: To examine the basics of multidimensional arrays in C++ To examine the basics of multidimensional arrays in.

Chapter 11Chapter 11CS 140CS 140 Page Page 1010

// This function uses the numerical code associated with a particular //// city to yield the character string representing that city's name. //void convertCodeToCity(CityCode city, string cityString){ switch(city) { case(Atlanta) : {cityString = "Atlanta"; break;} case(Chicago) : {cityString = "Chicago"; break;} case(Dallas) : {cityString = "Dallas"; break;} case(Denver) : {cityString = "Denver”; break;} case(LosAngeles): {cityString = "Los Angeles"; break;} case(NewYork) : {cityString = "New York"; break;} case(Seattle) : {cityString = "Seattle”; break;} } return;}

// This function uses the military time represented in the parameter time //// to compute the civilian time in hours and minutes, as well as a string //// indicating whether that time is AM or PM. //void computeTime(int time, int &hour, int &min, string AMorPM){ (time < 1200) ? (AMorPM = "AM”) : (AMorPM = "PM”); hour = ((time/100 - 1) % 12 + 1); min = time % 100;

return;}

Page 11: Chapter 7.4 Multidimensional Arrays Goals: To examine the basics of multidimensional arrays in C++ To examine the basics of multidimensional arrays in.

Chapter 11Chapter 11CS 140CS 140 Page Page 1111

// This function computes the price of a flight, using $2.50 //// per minute or $0.25 per mile, whichever is more expensive. //double computePrice(int nbrMinutes, int nbrMiles){ double timePrice, distancePrice, price;

timePrice = 2.50 * nbrMinutes; distancePrice = 0.25 * nbrMiles; price = (timePrice > distancePrice) ? (timePrice) : (distancePrice);

return price;}

1182 7 5 510 721 11347498 5 3 807 1215 13999256 3 1 1250 1529 8223037 1 6 1620 1755 8546045 6 2 1854 2000 8093572 2 4 2101 2212 10214168 4 7 2319 103 1341

Input fileInput file flightData.txt flightData.txt

Resulting outputResulting output