ICS 101 - Computer programming

Lab # 12

(TWO-DIMENSIONAL ARRAYS)

 

 

Objectives

 

 

Two dimensional array is  a tabular representation  of data consisting of rows and columns .

 

 Example

 

Write a program which reads a 2-D INTEGER array of size 3 x 4 column-wise. it then prints the sum of all even numbers in the array.

 

 Use the data:  

    11        6        33

 

    7         2        22     

 

    8         14        5      

 

    9         11         3

 

Your output should be:

SUM OF ALL EVEN NUMBERS =   52

 

                        

 

        Following is the output of the program.

 

        

    

 

 

Exercise 1

 

Two matrices A and B of maximum size 5x5 have the same number of rows and columns. Write a program that reads the number of rows and the number of columns, it then reads the two matrices row-wise and it calculates the matrix C which is A + B. Print the matrix C.

 

Use the following data:

 

2         3

 

4      5      6

 

7      8      9

 

 

2      0      7

7      1      3

The output should be in the form:

6      5      13

14     9      12

 

Exercise 2

 

A car selling company sells three car models in each of its four shops. The current car stock for the company is:

 

SHOP1

SHOP2

SHOP3

SHOP4

MODEL1

5

7

3

0

MODEL2

2

6

4

8

MODEL3

3

12

10

5

 

Write a program which reads the stock data into a two-dimensional array. The program then prints tables of the total number of cars in each shop.

 

The output should be in the form:

                                SHOP#               NUMBER OF CARS

                                    1                                 10

                                    2                                25

                                    3                                17

                                    4                                13

 

Exercise 3

 

Write a program which reads the number of rows and the number of columns of a two-dimensional INTEGER array of maximum size 5 X 5 it then reads an integer value N. If N is not a valid row number the program prints the error message INVALID ROW NUMBER otherwise it reads the array row-wise and it passes it and N to an integer function PRODCT which returns the product of the elements of row N. The program then prints this product.

Use the following data:

3          4

2

    2            4            6            8

    3            3            4            5

    1            2            3            4

 

Your output should be:

PRODUCT OF ROW# 2  =       180