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.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.