One-dimensional arrays hold data that is organized linearly in one direction. But data that is to be stored in more that one dimension cannot be stored directly in a one-dimensional array. A two-dimensional array may be visualized as a table consisting of rows and columns. C looks at the two-dimensional array as an array of arrays.
A Two dimensional 4x4 array:
4 columns ------------- | | | | | ------------- 4 | | | | | rows ------------- | | | | | ------------- | | | | | -------------
Two dimensional arrays can be declared as follows:
int A[4][5]; /* A 0 1 2 3 4 ---------------- 0 | | | | | | ---------------- 1 | | | | | | ---------------- 2 | | | | | | ---------------- 3 | | | | | | ---------------- */
By convention, the first dimension specifies the number of rows in the array. The second dimension specifies the number of columns in each row.
In the above example the array A has 4 rows and each row has 5 columns. The array can hold 20 integer variables.
To refer to an individual member of an array we use the square-bracket form. But unlike one-dimensional arrays we need an element from two dimensions. Inorder to do that we need to specify the row and the column for the element.
For example: A[2][3] is the element which is located in row 2 and column 3.
There are one basic loop we use to process the elements of a 2D array:
Processing ALL of the elements of a 2D array: do initial setup for (I = 0; I < NUM_ROWS; I++) { /* process each row I */ setup for row I for (J = 0; J < NUM_COLS; J++) /* process each column J of row I */ process ARRAYNAME[I][J] /* refers to element in row I and column J */ post process row I } post process
This loop can also be adjusted by changing the nesting order to process the two-dimensional array a column at a time.
In this lab we are going to get some experience with processing a two-dimensional array.
The idea is, we have a set of employees and a set of products each one of them sold. So the rows of the two-dimensional array correspond to Employees, while the columns correspond to the different products each one of them sold.
P1 P2 P3 P4 ... ---------------- Employee 1 | | | | | ---------------- Employee 2 | | | | | ---------------- Employee 3 | | | | | ---------------- . .
The following program reads in a group of values using a process all loop.
To this program we want to add two processes:
#include <stdio.h> #include <stdlib.h> void main() { int I; int J; int sales[5][6]; /* consider 5 employees and 6 products */ int total = 0; /* Declare two 1D arrays, one for the employee totals and one for the product totals */ /* following process loop is used to read in input values */ for (I = 0; I < 5; I++) { printf("Please enter details for Employee %d:\n",I+1); for (J = 0; J < 6; J++) { printf(" Sales for Product %d: ",J+1); scanf("%d",&(sales[I][J])); } printf("\n"); } /* following process loop is used to display the table */ printf("\n Sales Table: \n"); printf(" "); for (I=0; I < 6; I++) printf(" P%d ",(I+1)); printf("\n"); printf(" -"); for (I=0; I < 6; I++) printf("-----"); printf("\n"); for (I=0; I < 5; I++) { printf(" E%d| ",I+1); for (J=0; J < 6; J++) printf(" %3d ",sales[I][J]); printf("\n"); } /* Initialize the 1D array for employee totals */ /* Add a loop to calculate the total number of products sold by each employee */ /* Initialize the 1D array for product totals */ /* Add a loop to calculate the total number of products sold of each type */ }
Turn in a hard copy of your final program. Also, turn in a copy of output for your program showing that it works correctly.