Delete maximum element in Array Data Structure by R4R Team

Here we want to delete the maximum element in the given array:

Example-
Array is [2,9,3,5,1]
After deletion of max
Array is [2,3,5,1]


program-

#include< stdio.h>
int main()
{
int a[20],n,i,location,max;
printf("Enter number of elements in array\n");
scanf("%d",&n);
printf("Enter %d number in array",n);
for(i=0;i< n;i++)
scanf("%d",&a[i]);
printf("Array is\n");
for(i=0;i< n;i++)
printf("%d ",a[i]);
//find maximum number index value
max=a[0];
for(i=0;i< n;i++){
if(a[i]>max)
{
max=a[i];
location=i;
}
}
for(i=location;i< n-1;i++)
a[i]=a[i+1];
printf("Array after deletion of maximum number is \n");
for(i=0;i< n-1;i++)
printf("%d ",a[i]);
}


output-

Enter number of elements in array
6
Enter 6 number in array
1 6 3 4 1 2
Array is
1 6 3 4 1 2
After deletion of maximum number is
1 3 4 1 2

-In this program, we take an input in array, and try to find maximum element first.
-When we have a max element then we proceed to delete that number.
-Here we store the index value of the max element in variable 'location'.
-Then we just traverse and delete that index value.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!