CS 101 Lecture 6 PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Let Us C

1
CS 101
Computer Programming
Partha Pratim Paul
Lecturer, Dept. of ECE
Presidency University

2
Chapter 4

Arrays

3
What are Arrays
The C language provides a capability that enables the user to design a set of similar data
types, called array.

For understanding the arrays properly, let us consider the following program:

main( )
{ int x ;
x=5;
x = 10 ;
printf ( "\nx = %d", x ) ;
}

this program will print the value of x as 10.

Why so? Because when a value 10 is assigned to x, the earlier value of x=5, is lost. Thus,
ordinary variables are capable of holding only one value at a time. However, there are
situations in which we would want to store more than one value at a time in a single
variable.

4
What are Arrays
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.

5
What are Arrays
Definition of an array—

An array is a collective name given to a group of ‘similar quantities’.

Thus, 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.

6
A Simple Program Using Array
write a program to find average marks obtained by a class of 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 ( "\nEnter 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 ( "\nAverage marks = %d", avg ) ;
}

7
A Simple Program Using Array
Array Declaration

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 last program, the variable declared was: int marks[30] ;

Here, int specifies the type of the variable. The number 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, let us see how individual elements in the array can be referred. This is
done with subscript, the number in the brackets following the array name. This number
specifies the element’s position in the array. All the array elements are numbered, starting
with 0. Thus, marks[2] is not the second element of the array, but the third. In our program
we are using the variable i as a subscript 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. This ability to use variables as subscripts is what makes arrays so useful.

You might also like