Finding the smallest and the largest element in an array

#include <stdio.h>
void main()
{
    int i;
    int a[]={2,3,6,12,15,18};
    int n = sizeof(a)/sizeof(a[0]);
         int small = a[0],max=a[0];
         for(i=0;i<n;i++)
        {
             if(a[i]>max)
             max = a[i];
             if(a[i]<small)
            small = a[i];
        }
        printf("small is %d and large is %d",small,max);
}

Comments

Popular posts from this blog

finding how many times a sorted array is rotated