Arrays in c language:
Definition
An array is an
aggregate data type that lets you access multiple variables through a single
name by use of an index.
An ordinary variable cannot store more than one value at a time. Consider a situation of storing the marks of students of a class consists of sixty students. we need 60 variables . so the program will become very complex.
The solution to the above situation is to declare a variable
that can store more than one value. In this situation we need 60 blocks of
memory. A variable that can store more than one value is called an array. In an array all the data should be of same
data type . All the data are stored in contiguous memory location.
Array declaration:
To store integer value of 5 data example is
int a[5];.
a is actually an user defined variable name . all the rules of
naming an ordinary variable also apply to an array variable name.
Assigning data to an array example:
int a[5]={5,6,4,3,7};
the data are actually stored as
a[0]=5
a[1]=6
a[2]=4
a[3]=3
a[4]=7
note array index starts with zero and upper bound
index is less than one of array length. In the above example, length of array
is 5.
Getting
input for an array example
for(i=0;i<5;i++)
{
scanf(“%d”,&a[i]);
}
Printing
output for an array example is
for(i=0;i<5;i++)
{
printf(“%d\t ”, a[i]);
}
sample program:
#include<stdio.h>
#include<conio.h>
void
main()
{
int
a[5],i;
clrscr();
/*
getting input */
printf(“enter
5 values of a”);
for(i=0;i<5;i++)
{
scanf(“%d”,&a[i]);
}
/*
printing output */
printf(“The
datas of array a are”);
for(i=0;i<5;i++)
{
printf(“%d\t
”, a[i]);
}
getch();
}
No comments:
Post a Comment