/*****************************************************************/ // File : biggest.c // Purpose : To find the largest and second largest element in a list // Author : Nanda Kishor K N // Mail Id : knnkishor@yahoo.com, nandakishorkn@rediffmail.com // Website : www.c4swimmers.esmartguy.com ( WEB MASTER ) // Group : c4swimmers@yahoogroups.com ( GROUP OWNER ) /*****************************************************************/ #include /* Name : main() Purpose : This is the main function that accepts 'N' number of elements and computes the largest and second largest element in a list. This does not call any other function. Parameter : None */ int main() { // Variable Declarations & Initialization // Naming conventions are used like all integer variables should begin with 'i' int iList[50], iIdx, iN, iBig, iSec; printf("\nHow many numbers in the list ? "); scanf("%d",&iN); // Accept 'N' number of elements printf("\nEnter the elements:\n"); for(iIdx = 0; iIdx < iN; iIdx++) { scanf("%d",&iList[iIdx]); } // Computing the largest and second largest element in a list iBig = iList[0]; iSec = 0; for(iIdx = 1; iIdx < iN; iIdx++) { if (iList[iIdx] > iBig) { iSec = iBig; // Second biggest iBig = iList[iIdx]; // Biggest } else if (iList[iIdx] > iSec) iSec = iList[iIdx]; // Second biggest } // Display the 'N' number of elements in a list printf("\n\nThe elements in the list are: "); for(iIdx = 0; iIdx < iN; iIdx++) { printf("\n%d",iList[iIdx]); } // Display the result or print the biggest and second biggest element printf("\n\nThe biggest element in the list is: %d",iBig); printf("\nThe second biggest element in the list is: %d",iSec); return 0; } // End of main() program