2-DIMENSIONAL ARRAYS

 

Problem # 1

 

Write a program which reads the number of rows and columns of a two-dimensional REAL array QUIZ of maximum size 10 x 20. The program then reads quiz grades into the array and calls a function which returns the maximum quiz average. The program then prints the average. Use the following data:

 

3   4

50.0     67.0     80.0     40.0     ß----- Quiz#1

90.0     55.0     75.0     60.0     ß----- Quiz#2

68.0     80.0     92.0     55.0     ß----- Quiz#3

 

Your output should be:

MAXIMUM QUIZ AVERAGE  =  73.750000

 

Problem # 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

                       

(*) Problem # 3

 

An attendance sheet for a section of 3 students for 4 weeks is represented as a two-dimensional INTEGER array, with 1 indicating attendance and 0 absence. Write a program to find the student or students with the maximum number of absences.

Hint: Use a 1D-array of size 3 to store the sum of each row.

 

 

WEEK1

WEEK2

WEEK3

WEEK4

960000

1

1

0

1

960001

1

0

1

0

960002

0

0

1

1

Your output should be:

STUDENT(S) WITH THE MAXIMUM NUMBER OF ABSENCES:

            960001

            960002

Problem # 4

 

A polynomial such as:

            3x4 - 5x2 + x  +  6

can be represented by a two-dimensional array as:

           

3

4

0

0

-5

2

1

1

6

0

 

Write a program which will prompt for a value X and then compute the value of an arbitrary polynomial of X. The other input to the program is the degree N of the polynomial (i.e., the highest exponent in the polynomial e.g. 4 in the above example) and then the values of the coefficients and exponents.

 

Use the following data:

2.0                   ß------ X

4

3          4

0          0

-5         2

1          1

6          0

 

NOTE: The number of rows in the two-dimensional array is N + 1. Assume that  N + 1  £  10

Your output should be:

THE VALUE OF THE POLYNOMIAL =                  36.000000

 

Problem # 5

 

Write a program that reads a 2-D integer array of size 3 x 3 row-wise, it then finds the location of the maximum element in the array. Assume the array has distinct elements.

Use the following data:

20        40        35

10        12        50

15        45        25

 

Problem # 6

Write a program that creates an integer power table of size 4x3 where each element is equal to the row number raised to the power of the column number. It then prints the resulting 2-D array row-wise.

Your output should be:

            1                        1                      1

            2                        4                      8

            3                        9                    27

            4                      16                    64

 

 

Problem # 7

 

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

 

Your output should be:

              6                    5                      13

            14                    9                      12

 

Problem # 8

 

Two integer matrices A and B of maximum size 5x5, each having elements greater than zero, have the same number of rows and columns. Write a program that reads the number of rows and the number of columns, reads the two matrices row-wise and  then it calculates a matrix C such that:

 

             ì  a(i,j) / b(i,j)                          if  MOD(a(i,j), b(i,j)) = 0

c(i,j)  = í                                                                                              for all i and j

             î MOD(a(i,j), b(i,j))                if MOD(a(i,j), b(i,j)) ¹ 0

           

 Finally the program prints the matrix C row-wise.

 

Use the following data:

2          3

 

8          5          6          ß---- A

7          8          9

 

2          2          4          ß---- B

7          1          3

 

Your output should be:

            4                      1                      2

            1                      8                      3

 

 

 

 

 

Problem # 9

 

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 determines if the matrices are equal or not.

 

Use the following data:

2          3

 

4          5          6

7          8          9

 

4          5          6

7          1          9

 

(*) Problem # 10

 

Write a program which reads the number of rows and the number of columns of a two-dimensional  INTEGER array of maximum size 5 ´ 5 it then reads the array row-wise and it passes it to a logical function IDNTCL which determines if any two rows of the array are identical. The main program should then print either THE ARRAY HAS IDENTICAL ROWS or THE ARRAY HAS NO IDENTICAL ROWS accordingly.

Use the following data:

4          3

1          2          3

0          0          0

2          2          2

1          2          3

 

Your output should be:

THE ARRAY HAS IDENTICAL ROWS

 

Problem # 11

 

Write a program which reads the number of rows and the number of columns of a two-dimensional  INTEGER array of maximum size 5 ´ 5 it then reads the array row-wise and it passes it to a logical function IDNTCL which returns TRUE if all columns are identical. The main program should then print one of the following messages:

            ALL COLUMNS ARE IDENTICAL

            NOT ALL COLUMNS ARE IDENTICAL

 

Use the following data:

3          4

6          6          6          6

2          2          2          2

8          8          8          8

 

Your output should be:

ALL COLUMNS ARE IDENTICAL

Problem # 12

 

Write a program which reads the number of rows and the number of columns of a two-dimensional  INTEGER array of maximum size 5 ´ 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 PRDCTR 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

 

Problem # 13

 

Write a subroutine with the following header:

SUBROUTINE TEST(MAT, PSTV, NGTV, ZERO)

That takes a 2-D integer array MAT of size 3´4 and returns the number of positive, negative and zero elements in the array. Then write a main program which reads a 2-D integer array MAT of size 3´4, calls the subroutine and then prints the number of positive, negative and zero elements in the array.        

Use the following data:

 1         8           2         -3

 2         3           2          6

 1         8          -2          4

 

Your output should be:

NUMBER OF POSITIVE ELEMENTS =                   10

NUMBER OF NEGATIVE ELEMENTS =                   2

NUMBER OF ZERO ELEMENTS =                            0

 

Problem # 14

 

Write a program which reads a two-dimensional integer array A of size 3 x 4 row-wise. The program then calls a subroutine ALLPOS which returns all the positive numbers of array A in a one-dimensional integer array B of size 12. The main program then prints these positive elements. If the array A has no positive elements the main program should print the message: NO POSITIVE ELEMENTS.

Use the following data:

-3         4          -2         1

8          -6         7          5

-1         2          1          9

Your output should be in the form:

THE POSITIVE ELEMENTS ARE:

4          1          8          7          5          2          1          9

Problem # 15

 

Write a program that reads a 2D integer array A of size 2 x 2 row-wise. The program passes the array to a logical function CHECK that returns .TRUE. if all the elements of array A are positive. The main program prints the array A column-wise and also prints one of the messages:

            ALL ELEMENTS ARE POSITIVE

            NOT ALL ELEMENTS ARE POSITIVE

accordingly.

 

Use the following data:

77        9          -3         5

 

Problem # 16

 

Write a subroutine with the following header:

 

   SUBROUTINE TEST(MAT, SUMP, SUMT)

 

That takes a 2-D integer array MAT of size 3 X 4 and computes SUMP  and SUMT which is the sum of the positive elements and the sum of all the elements, respectively, in the array. Then write a main program which reads a 2-D integer array MAT of size 3X4 row-wise, calls the subroutine, and finally prints SUMP and SUMT.

 

Use the following data:

 

 4         8           3         -3

 2         5           6          7

 1         8          -2          7

 

Problem # 17

 

Write a program which reads the data given below in the first two columns of a two dimensional logical array of size 4 x 3 and then it constructs the AND truth table.

            T          T         

            T          F         

            F          T         

            F          F

 

Your output should be in the form:

 

            P            Q       P .AND. Q

 

            T            T              T

            T            F               F

            F            T               F

            F            F               F

 

 

Problem # 18

 

Write a program that reads a 2-D array X of size 3x3 row-wise. Then it creates another 2-D array Y of same size as X such that each element of Y is equal to the square of the corresponding element in X. Print both arrays row-wise with appropriate headings.

 

Problem # 19

  

Write a program that reads 3 x 3 integer array row-wise and finds and prints the number of all elements which are divisible by both 2 and 5.

 

Use the following data:

10        2          50

20        15        45

100      25        12

 

Problem # 20

 

Write a function that takes a 2-D integer array and returns the sum of the diagonal elements. Then write a main program that reads a 2-D integer array of size 3 x 3, calls the function and prints the sum of the diagonal elements of the 2-D array.

 

Use the following data:

3          2          4

1          5          1

2          4          2

 

Problem # 21

 

Write a subroutine SUMRWS that takes a 2-D integer array A of size 3x3 and returns the sum of each row in the corresponding element of a 1-D integer array B of size 3. Then write a main program that reads a 2-D integer array of size 3x3 row-wise, calls the subroutine and print the 1-D array corresponding to the sum of each row of the    2-D array.

 

Use the following data:

3          2          4

1          5          1

2          4          2

 

Your output should be:

            ROW#             SUM

               1                      9

               2                      7

               3                      8

 

 

Problem # 22

 

Write a program that reads a two-dimensional array representing the grades of students in 3 sections. The number of students in each section is 3. The program should then:

(a)   Find the average of each section and store it in the corresponding element of a 1-D array AVRG.

(b)  Print the grades and the averages in the form given below.

 

Use the following data:

50.0     75.0     60.0                 ß------ Section#1

90.0     45.0     70.0                 ß------ Section#2

80.0     55.0     67.0                 ß------ Section#3

 

Your output should be in the form:

       QUIZ1                  QUIZ2             QUIZ3             AVERAGE

    50.000000               75.000000       60.000000       61.666670

    90.000000               45.000000       70.000000       68.333340

    80.000000               55.000000       67.000000       67.333340

 

Problem # 23

 

A company owns four stores, each of which can store four particular items. The current item stock in these stores is:

                                                           ITEMS

 

1

2

3

4

STORE#1

14

7

5

0

STORE#2

9

20

25

12

STORE#3

25

25

40

30

 

Write a program which will read the stock data in a two-dimensional INTEGER array STOCK it then determines for each store the number of items with stock below ten.

 

Your output should be in the form:

 

NUMBER OF ITEMS WITH STOCK BELOW 10:

STORE#                             NUMBER OF ITEMS

1                                 3

2                                  1

3                                  0

 

 

(**) Problem # 24

 

A patient was hospitalized for 5 days in some hospital. His temperature was taken 5 times every day. Write a program that reads the temperatures of  this patient during the five days he spent in the hospital into a two-dimensional array. The program should then pass these readings to a subroutine that computes and returns: (1) the maximum temperature recorded during the patients stay in the hospital, (2) the average temperature of those recorded in some day (the program, should prompt for and read that specific day). The values returned to the main program should then be printed.

 

Use the following data as input:

 

Tuesday    Wednesday    Thursday    Friday    Saturday

   39.2           38.0              37.6         37.5         37.2

   39.4           38.2              37.2         37.5         37.2

   39.6           38.2              37.6         37.0         37.4

   38.8           37.8              37.4         36.8         37.4

   38.6           37.8              37.8         37.0         37.0

 

(**) Problem # 25

 

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.

 

Use the following data:

6          1          8

7          5          3

2          9          4

 

(**) Problem # 26

 

The postal charges in Riyals per kilogram of a parcel are according to the following table:

 

 

ZONE#1

ZONE#2

ZONE#3

ZONE#4

NORMAL MAIL

3.00

6.00

10.00

15.00

EXPRESS MAIL

8.00

12.00

25.00

40.00

 

Write a program which reads the postal charges in a two-dimensional array. It then computes the total bill for a customer after reading a data line of the form:

 

n          T1  Z1   W1      T2  Z2  W2   . . .  Ti  Zi  Wi  . . .  Tn  Zn  Wn

 

where n is the number of parcels posted by the customer, Ti is the type of mail for parcel i (1 for Normal mail and 2 for Express mail ), Zi is the Zone to which parcel i is sent (1, 2, 3, or 4) and Wi is the weight in kilograms for parcel i.

 

Use the following data:

 

3.00     6.00     10.00   15.00

8.00     12.00   25.00   40.00

 

3          1   3   10.5       2   4   7.0         1   1   15.0