You are on page 1of 20

Array

Programming in C
Dr. Nivedita Palia
Arrays: Definition

An array is a fixed size sequenced collection of elements of the


same data type in continuous memory.

It is a derived data type. (1 D Array)

Subscripted variable

index of first element is zero(0). Index is always an integer.

Array Declaration
int a[5]; //valid
int a[]={10,30,55,33,76}; //valid
float a[]={10,30,55,33,76}; //valid
float a[]; //invalid
#include <stdio.h>

/* array */

int main() {
int a[5],i;
printf("enter the elements of an array");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
for(i=0;i<5;i++)
printf("%d\n",a[i]);
return 0;
}
//WAP to input marks of 5 students and find the average of class
#include <stdio.h>

/* array */

int main() {
int A[10],i,avg,sum=0,n;
printf("enter number of students\n");
scanf("%d",&n);
printf("enter the value of arrays elements");
for(i=1;i<=n;i++)
scanf("%d",&A[i]);
for(i=1;i<=n;i++)
sum=sum+A[i];
avg=sum/n;
printf("%d",avg);

return 0;
}
2- D Arrays
• Arrays with rows and columns both.
• Used for tabular or matrix form of data
visualization.
• int m[2][2] ={34,66,454,88};
• int m[2][2]= { {34,66},{454,88} };
• int m[][2]={{34,66},{454,88} };
Memory Representation of 2-D Array
#include<stdio.h>
//Matrix Transpose
int main()
{int a[5][5],b[5][5],i,j,m,n;
printf("enter the number of rows and column of the first array A");
scanf("%d%d",&m,&n);
printf("Enter the element of first array");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf(" Print an array A\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
//Print transpose matrix
for(i=0;i<n;i++)
for(j=0;j<m;j++)
b[i][j]=a[j][i];
printf(" Print an array B\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%d\t",b[i][j]);
printf("\n");
}
return(0);
}
Matrix Addition
// Matrix Addition
#include<stdio.h>
int main()
{int a[5][5],b[5][5],c[5][5],i,j,m,n,o,p;
printf("enter the number of rows and column of the first array A");
scanf("%d%d",&m,&n);
printf("Enter the element of first array");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf(" Print an array A/n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
printf("enter the number of rows and column of the second array
B");
scanf("%d%d",&o,&p);
printf("Enter the element of second array");
for(i=0;i<o;i++)
for(j=0;j<p;j++)
scanf("%d",&b[i][j]);
printf(" Print an array B\n");
for(i=0;i<o;i++)
{
for(j=0;j<p;j++)
printf("%d\t",b[i][j]);
printf("\n");
}
if((m==o)&&(n==p))
{
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
}
else
printf("matrix addition is not possible\n");
printf("Addition matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
return(0);
}
Matrix Multiplication
#include<stdio.h>
int main()
{int a[5][5],b[5][5],c[5][5],i,j,m,n,o,p,k;
printf("enter the number of rows and column of the first array A");
scanf("%d%d",&m,&n);
printf("Enter the element of first array");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf(" Print an array A\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
printf("enter the number of rows and column of the
second array B");
scanf("%d%d",&o,&p);
printf("Enter the element of second array");
for(i=0;i<o;i++)
for(j=0;j<p;j++)
scanf("%d",&b[i][j]);
printf(" Print an array B\n");
for(i=0;i<o;i++)
{
for(j=0;j<p;j++)
printf("%d\t",b[i][j]);
printf("\n");
}
//Matrix Multiplication
if(n==o)
{
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}}}
else
printf("matrix multiplication is not possible\n");
printf("Multiplication matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
return(0);
}
END

You might also like