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

 

Individual Elements of Array as parameters

q       This is similar to passing simple arguments

q       The formal parameter should be a simple variable

q       The actual parameter should be the array name followed by the particular index in []

 

Example 1

 

/* reads a 1-D array and prints the sum of

   even and odd numbers */

 

#include <stdio.h>

#define SIZE 4

 

/* determines if an element is odd */

int odd(int num)

{  int isodd;

 

   isodd = num%2==1;

   return isodd;

}

 

main()

{ int x[SIZE], i, oddsum, evensum;

 

  printf("Enter %d integer values\n", SIZE);

 

  for (i=0; i<SIZE; i++)

      scanf("%d", &x[i]);

 

  oddsum=0;

  evensum=0;

 

  for (i=0; i<SIZE; i++)

     if (odd(x[i]))

        oddsum=oddsum+x[i];

     else

        evensum=evensum+x[i];

 

  printf("The sum of odd elements is  %d\n", oddsum);

  printf("The sum of even elements is %d\n", evensum);

 

  return 0;

}

 

 

Passing the whole array as a parameter

q       Unlike simple variables, array is not passed into a function, but rather its address is passed.

q       This is done by specifying the array name followed by brackets [] (size is not necessary).

q       This makes processing more efficient

q       The actual parameter is the array name (no brackets)

 

Example2:

 

/* reads the grades of students and prints the average grade.*/

 

#include <stdio.h>

#define SIZE 30

 

/* computes the average of elements in an array */

float average(float list[], int n)

{  int i;

   float sum=0.0;

 

   for (i=0; i<n; i++)

      sum+=list[i];

   return sum/n;

}

 

main()

{ int i,n;

     float grades[SIZE];

 

  printf("Enter number of students: ");

  scanf(“%d”,&n);

  printf("Enter grades for %d students\n", n);

 

  for (i=0; i<n; i++)

      scanf("%f", &grades[i]);

 

  printf("The Average grade is %.2f\n", average(grades, n));

  return 0;

}


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

 

  1. Write a function member that receives an array of integer, the size of the array and an integer variable.  The function then returns true if the integer variable is in the array and false otherwise.  Test your function by writing a main program that reads the size (maximum 10) and values of an array and then repeatedly read an integer value from the user and prints one of the following messages:

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.