Find Biggest In An Array, Without Sorting It: C

Advertisement:

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


You need to a flashplayer enabled browser to view this YouTube video

YouTube Link: http://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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 
#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();
}
 
</n>

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

Advertisement:

Enter your email address:

RSS Feed




Leave a Reply