Array Concept Using C: by Ms. P.Jayalakshmi, Ap/Cse Ms. K.Ramya, AP/ CSE
Array Concept Using C: by Ms. P.Jayalakshmi, Ap/Cse Ms. K.Ramya, AP/ CSE
By
1
INTRODUCTION TO ARRAYS
Array is the collection of similar data types stored in contiguous memory location.
Array might be belonging to any of the data types
Array size must be a constant value.
Array variable can store more than one value at a time.
Symbolic constant can also be used to specify the size of the array (#define SIZE 10;
Syntax:
data-type arr_name[array_size];
Array declaration, Example
initialization and
accessing
Two dimensional array
Example:
int marks[10];
Program:
#include <stdio.h>
void main()
{
int i,j,x,k=0,n,a[20],z=0,b[20];
float sum=0, t,mean,medn,mod;
printf(“\n Enter the no. of elements (Max 20)\n”);
scanf(“%d”,&n);
printf(“Enter the elements\n”);
for(i=0;i<n;i++)
{
scan (“%d”,&a[i]);
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
printf(“Sum: %f”,sum);
mean=sum/n;
}
for(i=0;i<n;i++)
{
for (j=i+1;j<n;j++)
{
if (a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
if (n%2==0)
medn=(a[n/2]+a[(n/2)-1])/2;
else
medn=a[n/2];
for(i=0;i<n;i++) //check
{
for ( j=0; j<i; j++ )
{
if (a[i]==a[j])
b[i]++;
}
}
for(i=0;i<n;i++)
{
if (b[i]>z)
z=b[i];
}
printf(“Mean :%f \n Median : %f \n Mean : “, mean, medn);
for ( i=0; i<n; i++)
{
if (b[i]==z)
printf(“%d\t”, a[i]);
}
}
TWO DIMENSIONAL ARRAYS
Example:
int a[3][4];
EXAMPLE PROGRAM:
#include<stdio.h>
#include<conio.h>
int main()
{
int m,n;
int a[20][20],b[20][20];
int i,j;
int sum[20][20],sub[20][20];
int opt,tr[20][20],opt1,ch,e,f;
printf("Note : For Addition or Subtraction , no. of rows and columns
should be same and for transpose of matrices , your first matrices entered
should be the desired matrices .\n");
printf("Enter the no. of rows: ");
scanf("%d",&m);
printf("Enter the no. of columns: ");
scanf("%d",&n);
printf("Enter the Data Elements of first matrices\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the no. of rows for second matrices: ");
scanf("%d",&e);
printf("Enter the no. of columns: ");
scanf("%d",&f);
printf("Enter the Data Elements of second matrices\n");
for(i=0;i<e;i++)
{
for(j=0;j<f;j++)
{
scanf("%d",&b[i][j]);
}
}
do
{
if(m==e&&n==f)
{
printf("Enter 1 for addition or subtraction of matrices\n");
if(n==e)
{
printf("Enter 2 for multiplication of matrices\n");}
printf("Enter 3 for transpose of first matrices\n");
}
else if(m!=n&&n==e)
{
printf("Enter 2 for multiplication of matrices\n");
printf("Enter 3 for transpose of first matrices\n");
}
else
{
printf("Enter 3 for transpose of first matrices\n");
}
scanf("%d",&ch);
switch(ch)
{
case 1 :
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum[i][j]=a[i][j]+b[i][j];
sub[i][j]=a[i][j]-b[i][j];
}
}
printf("Enter 1 for Addition or 2 for Subtraction: ");
scanf("%d",&opt);
switch(opt)
{
case 1 :
printf("The resultant matrices is :\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%3d",sum[i][j]);
}
printf("\n");
}
break;
case 2 :
printf("The resultant matrices is :\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%3d",sub[i][j]);
}
printf("\n");
}
}
break;
case 2 :
printf("The resultant matrices is : \n");
int k;
for(i=0;i<m;i++)
{
for(j=0;j<f;j++)
{ sum[i][j]=0;
for(k=0;k<m;k++)
{
sum[i][j]+=a[i][k]*b[k][j];
}
printf("%d\t",sum[i][j]);
}
printf("\n");
}
break;
case 3 :
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
tr[j][i]=a[i][j];
}
}
printf("The resultant matrices is :\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%3d",tr[i][j]);
}
printf("\n");
}
break; }}
while(ch>0);
getch();
}