Array
Array is nothing but a collection of like data types. 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.
Array Declaration
Syntax:
data type variable name[dimension];
e.g.:
int a[10];
float c[5];
char name[40];
Accessing elements of an array
We can access array elements by using a subscript. This subscript variable
can take different values and hence can refer to the different elements in the array
in turn.
Points to remember.
1. An array is a collection of similar elements.
2. The first element in the array is numbered 0, so the last element is less then the
size of the array.
3. An array is also known as the subscripted variable.
4. Before using an array its type and dimension must be declared.
5. An array elements are always stored in adjacent memory location.
6. Array starts from the 0 th location.
Two types of array
1. Single dimensional array.
e.g.:
#include<stdio.h>
#include<conio.h>
main()
{
int mark[10],avg,n,sum=0,i;
clrscr();
printf("Enter how many nos:");
scanf("%d",&n);
for(i=0;i<n;i++) /* store datas in an array */
{
printf("Enter marks one by one:");
scanf("%d",&mark[i]);
}
for(i=0;i<n;i++)
sum=sum+mark[i]; /* read data from an array */
avg=sum/n;
printf("Average marks=%d",avg);
getch();
}
Array Initialization
1. If the array elements are not given any specific values, they are supposed to
contain garbage values.
2. If the array is initialized where it is declared, mentioning the dimension of the
array is optional.
3. If the array is initialized where it is declared, its storage class must be either
static or extern.
e.g.:
---
int num[6]={2,4,3,4};
int num[] = { 2,4,4,7,8};
2. Two dimensional array
A two dimensional array is also called as matrix.
Declaration
Syntax:
data type variable name[ row dimension] [ column dimension];
e.g.:
int a[3][3];
e.g.:
#include<stdio.h>
#include<conio.h>
main()
{
int stud[4][2];
int i.j;
clrscr();
for(i=0;i<=3;i++)
{
printf(“Enter roll no and marks:”);
scanf(“%d%d”,&stud[i][0],&stud[i][1]);
}
for(i=0;i<=3;i++)
printf(“%d\t%d\n”,stud[i][0],stud[i][1]);
getch();
}
The array elements are being stored and accessed row wise.
Initializing a 2 dimensional array
While initializing an array it is necessary to mention the second (column)
dimension, whereas the first dimension (row) is optional.
e.g.:
int student [4] [2] = {
{124,89},
{125,90},
{145,90}
};
Multidimensional Array
A three dimensional array can be thoght of as an array of arrays. In practice,
one rarely uses this array.
E.g.:
int stud [2] [2] [2] = {
{
{2,3},
{4,8},
{ 8,10}
},
{
{ 3,8},
{9,7},
{7,5}
}
};
Strings or Character Arrays
A string constant is a one-dimensional array of characters terminated by a
null.
E.g.:
char name [] = { ‘k’,’a’,’v’,’I’,’t’,h’,’a’,’\0’};
or
char name[10] = “Kavitha”;
eg:
----
#include<stdio.h>
#include<conio.h>
main()
{
char name[10]="Lavanya";
int i=0;
clrscr();
while(name[i]!='\0')
{
printf("%c",name[i]);
i=i+1;
}
getch();
}
Note
1. The length of the string should not exceed the dimension of the character arry.
2. scanf() is not capable of receiving multiword strings. So in that case we can use
gets () instead of scanf ().
Some important programs
Matrix addition
#include<stdio.h>
#include<conio.h>
main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("Enter the first matrix value one by one:\n");
for(i=0;i<3;i++)
{
printf("Enter the %d row value:",i+1);
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
}
printf("Enter the second matrix value one by one:\n");
for(i=0;i<3;i++)
{
printf("Enter the %d row value:",i+1);
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
}
clrscr();
printf("The Matrix Addition is below\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("\t%d",c[i][j]);
}
printf("\n");
}
getch();
}
Matrix multiplication
#include<stdio.h>
#include<conio.h>
main()
{
int a[3][3],b[3][3],c[3][3],i,j,k;
clrscr();
printf("Enter the first matrix value one by one:\n");
for(i=0;i<3;i++)
{
printf("Enter the %d row value:\n",i+1);
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
}
printf("Enter the second matrix value one by one:\n");
for(i=0;i<3;i++)
{
printf("Enter the %d row value:\n",i+1);
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
}
clrscr();
printf("The matrix multiplication is below\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
Program to find the Maximum of n numbers
#include<stdio.h>
#include<conio.h>
main()
{
int a[10],big,i,n;
clrscr();
printf("Enter no of values:");
scanf("%d",&n);
printf("Enter the values one by one:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
big=a[0];
for(i=1;i<n;i++)
{
if(big<a[i])
{
big=a[i];
}
}
printf("Biggest no is:\n");
printf("%d\n",big);
getch();
}
Program for ascending order
#include<stdio.h>
#include<conio.h>
main()
{
int a[10],temp,i,j,n;
clrscr();
printf("Enter the no of values:\n");
scanf("%d",&n);
printf("Enter the values:\n");
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("Ascending order values \n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}