ICS 101 - Computer programming

Lab # 10 & 11

(ONE-DIMENSIONAL ARRAYS)

Objectives

•   ONE - DIMENSIONAL ARRAYS ( 1-D ARRAY)

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 ARRAYS AND SUBPROGRAMS

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

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 10. Print the count of those elements in the array that are odd and that are even.

Test your program with the following input:

5

13        42        31        14        62

COUNT OF EVEN ELEMENTS = 3

COUNT OF ODD   ELEMENTS = 2

Exercise 2

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

YEAR WITH MAXIMUM NUMBER OF STUDENTS IS        1998

YEAR WITH MINIMUM  NUMBER OF STUDENTS IS       1996

AVERAGE NUMBER OF STUDENTS IS         5177.000000

Exercise 3

Write a function that takes a 1-D integer array AVG 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