Array in c part-2 for beginner
Sorting
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],j,i,temp;
clrscr();
for(i=0;i<5;i++)
scanf("%d",&a[i]);
printf("\nassending order");
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("\n%d",a[i]);
}
getch();
}
Sorting descending
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,j,temp;
clrscr();
for(i=0;i<5;i++)
scanf("%d",&a[i]);
printf("\ndescending order");
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("\n%d",a[i]);
}
getch();
}
Reversal Of element in array
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
clrscr();
for (i=0;i<5;i++)
{
printf("enter arrey");
scanf("%d",&a[i]);
}
for (i=4;i>=0;i--)
{
printf("\n a[%d]=%d",i,a[i]);
}
getch();
}
MultiDimensional Array
#include<stdio.h>
#include<conio.h>
void main ()
{
int a[3][3],b[3][3],c[3][3],i,j,k,sum=0;
clrscr ();
printf ("enter the elements of a:\n");
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
scanf ("%d",&a[i][j]);
}
}
printf ("\nenter the elements of b:\n");
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
scanf ("%d",&b[i][j]);
}
}
printf ("\nMultiplication matrix is :\n");
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{ sum=0;
for (k=0;k<3;k++)
{
sum+=a[i][k]*b[k][j];
c[i][j]=sum;
}
printf ("%d\t",c[i][j]);
}
printf ("\n");
}
getch ();
}
Comments
Post a Comment