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

Program To Search An Element in An Array

The document contains code snippets for various array operations in C including searching and inserting elements in an array, deleting elements, merging two sorted arrays, adding two dimensional arrays, finding the sum of diagonal elements in a 2D array, and displaying the upper and lower halves of a 2D array.

Uploaded by

Karan Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Program To Search An Element in An Array

The document contains code snippets for various array operations in C including searching and inserting elements in an array, deleting elements, merging two sorted arrays, adding two dimensional arrays, finding the sum of diagonal elements in a 2D array, and displaying the upper and lower halves of a 2D array.

Uploaded by

Karan Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Program to search an element in an

array
Void search(int a[],int size)
{
int target,I,flag=0;
printf("Enter the number you are deleting);
scanf(%d, &target);
for ( i = 0; i < size; i++ )
{
if ( a[i] == target )
Flag=1;
break;
}
If(flag==1)
Printf(element found at %d position,i);
Else
Printf(element not found);
}

Program to insert an element in an
sorted array
void Insertion(int T[ ],int &size)
{
int target,k=0,i;
printf("Enter the number you are inserting);
scanf(%d,&target);

while((k< size) && (T[k] < target))
k++;

for(int i = size -1; i>=k; i --)
{
T[i + 1] = T[i];
}

T[k] = target;
size = size + 1;
}
Deleting an element from the sorted
array
void deletion(int T[ ],int &size)
{
int target,i;
printf("Enter the number you are deleting);
scanf(%d, &target);
for ( i = 0; i < size; i++ )
{
if ( a[i] == target )
break;
}
while ( i < size )
a[i ] = a[i+1];
size=size-1;
}

Merging of arrays
Void main()
{ int arr1[20],arr2[20],arr3[40],i=0,j=0,k=0, max1,max2;
printf("Enter the number of elements in list1 : ");
scanf("%d",&max1);
printf("Take the elements in sorted order :\n");
for(i=0;i<max1;i++)
{ scanf("%d",&arr1[i]); }
printf("Enter the number of elements in list2 : ");
scanf("%d",&max2);
printf("Take the elements in sorted order :\n");
for(i=0;i<max2;i++)
{ scanf("%d",&arr2[i]) }
while( (i < max1) && (j < max2) )
{
if( arr1[i] < arr2[j] )
arr3[k++]=arr1[i++];
else
arr3[k++]=arr2[j++];
}
while( i < max1 )
arr3[k++]=arr1[i++];
while( j < max2 )
arr3[k++]=arr2[j++];
}


Addition of two dimensional arrays
void main(void)
{ int i,j, mat1[3][3], mat2[3][3], sum[3][3];
printf(enter elements for mat1:);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(%d,&mat1*i][j]);
printf("enter elements for mat2:);
for(i=0;i<3i++)
for(j=0;j<3;j++)
scanf(%d,&mat2*i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
sum[i][j]=mat1[i][j]+mat2[i][j];
printf(sum of two matrices is \n);
for(i=0;i<3;i++)
{for(j=0;j<3;j++)
printf(%d,sum[i][j]);
Printf(\n);}
}

Sum of diagonal elements
Void main()
{
int i,j, mat[3][3], suml=0,sumr=0;
printf(enter elements for mat:);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(%d,&mat[i][j]);
for(i=0;i<3;i++)
suml=suml+mat[i][i];
for(i=0;i<3;i++)
sumr=sumr+mat[i][3-i-1];
printf(the sum of left diagonal is %d,suml);
printf(the sum of right diagonal is %d,sumr);
}

Display the upper half of a 2-d array
Void main()
{
int i,j, mat[3][3];
printf(enter elements for mat:);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(%d,&mat[i][j]);
printf(the upper half of the matrix is\n);
for(i=0;i<3;i++)
{for(j=0;j<3;j++)
if(i<=j)
printf(%d,mat[i][j]);
printf(\n);
}

Display the lower half of a 2-d array
Void main()
{
int i,j, mat[3][3];
printf(enter elements for mat:);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(%d,&mat[i][j]);
printf(the lower half of the matrix is\n);
for(i=0;i<3;i++)
{for(j=0;j<3;j++)
if(i>=j)
printf(%d,mat[i][j]);
printf(\n);
}

You might also like