INFORMATION & COMPUTER SCIENCE DEPARTMENT, KFUPM

ICS – 201 SECTION 55 & 56  (992 Semester)

INTRODUCTION TO COMPUTER SCIENCE

LAB  #04 TOPIC: 2-D Arrays

Instructor: Bashir M. Ghandi

 

Objective:       Learn how to declare, initialize & use 2-D arrays

                        Lean how to pass 2-D arrays as parameters to functions.

 


Declaration

·        2-D array can be visualized as a matrix or table of data elements consisting of rows and columns

·        Declaration is similar to 1-D array except that the size of both rows and columns must be specified, each in its own bracket.

Example:   int grades[100][6]

·        The first size indicates the number of rows, the second the number of columns 

·        The indexing starts from 0.  0…99 & 0…5 in the  above example

 

Initialization

·        2-D array can be initialized at declaration, using either of the following methods:

o       x[3][2] = { {17,22}, {-33,65}, {14,31}}

o       x[3][2] = {17, 22, -33, 65, 14, 31}

·        Each of these methods inserts the data into the array row-wise.

·        In either of these methods, if the data is not enough, the remaining elements are set to zero.

·        Data can also be read into a 2-D array using nested loops.  This can be row-wise or column-wise as shown below:

Row-wise

for (row=0; row<rsize, row++)

for (col=0; col<csize; col++)

       scanf(“%d”, &a[row][col]);

 

Column-wise

for (col=0; col<csize, col++)

for (row=0; row<rsize; row++)

       scanf(“%d”, &a[row][col]);

 

Passing multi-dimensional array as a parameter

·        This is similar to 1-D array  - the argument is the array name followed by brackets for each of the dimensions

·        However, the sizes of all the dimensions except the first, must be specified

 

Example:

The following function computes the maximum element of a 2-D integer array of maximum size 10 by 10 and of actual size, m by n.

int max(int a[][10], int m, int n)

{  int row,col, max=a[0][0];

   for (row=0; row<m; row++)

      for (col=0; col<n; col++)

         if (a[row][col] > max)

            max=a[row][col];

   return max;

}

 

·        Note that in the above function declaration, the parameter a[][10] actually represents a pointer to the first element of the array.  Thus the array is being passed by-reference (as an output parameter) even though the * operator is not used.  In fact a[][10] can as well be replaced by a pointer variable such as *p.

 

·        When calling the function above, we simply give the array name as the argument as in:

#include <stdio.h>

main()

{   int a[10][10], nrows, ncols,max;

     …………

       m=max(a,nrows,ncols);

       ……………

   }

 

Notice that even though the array is being passed as an output argument, we don’t use the & operator.

 

Programming Assignment:

1.         The program in the file LAB4_1.C (click its name to download it), reads two matrices of variable sizes (maximum of 10 by10) , multiply them and print the result.

            Study the program and run it to see how it works.

 

2.         Rewrite the matrix multiplication program in (1) above to define and use the following functions:

a.        input_mat to read the two matrices from the keyboard

b.       multiply_mat to compute the product of the two matrices

c.        dispaly_mat to print the matrix product

3.         Add the following functions to your program in 2 above:

a.       add_mat to add to matrices.

b.      scale_mat to multiply each element of a givem matrix by a given scalar (int) value

c.       transpose_mat to transpose a given matrix

d.      modify the main program so that it also prints:

1.      the sum of the two matrices

2.      the transpose of each of the matrices

3.      the scaling of the first matrix by 3.

 

Home Work: To be submitted both on diskettes and on paper(printed)

A magic square is an n x n array of integers from 1 to n 2 such that the sums of every row, column, and diagonal are equal. Write a program which will read a 3 x 3 array row-wise and then it passes the array to a logical function MGCSQR which returns .TRUE. if the array is a magic square. The main program one of the messages: MAGIC SQUARE or NOT A MAGIC SQUARE accordingly.

 

The following is an example of a magic square.

6          1          8

7          5          3

2          9          4