ICS 101 - Computer programming

Lab # 10

(ONE-DIMENSIONAL ARRAYS) I

 

 

Objectives

 

 

One dimensional array represents a group of memory locations. Each member of an array is called an element. An element is accessed by the array name followed by a subscript or an index.

 

 Example

 

  Read 5 data values into an array A and print the sum of all the elements of the array

  

 

        

 

        Following is the output of the program.

 

     

 

 

Exercise 1

 

Write a program that reads two integer arrays A and B each of size 4. The program then calls a subroutine, which computes the product of corresponding elements in both arrays and returns the result to the main program in an integer array C of the same size. Your program should then print the input arrays and the result with proper messages.

Test your program with the following data:

2          4          3          3         

5          6          2          4         

 

Your output should be:

ARRAY A:                     2                      4                      3                      3

ARRAY B:                     5                      6                      2                      4

ARRAY C:                    10                    24                      6                     12

 

Exercise 2

 

Read an integer N and then read N data values into an array of size 20. Print the count of those elements in the array that are odd and that are even.

Test your program with the following input:

11

13      42      31      14      25      36      27      28      19      12      62                      

Your output should be:

    COUNT OF EVEN ELEMENTS = 6

    COUNT OF ODD   ELEMENTS = 5

 

 

 

Exercise 3

 

Write a program that reads the number of students at KFUPM from year 1995 to 2000 and prints the following:

1.      The year with maximum number of students.

2.      The year with minimum number of students.

3.      The average number of students through all years 1995 to 2000.

Assume that no two years have the same number of students. You must use the 1-D array declaration INTEGER NUMST(1995:2000). Test your program with the following input:

 

                                    5000      4000      5012      6000      5500      5550

Your output should be:

    YEAR WITH MAXIMUM NUMBER OF STUDENTS IS        1998

    YEAR WITH MINIMUM  NUMBER OF STUDENTS IS       1996

    AVERAGE NUMBER OF STUDENTS IS         5177.000000