Lab # 8

(1-D Array)

 

© Manzur Ashraf

Objective:

 

 

 

 

 


Introduction:

 

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 language array 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 initialised 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

 

 

 

 

 

 

Basic overview of 1-D array:

 

Till now to read n number of integers (or float, double, character) we took n number of variables. For example, to read say 5 integers we used 5 integer variables say a, b, c, d, e. But with arrays we can use a single variable name to represent any number of variables of the same data type. The individual elements in the array can be accessed by supplying an index or subscript after the name of the array variable in the square brackets. The array is declared like any simple variable but the size of the array should be specified.

 

The first element of the array is always referenced with the 0 index. Array elements are always numbered from 0 to (size-1).

 

          int      arri[10] ;

         float   arrf[30] ;

 

Here two arrays are declared. The array arri contains 10 integers arri[0] to arri[9] and array arrf contains 30 floating values arrf[0] to arrf[29]. The first elements of arri and arrf are referenced as arri[0] and arrf[0]. The last elements of arri and arrf are referenced as arri[9] and arrf[29]. Similarly, the 5th element of arri can be referenced by arri[4], the 20th element of arrf can be referenced as arrf[19] and so on. Note that when the arrays are declared their sizes are also declared. Here the size of arri array is 10 and the size of arrf array is 30. Now, arri[0]…arri[9] can have any integer value say arri[3] = 123, arri[7] = 34; arrf[0]…to arrf[29] can have any float value say arrf[2] = 23.56, arrf[23] = 222.567.

 

The array size can also be given by using the define statement as shown below :

 

                   #define       SIZE1         10

                   #define       SIZE2         100

                   int      arri[SIZE1] ;

                   float  arrf[SIZE2] ;

 

 

How to read numbers into arrays ?:

 

The numbers can be read into arrays by using for loop. Consider the following example:

Example:      float  abc[25] ;

                   int      i ;

 

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

                   {        printf (“Enter the %d element \n”, i ) ;

                             scanf ( “%f”, &abc[i] ) ;              }

 

Here, the variable abc is declared as an array of size 25 consisting of float values. An integer variable i is declared to be used as a subscript for the array. Next, for loop is used starting from 0 and ending at 24 which is the range of the array abc. Inside the loop the printf statement asks the user to enter the particular element of the array and scanf reads that element. Note that here also the address operator & and the name of the array is used but the subscript is added so that all the elements can be read. The %f operator is used because the array is of float type.

 

How to print numbers from arrays ? :

 

The numbers can be printed from arrays by using for loop. Consider the following example:

 

Example:      double          abc[50] ;

                   int      n ;

 

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

                   {      printf (“The %dth  element of array is %lf\n”, n, abc[n]) ;     }

 

The printing of array is similar to reading. Here also for loop is used starting from 0 and ending at last element 49. Each time the printf statement prints the particular element of the array say for 25th element as The 25th element of array is 245.778. The %d operator is used because n is integer type and %lf is used because array abc is of double type.

 

 

How to initialize array ? :

 

Array can be initialized in their declarations like simple variables. Here the initial values for all the elements are given in the parentheses separated by commas. Examples of this are as follows:

 

          int      vector [ 5 ]  =  { 12, 67, -56, 89, 1 } ;

          float  xyz [ 8 ] = {1.5, 7.8, 5.6, -9.55, 0.5, 8.4} ;

          char   ccc [ 4 ] = {‘r’, ‘4’, ‘+’, ‘s’ } ;

          int      sss [  ] = { 12, 45, 78 } ;

 

In the second example the size of array xyz is 8 but only 6 initial values are given. In this case the remaining 2 elements xyz[6] and xyz[7] will have 0 as their initial value. Thus if the initialization list is smaller than the size the remaining elements are initialized with 0. In the last example the size of the array is not given but from the initialization list the size of the array is assumed as 3.

 

Now consider the problem of initializing a very big array of size say 400 with the same value for all the elements. It will be very difficult to initialize all the 400 elements by using the initialization list in the declaration. To avoid this the initialization can be done by using the for loop.

 

 

Example:                float  abc[400] ;

                             int      i;

                             for (i = 0 ; i < 400 ; ++i )  {   abc [ i ] = 1.0 ;      }

 

In the above example all the 400 elements of the array abc are initialized with the 1.0 value by using the for loop.

 

Solved Example:

 

#include<stdio.h>

void main()

{

                   float  grades[5] ; // array declaration

                   int      i ;

                   printf("Please , Enter five grades to store in array : \n");

                    printf("*********************************************\n");

                   printf("\n");

                   for (i = 0; i < 5; ++i) // loop to read (or store) five grades in array

                   {

                             printf ("Enter the %d element of array :", i ) ;

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

 

                   }

     

                   for (i = 0; i < 5; ++i) // loop to display five grades stored by

                                                // you in array  grades

                   {

                     printf ("The %d th  element of array is %lf\n", i, grades[i]) ;

                   }

 

}  // end of main

Exercises:

 

Problem#1:

Cut & Past Solve Example given above. Compile , Execute and understand it.

Now modify it to display average  and  maximum  of grades.

 

Problem#2:

Create three arrays. Read data into the first two of them. Subtract each element in the first array from the corresponding element in the second array. Store the differences in the third array. Print all the arrays.