Professional Documents
Culture Documents
Array Examples
Array Examples
Array Examples
// TRANSPOSE OF MATRIX
#include<stdio.h>
int main()
{
int a[3][3],t[3][3],i,j;
for(i=0;i<=2;i++) // 00 01 02
{ // 10 11 12
for(j=0;j<=2;j++) // 20 21 22
{
t[j][i] = a[i][j] ;
}
}
printf("\n Elements of array a[3][3] is : \n ");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\t %d",a[i][j]);
}
printf("\n");
}
printf("\n Elements of array t[3][3] is : \n ");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\t %d",t[i][j]);
}
printf("\n");
}
return 0;
}
// ADDITION OF MATRIX
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],sum[10][10],r1,c1,r2,c2,i,j;
printf("\n Enter row and col for matrix a :");
scanf("%d%d",&r1,&c1);
printf("\n Enter row and col for matrix b :");
scanf("%d%d",&r2,&c2);
if(r1==r2 && c1==c2)
{
printf("\n Enter any Elements for array a[%d][%d] : ",r1,c1);
for(i=0;i<=r1-1;i++)
{
for(j=0;j<=c1-1;j++)
{ scanf("%d",&a[i][j]); }
}
printf("\n Enter any Elements for array b[%d][%d] : ",r2,c2);
for(i=0;i<=r2-1;i++)
{ for(j=0;j<=c2-1;j++)
{ scanf("%d",&b[i][j]); }
}
// addition
for(i=0;i<=r1-1;i++) // 00 01 02
{ // 10 11 12
for(j=0;j<=c1-1;j++) // 20 21 22
{ sum[i][j] = a[i][j] + b[i][j];}
}
printf("\n Elements of array sum[%d][%d] is : \n ",r1,c1);
for(i=0;i<=r1-1;i++)
{ for(j=0;j<=c1-1;j++)
{ printf("\t %d",sum[i][j]); }
printf("\n");
}
}
else
{ printf("\n Matrix Addition is not possible :"); }
return 0;
}
//Enter any elements in array & WAP for accept any no & position from
// user and insert element in array entered position by user
#include<stdio.h>
int main()
{ int a[100],i,dim,n,pos;
printf("Enter any Dimention :");
scanf("%d",&dim);
printf("Enter any %d elements : ",dim);
for(i=0;i<=dim-1;i++)
{scanf("%d",&a[i]);
}
printf("All elements of array is \n ");
for(i=0;i<=dim-1;i++)
{printf("\n a[%d] = %d store in address %ld",i,a[i],&a[i]) ;
}
printf("\n Enter any no & position for insert element in array :");
scanf("%d%d",&n,&pos);
for(i=dim-1;i>=pos;i--)
{ a[i+1] = a[i];
}
a[pos] = n;
dim++;
printf("All elements of array is \n ");
for(i=0;i<=dim-1;i++)
{printf("\n a[%d] = %d store in address %ld",i,a[i],&a[i]) ;
}
return 0;
}
//Enter any elements in array & WAP for delete element from array
#include<stdio.h>
int main()
{ int a[100],i,dim,pos;
printf("Enter any Dimention :");
scanf("%d",&dim);
printf("Enter any %d elements : ",dim);
for(i=0;i<=dim-1;i++)
{ scanf("%d",&a[i]); }
printf("All elements of array is \n ");
for(i=0;i<=dim-1;i++)
{ printf("\n %d id store in a[%d] and address is %ld",a[i],i,&a[i]) ;
}
printf("\n Enter any position for delete element from arary :");
scanf("%d",&pos);
for(i=pos;i<=dim-2;i++)
{ a[i] = a[i+1];
} dim--;
printf("All elements of array is \n ");
for(i=0;i<=dim-1;i++)
{ printf("\n %d id store in a[%d] and address is %ld",a[i],i,&a[i]) ;
}
}
//Enter any 5 elements in array & WAP for accept any no from user
// & search eneterd element is present in array or not LINEAR SEARCH
#include<stdio.h>
int main()
{
int a[5],i,dim,num,flag=0,pos;
printf("\n Enter any dimention :");
scanf("%d",&dim);
printf("Enter any %d elements : ",dim);
for(i=0;i<=dim-1;i++)
{printf("\n Enter %d Element in Array ",i+1);
scanf("%d",&a[i]);
}
printf("\n Enter any number for search :");
scanf("%d",&num);
for(i=0;i<=dim-1;i++)
{ if(num == a[i])
{ flag = 1;
pos = i+1;
break;
}
}
if(flag == 1)
{ printf("\n %d is present in array at %d position :",num,pos); }
else
{ printf("\n %d is not present in array :",num); }
return 0;
}
//Enter any elements in array & WAP for sort elements using Selection sort
#include<stdio.h>
int main()
{ int a[100],i,j,temp,dim;
printf("Enter any dimention : ");
scanf("%d",&dim);
printf("Enter any %d elements : ",dim);
for(i=0;i<=dim-1;i++)
{ scanf("%d",&a[i]);
}
printf("Before Selection Sort of array is \n ");
for(i=0;i<=dim-1;i++)
{ printf("\n %d ",a[i]);
}
/*Selection Sort */
for(i=0;i<=dim-1;i++)
{for(j=i+1;j<=dim-1;j++)
{ if(a[i]>a[j])
{ temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("After Selection Sort of array is \n ");
for(i=0;i<=dim-1;i++)
{
printf("\n %d ",a[i]);
}
return 0;
}
//Enter any elements in array & WAP for sort elements using bubble sort
#include<stdio.h>
int main()
{
int a[100],i,j,temp,dim;
printf("Enter any dimention");
scanf("%d",&dim);
printf("Enter any %d elements : ",dim);
for(i=0;i<=dim-1;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=dim-1;i++)
{
for(j=0;j<=dim-2;j++)
{
if ( a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("After Bubble Sort of array is \n ");
for(i=0;i<=dim-1;i++)
{
printf("\n %d ",a[i]);
}
return 0;
}
//Enter any elements in array & WAP for sort elements using insertion sort
#include<stdio.h>
int main()
{
int a[100],dim,i,j,temp;
printf("Enter any Dimention of Array :");
scanf("%d",&dim);
/* Input */
printf("\n Enter any %d elements in array :",dim);
for(i=0;i<=dim-1;i++)
{
scanf("%d",&a[i]);
}
/* Insertion Sort */
for( i=1 ; i<=dim-1 ; i++)
{
for(j = i; j>=1 ; j--)
{
if(a[j] < a[j-1]) // 11[0] 22[1] 33[2] 44[3] 55[4]
{
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
}
/* Output */
printf("\n After Insertion Sort Method Array is :\n ");
for(i=0;i<=dim-1;i++)
{
printf("\n %d",a[i]);
}
return 0;
}
//Enter any 5 elements in array & WAP for display max element of array
#include<stdio.h>
int main()
{ int a[100],i,dim,max,pos=1;
printf("\n Enter any dimention :");
scanf("%d",&dim);
printf("Enter any %d elements : ",dim);
for(i=0;i<=dim-1;i++)
{printf("\n Enter %d Element in Array ",i+1);
scanf("%d",&a[i]);
}
max = a[0]; // 0 1 2 3 4
for(i=0;i<=dim-1;i++) // 5 7 9 4 6 max 9 pos =3
{ if (max < a[i])
{ max = a[i];
pos = i+1;
}
}
printf("\n %d is maximum element of array store in %d position ",max,pos) ;
return 0;
}
// Separate non zero element from array and store in another array
#include<stdio.h>
int main()
{ int a[100],t[100],dim1,dim2=0,i;
printf("\n Enter any dimention :");
scanf("%d",&dim1);
printf("Enter any %d elements : ",dim1);
for(i=0;i<=dim1-1;i++)
{ scanf("%d",&a[i]);
}
printf("\n All elements of array is \n ");
for(i=0;i<=dim1-1;i++)
{ printf("\n %d id store in a[%d] ",a[i],i) ;
}
for(i=0;i<=dim1-1;i++)
{if(a[i] != 0)
{ t[dim2] = a[i];
dim2++;
}
}
printf("\n All elements of array is \n ");
for(i=0;i<=dim2-1;i++)
{ printf("\n %d id store in t[%d] ",t[i],i) ;
}
return 0;
}