Finding largest and smallest element in an array
Suppose we have some elements which is stored as the element of the array. Here we want to find out the largest and smallest element which is present as the element of the array.
Let array elements are given below:
a[0] a[1] a[2] a[3] a[4] a[5]
10 12 11 2 14 15
Largest=15
Smallest=2
Write a program to find the largest and smallest element in an array.
#include<stdio.h>
main( )
{
int a[10];
int j, n, max, min, sum;
sum=0;
printf("How many numbers are in the array:\n");
scanf("%d",&n);
printf("Enter ten elements in the array :");
for(j=0;j<=n-1;++j)
{
scanf("%d", &a[j]);
}
printf(" Contents of the array elements\n");
for(j=0; j<=n-1;++j)
{
printf("%d", a[j]);
}
printf("\n");
min=max=a[0];
for(j=0;j<=n-1;++j)
{
if (max<a[j])
max=a[j];
if (min>a[j])
min=a[j];
}
for(j=0;j<=n-1;++j)
{
sum+=a[j];
printf("Min Value=%d",min);
printf("Max Value=%d",max);
printf("Sum=%d",sum);
}
getch();
}