Question Description
Lab10
Two-Dimensional Array Data Type
A two-dimensional array is a collection of components of the same type that is structured in two dimensions. Individual components are accessed by their position within each dimension. Three types are associated with a two-dimensional array data type: the type of the items to be stored in the individual places in the structure, the type of the index for the first dimension, and the type of the index for the second dimension. In C++, the type of both indexes must be integral.
const int MAX_ROWS = 10;
const int MAX_COLUMNS = 5;
float twoDimAry[MAX_ROWS][MAX_COLUMNS];
twoDimAry is an array variable that has 10 rows and 5 columns. Each row and column entry is of type float. The following code fragment sets all the entries in twoDimAry to zero:
for (int column = 0; column < MAX_COLUMNS; column++)
for(int row = 0; row < MAX_ROWS; row++)
twoDimAry[row][column] = 0.0;
Two-Dimensional Array Processing
The number of rows and columns in the two-dimensional array variable is fixed at compile time. The number of rows and columns that contain valid data items can vary as the program executes. Therefore, each dimension should have a parameter associated with it that contains the number of rows or columns actually used.
Processing a two-dimensional array variable requires two loops: one for the rows and one for the columns. If the outer loop is the index for the column, the array is processed by column. If the outer loop is the index for the row, the array is processed by row. The preceding loop processes twoDimAry by columns.
Multidimensional Arrays
You have seen one-dimensional and two-dimensional arrays. In C++, arrays may have any number of dimensions. To process every item in a one-dimensional array, you need one loop. To process every item in a two-dimensional array, you need two nested loops. The pattern continues to any number of dimensions. To process every item in an n-dimensional array, you need n nested loops.
Passing Arrays as Parameters
In the section on one-dimensional arrays, we said that the programmer passes the base address of an array and the number of elements in the array as parameters. The function does not need to know the actual size of the array. For arrays of more than one dimension, the function must know the sizes of all of the dimensions except the first. For example, if a function is defined to set the first num values of each row in twoDimAry to a specific value, the prototype might look like this:
void SetSomeVals(ItemType twoDimAry[][MAX_COLUMNS],
int rowsUsed, int num, ItemType initialValue);
Any argument must have exactly the same number of elements specified for the second dimension. If it does not, the program continues but does not initialize the correct locations. It is safer to define a type using a typedef statement, put the type name on the formal parameter list, and define the actual array to be of that type. Here is an example showing how this could be done:
const int MAX_ROWS = 10;
const int MAX_COLUMNS = 5;
typedef char ItemType;
typedef char TwoDType[MAX_ROWS][MAX_COLUMNS];
void SetSomeVals(TwoDType twoDimAry, int rowsUsed,
int num, ItemType initialValue);
Any array to be passed to SetSomeVals should be defined to be of type TwoDType. Although this example is of a two-dimensional array, this pattern of defining a type and using the type name can be used for arrays of any number of dimensions.
Problem
You have been asked to product a “Monthly Sales Report” for your neighbor’s retail chain of appliance stores. First, create a data file infile.txt containing sales data for his 7 stores.
Takoma_store 2.7 71.3 14.7 23.9 51.2
Bethesda_store 12.7 8.9 17.8 7.9 18.3
(Add 5 more data values for a total 7 stores each with 5 items each)
The first data item is the store name. It should be store in an array of 7 strings.
The next 5 float values are the sales totals for each store over the the first five months of the year. These values should be stored in a two-dimensional array (7 by 5).
Requirements
Your program must use the structure chart below:
In the Get_store_data function, you must use a nested loop to read and store the data into the array of store names and into the two-dimensional store sales array.
In the Compute_sales_total function, the store index is used to read the sales figures in the indicated row of the sales array. Using a for statement, this function should total the sales amounts and return this total to main.
Your main function should first call the Get_store_data function. Then, using a for loop, the main function should call the Compute_sales_total function sending it each of the seven indices for the rows of the sales array. Of course, the sales totals for each of the stores must be stored in main in a store totals array.
Finally, the main function must call the Display_Store_Data function send it the store sales array, the store names array, and the store totals array. This function simply prints a heading and then, using a counter-controlled loop, print the corresponding data from each array to produce the output shown below: