ARRAY



What is Array?

Array is collection of similar type of data elements.








Displaying array
Input Output
#include<stdio.h>
main()
{
int a[5];
clrscr();
a[0]=50;
a[1]=45;
a[2]=67;
a[3]=34;
a[4]=21;
printf("%d\n%d\n%d\n%d\n%d\n",a[0],a[1],a[2],a[3],a[4]);
getch();
}





Displaying of an array (method 2)
Input Output
#include<stdio.h>
main()
{
int a[5]={10,20,30,40,50};
clrscr();
printf("\n%d\n%d\n%d\n%d\n%d\n",a[0],a[1],a[2],a[3],a[4]);
getch();
}





Displaying an array using For
Input Output
#include<stdio.h>
main()
{
int a[7]={12,23,34,45,56,67,78};
int i;
clrscr();
for(i=0;i<=6;i++)
{
printf("%d\n",a[i]);
}
getch();
}





Addition using an array and scanf
Input Output
#include<stdio.h>
main()
{
int a[5];
int i,b;
clrscr();
printf("Enter the values for a");
for(i=0;i<=4;i++)
{
scanf("%d",&a[i]);
}
b=a[1]+a[2]+a[3]+a[4]+a[0];
for(i=0;i<=4;i++)
{
printf("\n%d",a[i]);
}
printf("\n%d",b);
getch();
}






2-D Array
Input Output
#include<stdio.h>
main()
{
int a[2][4];
clrscr();
a[0][0]=10;
a[0][1]=20;
a[0][2]=30;
a[0][3]=40;
a[1][0]=11;
a[1][1]=21;
a[1][2]=31;
a[1][3]=41;
printf("%d\t%d\t%d\t%d\n",a[0][0],a[0][1],a[0][2],a[0][3]);
printf("%d\t%d\t%d\t%d",a[1][0],a[1][1],a[1][2],a[1][3]);
getch();
}






2-D Array
Input Output
#include<stdio.h>
main()
{
int a[3][3]={
{10,20,30},{11,21,31},{12,22,32}
};
clrscr();
printf("%d  %d  %d\n",a[0][0],a[0][1],a[0][2]);
printf("%d  %d  %d\n",a[1][0],a[1][1],a[1][2]);
printf("%d  %d  %d",a[2][0],a[2][1],a[2][2]);
}





Adding of 2-D Arrays
Input Output
#include<stdio.h>
main()
{
int a[2][2]={{1,2},{3,4}};
int b[2][2]={{5,6},{7,8}};
int c[2][2]={{9,10},{11,12}};
int d[2][2];
int i,j;
clrscr();
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
d[i][j]=a[i][j]+b[i][j]+c[i][j];
}
}
printf("\nTheir addition is\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",d[i][j]);
}
printf("\n");
}
getch();
}





Adding of 2-D Arrays using scanf
Input Output
#include<stdio.h>
main()
{
int i,j,r,c;
int a[5][5];
int b[5][5];
int d[5][5];
clrscr();
printf("enter the no of rows of the matrices");
scanf("%d",&r);
printf("\nenter the no of columns of the matrices");
scanf("%d",&c);
printf("\nenter the elments of matrix a:");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}}
printf("\nenter the elments of matrix b:");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
d[i][j]=a[i][j]+b[i][j];
}}
printf("Addition of the matrix a and b is as follows:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",d[i][j]);
}
printf("\n");
}
getch();
}






Multiplying of two matrix
Input Output
#include<stdio.h>
main()
{
int r1,c1,r2,c2,k,i,j;
int a[5][5],b[5][5],c[5][5];
clrscr();
printf("\nEnter the number of rows for the matrix a:-");
scanf("%d",&r1);
printf("\nEnter the number of columns for the matrix a:-");
scanf("%d",&c1);
printf("\nEnter the number of rows for the matrix b:-");
scanf("%d",&r2);
printf("\nEnter the number of columns for the matrix b:-");
scanf("%d",&c2);
if(c1==r2)
{
printf("\nEnter the elements of matrix a:-");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}}
printf("\nEnter the elements of matrix b:-");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&b[i][j]);
}}
for(i=0;i<r1;i++)
{
c[i][j]=0;
for(j=0;j<c2;j++)
{
for(k=0;<c2;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
else
{
printf("multiplication of such matrix is not possible");
}
getch();
}