int a[20] = {0};//defines a length of 20 array a int max = 0;//is used to store the maximum value of all elements in array a int min = 0;//is used to store the minimum value of all elements in array a int sum = 0;//used to store the number The aver of each element of group A and float = 0.0;//is used to store the average value of all elements in array a int secondmax = 0;//is used to store the second largest of array A, the second largest value is the maximum value of the remaining element different from the maximum for (int i = 0; I < 20; i++) {//used to loop over 20 random values and place them in the corresponding position in array A, and print out the obtained number a[i] = arc4random ()% (50-10 + 1) + 10; Sum + = a[i];//computes the value of the element in array a printf ("%d", a[i]); } aver = (float) sum/20; printf ("sum =%d aver =%f\n", sum, aver); for (int i = 0; i <; i++) {if (i = = 0) {///a[0] values are assigned to Max and Secondmax, Min, primarily to prevent the 20 numbers obtained from being initially determined by Max and Secondmax The values assigned at the time of righteousness are small, or are larger than min, and thus affect the correct result, where Max and Secondmax do not, because the range of random values is 10~50, which is written only to indicate that this is possible, and to prevent Min from appearing this situation max = a [i]; Secondmax = A[i]; min = a[0]; } else {if (Max < a[i]) {Secondmax = max; max = A[i]; } else if (Secondmax < a[i] && a[i]! = max) {SECONDMAx = A[i]; } if (min > A[i]) {min = a[i]; }}} printf ("max =%d min =%d Secondmax =%d", max, Min, Secondmax);
Randomly generates 20 positive integers [10, 50] into the array, and computes the maximum, minimum, average, sum, and second largest values of all elements in the array.