| |
ARRAYS in 'C' language
When we want to stores more number of values of the same data type, we have to declare those many variables. This process is tedious and time consuming. To overcome this, we use "Arrays".
An Array is a group of elements consisting of similar type of data. All the element values are identified by a single variable name along with the index. These array elements are allotted memory locations in continuous manner, and so, it is easy to retrieve them. The use of an array reduces the usage of so many variables in the program.
Arrays are of 2 types.
(a) Single Dimensional arrays and
(b) Multi Dimensional arrays.
SINGLE DIMENSIONAL ARRAYS:- In this type of array, only one dimension i.e., number of elements that can be stored in an array are given. Each element is identified by its array name and its subscript or index.
The index of an array starts from 0 and ranges till size-1.
Syntax: data type array name[size];
Data type is the type of data we want to stores in an array and size indicates the no. of values that can be stored in an array.
Accepting the values into an array:-
i) You can store the values into an array while declaring the array only.
Eg: int a[5]={23,5,6,7,10};.
ii) You can also assign the values after declaring a variable.
Eg: int a[5]; a[0]=5; a[1]=12;a[2]=4;a[3]=10;a[4]=55;.
iii) You can also accept the values into an array during runtime also by using scanf. In this we give the address of each and every element in which the values should be stored. This makes the statement too long.
iv) The above problem can be overcome by using loops in the program while accepting and displaying.
Eg:
/*to accept array values and display them*/
main()
{
int a[5], i;
clrscr();
printf("\nEnter the values :");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("\nThe values are :");
for(i=0;i<5;i++)
{
printf("\n%d",a[i]);
}
getch();
}
We can do number of programs on single dimension arrays like finding the sum and average of the values in an array, searching a particular value, finding the max and min values, sorting the arrays values etc.,
SORTING: Sorting means arranging in an order. In an array, we can sort the values either in ascending order or in descending order. There are no. of techniques available to sort the arrays like bubble sort, selection sort, quick sort, merge sort, insertion sort etc., We learn here bubble sorting and selection sorting.
Bubble sort:- In this type of sorting, the value in the current position and the next value are compared and the values are interchanged depending on the condition. After interchanging the values, the location is incremented and now the next value position becomes the current position and this is again checked with the next value following it. This goes on till all the values are sorted. In this type, we will get first the last value of the location, whether it is in ascending or in descending order.
For Eg: a[0] -a[1] , a[1] - a[2] , a[2]- a[3] and so on.
Eg: /*bubble sort*/
main()
{
int n,I,j,temp,a[20];
clrscr();
printf("\n Enter the no. of elements :");
scanf("%d",&n);
printf("\nEnter the values :");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nSorted values are :");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}
Selection sort:- This is another technique of sorting in which the first element is compared with the rest of the elements and places the minimum value first in case of ascending order. So after completing first inner loop completely minimum value is placed in the first element of an array. Now in the second inner loop, the second element value is compared with the rest of the values and this procedure goes on till all the values are compared and sorted.
Eg: /*selection sort*/
main()
{
int n,I,j,temp,a[20];
clrscr();
printf("\n Enter the no. of elements :");
scanf("%d",&n);
printf("\nEnter the values :");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
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("\nSorted values are :");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}
MULTI DIMENSIONAL ARRAYS:- Arrays having more than one dimension are called multi dimensional arrays. There two dimensional arrays and three dimensional arrays available.
Double dimensional arrays:- When we want values to be stored in the form of a row and column i.e., like a matrix form, we have to use this 2D arrays. In this array, we have to specify the no. of rows and columns required and all the elements are allotted memory in a continuous locations. The elements are identified as for eg: if it is a[2][3] matrix, a[0][0], a[0][1], a[0][2], a[1][0], a[1][1] and a[1][2].
Assigning values to a 2D arrays:
(a) int a[2][3]={ 10,10,23,56}
(b) int a[3][2]={{3,4},{6,7},{10,12}};
You can also store values in a 2D array during run time using loops and scanf. Since, it is a 2d array, we have to use 2 for loops.
Eg: /*multiplication of matrices*/
main()
{
int a[30][30],b[30][30],i,j,m,n,p,q,k,c[30][30];
clrscr();
printf("Enter dimensions of a\n");
scanf("%d%d",&m,&n);
printf("\nenter dimensions of b");
scanf("%d%d",&p,&q);
printf("Enter %d elements for a",m*n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n Enter %d elementsfor b",p*q);
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
if(n==p)
{
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<q;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
printf("\nThe product of 2 matrices is\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)printf(" %d",c[i][j]);
printf("\n");
}
}
}
else
printf("\nCannot multiply");
getch();
}
Please send comments to vgdarur.javafive@blogger.com
|