Calculate the second largest value of a group of random numbers with C, and cannot be obtained by sorting the whole.
1 randomly generates 20 positive integers [10, 50] into the array, and calculates the maximum value, minimum value, average value, sum of each element, and second value of all elements in the array.
Int A [20];
Int sum = 0; // store the sum of array elements
// Assign values to Arrays
Printf ("the element in the array is \ n ");
For (INT I = 0; I <20; I ++ ){
A [I] = arc4random () % 41 + 10;
Sum + = A [I]; // sum the number in the array
Printf ("% d", a [I]);
}
Printf ("\ n ");
Int max = A [0]; // store the maximum value in the array
Int min = A [0]; // store the minimum value in the array
Int second = A [0]; // store the second largest value in the array
Int Ave = 0; // average value of Storage
Ave = sum/20;
For (INT I = 1; I <20; I ++ ){
// Calculate the maximum value in the array
If (A [I]> MAX ){
Max = A [I];
}
// Calculate the minimum value in the array
If (A [I] <min ){
Min = A [I];
}
}
// Calculate the second largest value in the array
For (INT I = 1; I <20; I ++ ){
If (second <Max & Second <A [I]) {
If (A [I] = max ){
Continue;
}
Second = A [I];
}
}
Printf ("maximum value: % d \ n", max );
Printf ("minimum value: % d \ n", min );
Printf ("the average of these numbers is % d \ n", Ave );
Printf ("second-largest value: % d \ n", second );
Store a positive integer randomly generated by C in the array, and calculate the maximum, minimum, average, and sum of all elements in the array, and the second largest value.