0% found this document useful (0 votes)
33 views

Arrays

#include main() int a ; int i,n; printf("Enter no.of elements: "); scanf("%d",&n); / Program to read value into the DDA and print them.

Uploaded by

vasuveeesam
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Arrays

#include main() int a ; int i,n; printf("Enter no.of elements: "); scanf("%d",&n); / Program to read value into the DDA and print them.

Uploaded by

vasuveeesam
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

/*CompileTime Initialization*/

#include<stdio.h>
main()
{
int a[10]={10,20,30,40};
int i;
printf("\nThe elements are\n");
for(i=0;i<4;i++)
printf("%d ",a[i]);

/*CompileTime Initialization*/
#include<stdio.h>
main()
{
int a[]={10,20,30,40};
int i;
printf("\nThe elements are\n");
for(i=0;i<4;i++)
printf("%d ",a[i]);

/*CompileTime Initialization*/
#include<stdio.h>
main()
{
int a[10];
int i;
a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
printf("\nThe elements are\n");
for(i=0;i<4;i++)
printf("%d ",a[i]);
}

/*Runtime Initialization*/
#include<stdio.h>
main()
{
int a[10];
int i,n;
printf("Enter no.of elements: "); scanf("%d",&n);

printf("Enter elements: ");


for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nThe elements are\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}

/*Program on Even numbers Count*/


#include<stdio.h>
main()
{
int a[10];
int i,n,ecount=0;
system("clear");
printf("Enter no.of elements: "); scanf("%d",&n);
printf("Enter elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nThe elements are\n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
if( a[i]%2==0 )
ecount++;
}
printf("\nEven numbers count = %d\n",ecount);
}

/*Program on Odd numbers Count*/


#include<stdio.h>
main()
{
int a[10];
int i,n,ocount=0;
system("clear");
printf("Enter no.of elements: "); scanf("%d",&n);
printf("Enter elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nThe elements are\n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
if( a[i]%2!=0 )
ocount++;
}
printf("\nEven numbers count = %d\n",ocount);
}

#include<stdio.h>
main()
{
int a[10];
int i,n,ocount=0,osum=0,ecount=0,esum=0;
system("clear");
printf("Enter no.of elements: "); scanf("%d",&n);
printf("Enter elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nThe elements are\n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
if( a[i]%2!=0 )
{
ocount++;
osum = osum+a[i];
}
else
{
ecount++;
esum = esum+a[i];
}
}
printf("\nOdd numbers count = %d\n",ocount);
printf("\nOdd numbers sum = %d\n",osum);
printf("\neven numbers count = %d\n",ecount);
printf("\neven numbers sum = %d\n",esum);
}

#include<stdio.h>

main()
{
int a[10];
int i,n,sum=0;
float avg;
system("clear");
printf("Enter no.of elements: "); scanf("%d",&n);
printf("Enter elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);

printf("\nThe elements are\n");


for(i=0;i<n;i++)
{
printf("%d ",a[i]);
sum = sum+a[i];
}
avg = (float)sum/n;
printf("\nsum = %d\n",sum);
printf("average = %f\n",avg);
}

/*Program to read value into the DDA & print them*/


#include<stdio.h>
main()
{
//int a[3][2]={10,20,30,40,50,60};
//int a[][2]={10,20,30,40,50,60};
int a[3][2]={{10,20},{30,40},{50,60}};
int i,j;
printf("The array elements are\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}

/*Program to read & print DDA elements using runtime initialization*/


#include<stdio.h>
main()
{
int i,j,m,n;
printf("Enter no. of rows & no. columns ");
scanf("%d %d",&m,&n);
int a[5][5];
printf("Enter the DDA elements: ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("The DDA elements are \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
}

/* Program for addition of two matrices*/


#include<stdio.h>
#define rows 10
#define cols 10
main()
{
int
a[rows][cols],b[rows][cols],c[rows][cols],i,j,m,n,p
,q;
system("clear");
/* Reading elements of a matrix */
printf("Enter size of a matrix: "); scanf("%d
%d",&m,&n);
printf("Enter a matrix elements\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf("Enter a[%d,%d] ele: ",i,j);
scanf("%d",&a[i][j]);
}
/* Reading elements of b matrix */
printf("Enter size of b matrix: "); scanf("%d
%d",&p,&q);
printf("Enter a matrix elements\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)

printf("Enter a[%d,%d] ele: ",i,j);


scanf("%d",&b[i][j]);

}
/* Addition code */
if(m==p && n==q)
{
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
/* Printing resultant matrix c */
printf("\n\nThe resultant matrix is
given as\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%d ",c[i][j]);
printf("\n");

}
}
else
printf("\nAddition is not possible");
}

/*Program to find biggest & smallest element in


the matrix and also find their positions*/
#include<stdio.h>
main()
{
int a[6][6],m,n,i,j,big,small;
int brp,srp,bcp,scp;
printf("Enter no. of rows and columns
");
scanf("%d %d",&m,&n);
printf("Enter the matrix elements ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("The matrix elements are \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
big=small=a[0][0];
brp=bcp=srp=scp=0;

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]>big)
{
big=a[i][j];
brp=i;bcp=j;
}
if(a[i][j]<small)
{
small=a[i][j];
srp=i;scp=j;
}
}
}
printf("The biggest element =
%d\n",big);
printf("The biggest element location at
a[%d,%d] = %d\n",brp,bcp,a[brp][bcp]);
printf("The smallest element =
%d\n",small);
printf("The smallest element location
at a[%d,%d] = %d\n",srp,scp,a[srp][scp]);
}

You might also like