ccplusplus.com
Learn C, C++ Concepts
Sunday, September 11, 2011
maximum and minimum number in an array
/****************************************************** * File : maximum-and-minimum-number-in-an-array.c * Author : Saurabh Gupta * Desc : find maximum and minimum number from an array * Source : http://saurabhgupta0527.blogspot.com/ * Created : PM 11:55 10 September 2011 * Note : change MAX_NUM_ELE to number of * values to search for *****************************************************/ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #define MAX_NUM_ELE 10 int main() { int i, nMaximumNumber, nMinimumNumber, max_index, min_index; int NumbersArray[MAX_NUM_ELE]; printf("\n\n\t Program for finding Maximum & Minimum number from an array"); printf("\n\n\t\t\t Enter any %d Numbers\n", MAX_NUM_ELE); for(i = 0; i < MAX_NUM_ELE; i++) { printf("\n\t\t\t Enter number %d = ",i); scanf("%d",&NumbersArray[i]); } nMaximumNumber=0; nMinimumNumber=NumbersArray[0]; for(i=0;i<MAX_NUM_ELE;i++) { if(NumbersArray[i]>nMaximumNumber) { nMaximumNumber=NumbersArray[i]; max_index=i; } if(NumbersArray[i]<=nMinimumNumber) { nMinimumNumber=NumbersArray[i]; min_index=i; } } printf("\n\n\t\t\t Maximum number is %d at index %d",nMaximumNumber,max_index); printf("\n\n\t\t\t Minimum number is %d at index %d",nMinimumNumber,min_index); printf ("\n"); return 0; } /* * OUTPUT * [sgupta@rhel54x64 c]$ gcc maximum-and-minimum-number-in-an-array.c -o maximum-and-minimum-number-in-an-array [sgupta@rhel54x64 c]$ ./maximum-and-minimum-number-in-an-array Program for finding Maximum & Minimum number from an array Enter any 10 Numbers Enter number 0 = 2 Enter number 1 = 1 Enter number 2 = 2 Enter number 3 = 3 Enter number 4 = 2 Enter number 5 = 4 Enter number 6 = 5 Enter number 7 = 4 Enter number 8 = 377 Enter number 9 = 3 Maximum number is 377 at index 8 Minimum number is 1 at index 1 [sgupta@rhel54x64 c]$ */
See also
Other popular tricky C Sample Codes and language Concept
.
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment