Here is a small program that will find the largest and second largest value in an array.
The relevant bits to take from this is that it scans through the array of elements one at a time and compares it with its current "largest value". If the value in the array is larger then it stores that as the new largest value. Before storing that it copies the old largest value as the new "second largest value".
[Edit: I have updated the algorithm to handle the case where the second largest value would never be set. For example with an input set of {2,1}. Additionally I updated it to handle negative numbers.]
#include <stdio.h>
#include <limits.h>
int myArray [] = {2,5,9,1,7,13,3,11};
#define NumElements( array ) sizeof(array)/sizeof(array[0])
int main(int argc, const char * argv[])
{
int i;
int largest = INT_MIN;
int secondLargest = INT_MIN;
for( i=0; i<NumElements(myArray); i++ )
{
if( myArray[i] > largest )
{
secondLargest = largest;
largest = myArray[i];
}
else if( myArray[i] > secondLargest )
{
secondLargest = myArray[i];
}
}
printf( "largest: %d\n", largest );
printf( "second largest: %d\n", secondLargest );
return 0;
}