Find Biggest In An Array, Without Sorting It: C


First we have to assume that a[0] is biggest. Now store the value of a[0] inside a variable called big. Now compare value of big with other values in the array. If big is less than any other value, then store the bigger value inside variable big.

Video Tutorial: Biggest In An Array, Without Sorting It: C


[youtube https://www.youtube.com/watch?v=tKaImGrxgKo]

YouTube Link: https://www.youtube.com/watch?v=tKaImGrxgKo [Watch the Video In Full Screen.]



Note that, the previous value inside the variable big is discarded.

Full source code

#include < stdio.h >
#include < conio.h >

void main()
{
int big, a[20], N, pos, i;
clrscr();

printf("Enter the array size\n");
scanf("%d", &N);

printf("Enter %d elements\n", N);
for(i = 0; i < N; i++) 
           scanf("%d", &a[i]);

         big = a[0];
         pos = 0;

         for(i = 1;  i < N; i++)
          if(a[i] > big)
  {
    big = a[i];
    pos = i+1;
    }

 printf("Big is %d and its position is %d", big, pos);

 getch();
}

Output
Enter the array size
5
Enter 5 elements
2
0
100
108
55
Big is 108 and its position is 4

Leave a Reply

Your email address will not be published. Required fields are marked *