ICS 101 - Computer programming

Lab # 10 & 11

(ONE-DIMENSIONAL ARRAYS)

 

 

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.

 

     

 

 

One dimensional array can be passed to a subprogram or can be used locally within a subprogram. In both cases, the array  must be declared within the subprogram.

 

 Example

 

Write a program which reads a 1-D INTEGER array of size 6 it then prints the product of the positive elements (i.e. elements greater than zero) in the array. The product must be computed by an INTEGER function PRODCT invoked by the program.

 

 Use the data:  -2     3     2     5     -4     2

 

Your output should be:

PRODUCT OF POSITIVE ELEMENTS =          60

 

 

                     

 

 

        Following is the output of the program.

 

        

 

 

 

Exercise 1

 

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 2

 

Write a function that takes a 1-D integer array and returns the average of its elements.

Then write a main program that reads a 1-D integer array of size 6, calls the Function

and prints the average of the elements of the 1-D array.

 

Use the data:

2          4          10          8          5          3

 

Your output should be:

the average of the elements =          5.333333

 

 

Exercise 3

 

Write a program that reads the grades of a number of students that does not exceed 20. The program should then pass these grades to a subroutine that computes and returns: (1) the average of the grades, (2) the maximum grade and (3) the count of grades below the average . The values returned to the main program should then be printed.

 

Use the data:

7

87.5       93.0       56.5        69.5       97.5       36.0     78.0

 

Your output should be:

THE AVERAGE OF THE GRADES =  74.0

THE MAXIMUM GRADE = 97.5

COUNT OF GRADES  BELOW THE AVERAGE = 3