(1) Store data in a one-dimensional array, such as an A array.
(2) Find the maximum value in the array (assuming that the variable max is used to store the maximum value). First, consider a[0] as the maximum number, that is, make max=a[0] first. Compare all elements except a[0] (represented by a[i]) with max one by one, if a [I] >; Max, then a[i] is the current maximum number, so max=a[i]. Max is the maximum value after comparing all elements.
(3) Output the maximum number (the value of max).
This problem involves the nesting of loops and the content of one-dimensional arrays in C language. Taking six numbers as an example, the specific process is as follows:
# include & ltstdio.h & gt
int main(void)
{
int a[6],I,max
for(I = 0; I<6; i++)? //Enter 6 numbers in array A..
scanf("%d ",& ampa[I]);
max = a[0]; ? //First, consider a[0] as the maximum number, and store a[0] in max.
for(I = 1; I<6; i++)? //Compare the remaining 5 numbers with max, and store the largest number in max.
if(a[I]& gt; max)? max = a[I];
printf("max=%d\n ",max); //Output the maximum value
Returns 0;
}
Program running results:
10 ? 2 ? 14 ? 6 ? 5 ? 1 1
max= 14
Extended data:
One-dimensional array is the simplest array and its logical structure is a linear table. To use one-dimensional array, you need to go through the process of definition, initialization and application.
Array declaration:
(1) In the declaration format of array, "data type" is the data type of declaring array elements, which can be any data type in java language, including simple type and structural type. "Array name" is the name used to unify these same data types, and its naming rules are the same as those of variables.
(2) After the array is declared, the next step is to allocate the memory needed by the array. At this time, the operator new must be used, where the "number" tells the compiler how many elements need to be stored in the array declared, so the new operator tells the compiler to allocate a space for the array in memory according to the number in parentheses.
Using the new operator to allocate memory space for array elements is called dynamic allocation.
For example:
int[]x; //Declare an int array named x.
x = new int[ 10]; //X array contains 10 elements, and memory space is allocated for these 10 elements.
When you declare an array, you can also combine two statements into one line in the following format:
Data type [] Array name = new data type [number];
Using this format, declare an array and allocate a piece of memory for the array. The above example can be written as follows:
int[]x = new int[ 10];
The int[]x on the left of the equal sign is equivalent to defining a special variable X. The data type of X is a reference to the int array object, X is a reference variable of the array, and the number of array elements referenced by X is uncertain.
The New int[ 10] to the right of the equal sign is to create an array object with 10 int variables in the heap memory. int[]x = new int[ 10]; Is to assign the array object on the right to the array reference variable on the left. ?
References:
Array _ Baidu Encyclopedia