Written by Kousik :
C provides some fundamental DATA types. They are int, float, char, double. They are very useful and
important in C language. But they have a limiting fact. The fact is that a
variable of these types can store only one value at a time. With variables of
those types we cannot handle a large volume of data of same type at a time. So
we need a powerful data type with which we can solve this problem. C supports
such a data type called ARRAYS.
Definition: An array is a well defined collection of elements of same data
type. In others words One can say that an array is grouping of some elements
that are of same data type. Some applications of arrays in C are given below:
§ To store age of some boys.
§ To store marks of subjects.
§ To store daily rainfall data.
And so on.
Declaration
of ARRAYS: ARRAY in C can be declared by following format
int
array[50]; , float temperature[7];
See carefully that an array is declared by
defining the data type first then the name and then the size of array.
Now a question should be come to reader’s
mind and that is how can we take input of many elements at a time by using
array? Answer of this question is very easy. This problem can be solved by
using for loop for taking input by using following syntax.
For
(i=0; i<n; i++)
Scanf
(“%d”, &arr[i]);
Where n is the size of the array and arr is
the name of array.
Now here a example is given to understand
ARRAYS and the way of using an ARRAY in C.
#include<stdio.h>
int main()
{
int a[25],i,n,sum=0,mean;
printf("Enter the number of array elements\n");
scanf("%d",&n);
printf("Enter the value of array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
Printf(“%d ”, a[i]);
}
return 0;
}
There are two kinds of array those can be
used in C language. One is ONE
DIMENSIONAL and the other one is TWO
DIMENSIONAL.
In this section we will discuss about only
one dimensional array.
INITIALIZATION
OF ONE DIMENSIONAL ARRAY: After declaring an array
its elements can be initialized. Otherwise elements of array are of ‘garbage’.
The following syntax can be used to initialize an array.
Data
type variablename[size]={values};
Example
: int student[3]={1,2,3};
Above example is called compile time
initialization. Another type of initialization can be used for initialize an
array. That is Run time initialization. For example take the following segment.
for(j=0;j<50;j++)
{
If(j<20)
A[i]=0.0;
Else
A[i]=1.0;
}
We
can also use a scanf function to initialize an array as we discuss earlier with
the answer of the question how to take input value of elements of an array.
The following example illustrates a sorting
program in ascending order as an important application of arrays in C science.
/* a program to sort an array */
# include<stdio.h>
int main()
{
int a[25],i,j,n,temp,item;
char c;
printf("Enter the number of array elements\n");
scanf("%d",&n);
printf("Enter the value of array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
/* sorting of array */
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("After sorting \n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}
0 comments:
Post a Comment