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.

Source: Internet
Author: User

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.

Related Article

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.