ICS 101 - Computer programming

Lab # 10

(ONE-DIMENSIONAL ARRAYS) I

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.

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:

7

13      42      31      14      25      36      27

COUNT OF EVEN ELEMENTS = 3

COUNT OF ODD   ELEMENTS = 4

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 program to read a 1-D array A of size 7 and another 1-D array B of size 4. Then the program finds and prints the number of occurrences of each element of array B in array A.

Use the following data:

4          3          4          6          4          2          3

2          4          3          9