Array Examples

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

//ENTER ANY ELEMENTS FOR 2D array A[row][col] WAP FOR ACCEPT

//& DISPLAY ALL ELEMENTS OF ARRAY IN MATRIX FORM


#include<stdio.h>
int main()
{ int a[10][10],i,j,row,col;
printf("\n Enter row and col for matrix :");
scanf("%d%d",&row,&col);
printf("\n Enter any Elements for array a[%d][%d] :",row,col);
for(i=0;i<=row-1;i++) //row
{ for(j=0;j<=col-1;j++) //col
{ printf("\n Enter any value for a[%d][%d]",i,j);
scanf("%d",&a[i][j]); }
}
printf("\n Elements of array a[%d][%d] is : \n ",row,col);
for(i=0;i<=row-1;i++)
{ for(j=0;j<=col-1;j++)
{ printf("\t %d",a[i][j]); }
printf("\n");
} return 0;
}

// TRANSPOSE OF MATRIX
#include<stdio.h>
int main()
{
int a[3][3],t[3][3],i,j;

printf("\n Enter any Elements for array a[3][3] : ");


for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&a[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;
}

// multiplication OF TWO MATRIX


#include<stdio.h>
int main()
{ int a[10][10],b[10][10],res[10][10],i,j,k;
int r1,c1,r2,c2;
printf("Enter row & col for matrix a : ");
scanf("%d%d",&r1,&c1);
printf("Enter row & col for matrix b : ");
scanf("%d%d",&r2,&c2);
if(c1 == r2)
{
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]);
}
}
for(i=0;i<=r1-1;i++)
{
for(j=0;j<=c2-1;j++)
{
res[i][j]= 0;
for(k=0;k<=c1-1;k++) // either value of c1 or r2
{
res[i][j] = res[i][j]+a[i][k] * b[k][j];
}
}
}
printf("\n Res of array res[%d][%d] is : \n ",r1,c2);
for(i=0;i<=r1-1;i++)
{
for(j=0;j<=c2-1;j++)
{
printf("\t %d",res[i][j]);
}
printf("\n");
}
}
else
{
printf("Multiplication is not possible");
}
return 0;
}

// ENTER ANY ELEMENTS FOR MATRIX 3 BY 3 WAP FOR TRANSPOSE


// WITHOUT USING ANOTHER MATRIX
int main()
{ int a[3][3],i,j,temp;
printf("\n Enter any Elements for array a[3][3] : ");
for(i=0;i<=2;i++)
{ for(j=0;j<=2;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n Elements Before Transpose 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");
}
for(i=0;i<=2;i++) //a // 1 2 3 1 4 7
{ // 4 5 6 2 5 8
for(j=0;j<=2;j++) // 7 8 9 369
{
if(i<j)
{
temp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = temp;
}
}
}
printf("\n Elements After Transpose 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");
}
return 0;
}
//Enter any elements in array & WAP for Reverse of all elements of array
#include<stdio.h>
int main()
{ int a[100],i,j,dim,temp;
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 Array Before Reverse ");
for(i=0;i<=dim-1;i++)
{printf("\n %d ",a[i]);
}
i= 0; // starting position
j = dim-1; // last posituon
while(i<j)
{ temp = a[i];
a[i] = a[j];
a[j] = temp;
i++; // update position to 0>1>2
j--; // update position dim-1->dim-2->dim-3
}
printf("\n Array After Reverse ");
for(i=0;i<=dim-1;i++)
{printf("\n %d ",a[i]);
}
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;
}

// BINARY SEARCH // ARRAY IS IN SORTING ORDER


#include<stdio.h>
int main() // LB MID UB
{ // 0 1 2 3 4 5 6 7 8 9
int a[10]= {11,13,14,15,17,19,20,21,23,24};
int LB,UB,MID,n,flag=0;
printf("\n Enter any number for search in array :");
scanf("%d",&n); // n 23
LB = 0;
UB = 9; // dim-1
while(LB<=UB)
{
MID=(LB+UB)/2; // mid = 8
if(a[MID] == n) // a[8] = 23 n = 14
{
flag = 1;
break;
}
else if(a[MID] < n)
{
LB = MID + 1;
}
else if(a[MID] > n)
{
UB = MID - 1;
}
}
if(flag ==1)
{
printf("\n %d is Present in array at %d position :",n,MID+1);
}
else
{
printf("\n %d is not Present in array :",n);
}
}

//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;
}

// Upper Triangular Matrix without using another matrix


#include<stdio.h>
#include<conio.h>
void main()
{ int a[3][3],temp,i,j;
clrscr();
printf("\n Enter any Elements for array a[3][3] : ");
for(i=0;i<=2;i++)
{ for(j=0;j<=2;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n Before convert upper triangular matrix 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");
} // 00 01 02
for(i=0;i<=2;i++) //a // 1 2 3 1 4 7
{ // 4 5 6 0 5 8
for(j=0;j<=2;j++) // 7 8 9 0 0 9
{
if(i>j)
{
a[i][j] = 0;
}
}
}
printf("\n After convert upper triangular matrix 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");
}
getch();
}

You might also like