Thursday 3 October 2013

Finding Maximum and Minimum element of an array using C

#include<stdio.h>
main()
{
int array[10],num,i,max,min,maxloc=1,minloc=1,size;
printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
printf("\nEnter the numbers of elements you want in this array\n");
scanf("%d",&size);
printf("\nEnter %d elements of this array\n",size);
for(i=0;i<size;i++)
scanf("%d",&array[i]);
max=array[0];
min=array[0];
for(i=1;i<size;i++)
{
if(array[i]>max)
{
max=array[i];
maxloc=i+1;
}
if(array[i]<min)
{
min=array[i];
minloc=i+1;
}
}
printf("\n%d is the greatest element in the Array at %d position\n",max,maxloc);
printf("\n%d is the smallest element in the Array at %d position\n",min,minloc);
}

No comments:

Post a Comment