INFORMATION & COMPUTER SCIENCE
DEPARTMENT, KFUPM
ICS – 201 SECTION 55 & 56 (992 Semester)
INTRODUCTION TO COMPUTER SCIENCE
LAB
#01 TOPIC: Review of 1-D
arrays
Instructor: Bashir M. Ghandi
Objective:
To review 1-D arrays
--declaration, access and parameter passing
To
be able to apply 1-D array in problem solving
Introduction
Recall that arrays are very useful tools when
we need to keep several values (e.g grades of students in an exam) in
memory. Instead of using several
variables, we use one array variable.
Individual element is then accessed by specifying its index.
Examples:
1. int
a[5]; declares an array of size 5.
|
|
|
|
|
0 |
1 |
2 |
3 |
4 |
Note: In C index always starts from 0
2. a[0]=10; a[1]=20;
a[2]=30; a[3]=40; a[4]=50;
Assigns values to the elements.
3. Array
can also be initialized at point of declaration:
int
a[]={10, 20, 30, 40, 50};
Note: if you specify a size but give fewer values, the remaining cells
will be
initialised to zero
Example2:
float grades[SIZE];
Programming
Exercise:
1. Modify
Example 2 so that the program also reads the ID numbers of the students in
another array (of long int) and then prints the average grade, and a list of
IDs and grades of those students below the average.
Sample Input |
Sample Output |
Number of students: 5 101
75 102
80 103
65 104
98 105
60 |
Average grade = 75.60 Students below average ID grade 101 75 103 65 105 60 |
2. A shop sells 5 items with codes and
unit prices as shown in the following table:
CODE |
UNIT PRICE |
1 |
SR8.00 |
2 |
SR5.50 |
3 |
SR10.50 |
4 |
SR15.00 |
5 |
SR20.00 |
Write a program that reads and stored these prices in a one-dimensional float array price. The program then repeatedly reads the code and quantity of items purchased by a customer until the sentinel 0 0 is read. It then prints the total bill for that customer.
Sample Input |
Sample Output |
2 3 3 5 5 2 0 0 |
Custome6 bill = SR106.50 |
N IS
A MEMBER OF THE ARRAY OR
N IS
NOT A MEMBER OF THE ARRAY
Note: Your
program should not stop until the user enters –1.
Home Work:
Write a program
that reads two integer arrays (maximum size 10) and prints the UNION and INTERSECTION of
the two arrays. If the intersection is
empty, the program should print an appropriate message.
Note: Your
program should include the following functions:
read_array that
reads data into a 1-D array
intersection:
that computes the intersection of two arrays
union: that
computes the union of two arrays
print_array: that
prints the content of an array.
Hint: You may use the function member in problem
3.