Calculate the maximum value of 10 integers in C Language
We know that to calculate the maximum value of three integers, we can define three variables to store these three values, but if there are ten, one hundred, one thousand, it makes no sense to define so many variables. How can this problem be solved? At this time, we can use an array to store the values we need to compare. For example, we can use a [10] for ten values. Beautiful, concise, and efficient! The data storage solution is comparison. First define a variable, such as MAX, to store one of the ten values and then compare them one by one. But for convenience, we usually select the first value of the array, that is, a [0], to assign a value to the variable, and then compare it with a [1], a [2], and order in sequence. If MAX is less than a [1], the value of a [1] is assigned to MAX and then compares with a [2. The C language code is as follows:
# Include <stdio. h> int main () {int I, MAX, a [10]; printf ("Enter the ten numbers to be compared:"); for (I = 0; I <10; I ++) scanf ("% d", & a [I]); MAX = a [0]; for (I = 0; I <10; I ++) {if (a [I]> MAX) {MAX = a [I] ;}} printf ("the maximum value of 10 is: % d \ n ", MAX); return 0 ;}
TIPS: The return Statement cannot return a "pointer" pointing to the "stack memory" because the function body is automatically destroyed when it ends.