Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Topic seven: Arrays

1
Definition
An array is a grouping of contiguous memory
segments.
Also an array can be defined as a collection of
similar elements.
Arrays allow one to build collections of like
variables.
Introduction
For understanding the arrays properly, let us consider the
following program
#include<stdio.h>
main( ){
int x ;
x=5;
x = 10 ;
printf ( "x = %d", x ) ;
return 0;}
This program will print the value of x as 10 because when a value
10 is assigned to x, the earlier value of x which was 5, is lost.
Thus, ordinary variables (the ones which we have used so far)
are capable of holding only one value at a time (as in the above
example). However, there are situations in which we would want
to store more than one value at a time in a single variable.
For example, suppose we wish to arrange the percentage marks
obtained by 100 students in ascending order. In such a case we
have two options to store these marks in memory:
a) Construct 100 variables to store percentage marks obtained
by 100 different students, i.e. each variable containing one
student’s marks.
b) Construct one variable (called array or subscripted variable)
capable of storing or holding all the hundred values.
Obviously, the second alternative is better. A simple reason for
this is, it would be much easier to handle one variable than
handling 100 different variables.
An array is a collective name given to a group of
‘similar quantities’. These similar quantities
could be percentage marks of 100 students, or
salaries of 300 employees, or ages of 50
employees. What is important is that the
quantities must be ‘similar’. Each member in the
group is referred to by its position in the group.
As stated before that an array is a collection of similar elements.
These similar elements could be all ints, or all floats, or all chars,
etc.
Usually, the array of characters is called a ‘string’, whereas an
array of ints or floats is called simply an array. Remember that all
elements of any given array must be of the same type. i.e. we
cannot have an array of 10 numbers, of which 5 are ints and 5 are
floats.
Arrays in C share the following characteristics:
-Variables in an array share the same name
-Variables in an array share the same data type
-Individual variables in an array are called elements
-Elements in an array are accessed with an index
number
Like any other variable, arrays occupy memory space. Moreover,
an array is a grouping of contiguous memory segments, for
example as demonstrated in figure below

The six-element array above starts with index 0. Elements in an


array begin with index number zero. There are six array
elements, starting with element 0 and ending with element 5.
One-dimensional Arrays
• There are occasions when it is suitable to use a one-
dimensional array. For example;
• The number of pages in each chapter of a book
• A list of students’ marks
• A list of phone numbers
A Simple Program Using Array
A program to find average marks obtained by 30 students in a test.
#include<stdio.h>
main(){
int avg, sum = 0;
int i;
int marks[30]; /* array declaration */
for(i=0;i<=29;i++){
printf("Enter marks ");
scanf("%d",&marks[i]);} /* store data in array */
for(i=0;i<=29;i++){
sum=sum+marks[i];} /* read data from an array*/
avg=sum/30;
printf("Average marks = %d",avg);
return 0;}
Array Declaration
To begin with, like other variables an array needs to be declared
so that the compiler will know what kind of an array and how
large an array we want. In our program we have done this with
the statement:

int marks[30] ;

Here, int specifies the type of the variable, and the word marks
specifies the name of the variable. The [30] tells how many
elements of the type int will be in our array. This number is often
called the ‘dimension’ of the array. The bracket ( [ ] ) tells the
compiler that we are dealing with an array.
Accessing Elements of an Array
Once an array is declared, all the array elements
are numbered, starting with 0. Thus, marks[2] is
not the second element of the array, but the
third as stated in previous slides. In our program
we are using the variable i to refer to various
elements of the array. This variable can take
different values and hence can refer to the
different elements in the array in turn.
Entering Data into an Array
Here is the section of code that places data into an array:
for ( i = 0 ; i <= 29 ; i++ )
{
printf ( "\nEnter marks " ) ;
scanf ( "%d", &marks[i] ) ;
}
The for loop causes the process of asking for and receiving a student’s marks
from the user to be repeated 30 times. The first time through the loop, i has a
value 0, so the scanf( ) function will cause the value typed to be stored in the
array element marks[0], the first element of the array. This process will be
repeated until I becomes 29. This is last time through the loop, which is a
good thing, because there is no array element like marks[30].

In scanf( ) function, we have used the “address of” operator (&) on the
element marks[i] of the array, just as we have used it earlier on other
variables. In so doing, we are passing the address of this particular array
element to the scanf( ) function, rather than its value; which is what scanf( )
requires.
Reading Data from an Array
The balance of the program reads the data back out of
the array and uses it to calculate the average. The for
loop is much the same, but now the body of the loop
causes each student’s marks to be
added to a running total stored in a variable called sum.
When all the marks have been added up, the result is
divided by 30, the number of students, to get the
average.
for ( i = 0 ; i <= 29 ; i++ )
sum = sum + marks[i] ;
avg = sum / 30 ;
printf ( "\nAverage marks = %d", avg ) ;
Initializing One-Dimensional Arrays
There are two ways to initialize your arrays: within the array declaration and
outside of the
array declaration.
First way:
int rate[5] = {0, 1, 2, 3, 4};

second way:
Another way to initialize your array elements is to use looping structures such
as the for loop.
To demonstrate, examine the following program code.
#include <stdio.h>
main()
{
int x;
int rate[5];
for ( x = 0; x < 5; x++ ){
rate[x] = 0;}
}
To print the entire contents of an array, you also will need to use
a looping structure, as demonstrated in the next program.
#include <stdio.h>
main(){
int x;
int rate[5];/*initialize array elements*/
for ( x = 0; x < 5; x++ ){
rate [x] = x;}/*print array element contents*/
for ( x = 0; x < 5; x++ ){
printf("\nThe value of rate index %d is %d\n", x, rate[x]);}
return 0;}
Accessing elements in array
#include <stdio.h>
main(){
int x;
int a= 0;
int rate[5];
for (x=0;x<5;x++){
rate[x] = (x + 5);}
do {
printf("\nEnter a valid index (0-4): ");
scanf("%d", &a);}
while (a<0 || a>4);
printf("\nThe value of index %d is %d\n", a, rate[a]);
return 0;}

You might also like