Computer Science 1511
Computer Science II

Laboratory Assignment 11
Two-Dimensional Array Manipulation
Due at the end of Lab

Introduction

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 4x5 array:

            5 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 index in each dimension. 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 the row numbered 2 (the third row since we start counting at 0) and the column numbered 3 (the fourth column since we start counting at 0).

Manipulating Arrays

The basic loop we use to process the elements of a 2D array is a nested set of for loops with one for loop for each dimension:

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.

                       Product
                     P1 P2 P3 P4 ... 
                    ----------------        
        Employee 1  |  |  |  |  | 
                    ----------------        
        Employee 2  |  |  |  |  |
                    ----------------        
        Employee 3  |  |  |  |  |
                    ----------------        
           .
           .

The Program

The following program reads in a group of values using a process all loop.

To this program we want to add two processes:

  1. A loop to calculate the total number of products sold by each employee
    For employee 1, total products sold = P1(employee 1) + P2(employee 1) + P3(employee 1) ...
  2. A loop to calculate the total number of products sold of each type.
    For product 1, total sold = P1(employee 1) + P1(employee 2) + P3(employee 3) ...
#include <stdio.h>
#include <stdlib.h>

int main() {
  int I;
  int J;
  int sales[5][4];    /* consider 5 employees and 4 products */
  int total = 0;

  /* Declare two 1D arrays, one for the employee totals and one for the
     product totals. */

               
  /* The following set of loops is used to read in the input values. */

  for (I = 0; I < 5; I++) {
    printf("Please enter details for Employee %d:\n",I+1);
    for (J = 0; J < 4; J++) {
      printf("  Sales for Product %d: ",J+1);
      scanf("%d",&(sales[I][J]));
    }
  }
       
  /* following process loop is used to display the table  */

  printf("\n Sales Table: \n");
  printf("      ");
  for (I=0; I < 4; I++)
    printf("  P%d ",(I+1));
  printf("\n");

  printf("     -");
  for (I=0; I < 4; I++)
    printf("-----");
  printf("\n");

  for (I=0; I < 5; I++) {
    printf("  E%d| ",I+1);
    for (J=0; J < 4; 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 */

  /* Print out the 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 */

  /* Print out the number of products sold by type */

  printf("Press return to finish\n");
  fflush(stdin);
  getchar();
  return 0;
}

What to turn in

Turn in a hard copy of your final program. Also, turn in a copy of output for your program showing that it works correctly.