Thoughts on Sorting arrays in c/c ++, calculating the average value, and obtaining the maximum and minimum values

Source: Internet
Author: User

Author: Guan Ning, a professor at the program Department of Netease College

Recently, I have found many problems in helping others explore and think about arrays, local variables, and full-population variables!

Sort operation

# Include

Voidsort (array, n)
Intarray [];
Intn;
{
Inti, j, k, t;
For (I = 0; I) {
K = I; [/LIGHT]/* record I for switching */
For (j = I + 1;) j {
If (array [j])/* use k to compare whether the element size of the j loop is greater than the current element value of the external I loop */
{
K = j;/* define the value of k as the smallest element (subscript) of the j loop for the swap operation */
}
T = array [k];/* defines the value of t as the value of the minimum element of the j loop */
Array [k] = array [I];/* replace the value (that is, the value of array [j]) in the j loop with the value of the element of this external I loop */
Array [I] = t;/* replace the value of the current element of the external I loop with the value of the temporary storage (that is, array [j]) in front of t */
}
}
}
Main ()
{
Inta [10], I;/* defines a [10] as an array, which is used to input 10 different numbers. defines the convenient I for loop ;*/
Printf ("enter the array ");
For (I = 0; I <10; I ++)
{
Scanf ("% d", & a [I]);/* use these 10 cycles to assign values to each array element from a [0] to a [9] */
}
Sort (a, 10);/* transmits the address and number of elements of array a to the form parameter of the custom sorting Han Number */
Printf ("the sorted array :");
For (I = 0; I <10; I ++)
{
Printf ("% d", a [I]);/* print the sorted array elements on the screen using 10 loops */
}
Printf ("");

}/* This code exercise serial number indicates that when the array name is used as the actual parameter of the Han number, the array value is not passed to the form parameter,
Instead, the starting address of the actual parameter array is passed to the form parameter array, so that the two arrays occupy a portion of memory units, while
No need to create two different arrays to waste memory space */


Average and maximum and minimum values

# Include

Floatmax;/* defines the global variable max for storing the maximum number */
Floatmin;/* define the full resident variable min for storing the smallest number */



Main ()
{
Floatcount_avg ();/* declare the Data Type of the custom Han Number */
Floatscore [10];/* defines an array to store input numbers */
Floatscore_avg;
Inti;/* define the variable used for loop I */
For (I = 0; I <10; I ++)/* input a value using an array element with 10 loops of score [0]-score [9 */
{
Scanf ("% f", & score);/* format the input */
}
Score_avg = count_avg (score, 10);/* Call count_avg to calculate the average value. The actual parameters include the address of the array score and the number of array elements */
Printf ("avg = % 6.2f max = % 6.2f min = % 6.2f", score_avg, max, min);/* print the average, maximum, and minimum values on the screen */
}

Floatcount_avg (array, score_num)/* defines the data type of the custom count_avg to be a floating point */
Floatarray [];/* definition form parameter array [] This array is a floating point */
Intscore_num;/* defines the form parameter score_num as the positive shape to obtain the actual parameter, that is, the number of elements in the array */
{
Inti;/* define the variable used for loop I */
Floatsum = array [0];/* pre-set the initial value of the total number to array [0] for subsequent comparison operations */
Floatavg;/* define avg, that is, the draw value storage variable as a floating point */

Max = array [0];/* pre-set the initial maximum value to array [0] for later operations */
Min = array [0];/* pre-set the initial value of the minimum value to array [0] to facilitate subsequent operations */

For (I = 1; I <10; I ++)/* use a 9-time loop to compare one by one with array [0] To find the maximum and minimum values */
{
If (array> max)/* compare the stored maximum value with the array in the current loop (find maxcompute )*/
{
Max = array;/* The storage that meets the conditions is the maximum value. Note: (Here we use the loop to judge the repeated comparison operation to gradually replace the maximum value and finally obtain the maximum number )*/
}
Elseif (array <min)/* compare the stored minimum value with the array in the current loop (Small computation )*/
{
Min = array;/* The storage that meets the conditions is the minimum value. Note: (here, we use the loop to judge the repeated comparison operation to gradually replace the minimum value and finally obtain the maximum number )*/
}
Sum + = array;/* calculate the total number of array elements */
}

Avg = sum/score_num;/* calculate the final average value */
Return (avg);/* returns the average value */
}


/* In this example, You need to note that using global variables can reduce the number of actual and formal parameters, thus reducing the memory space and the time overhead of passing value data. In addition, the C language also specifies the external
Arrays can enrich the initial values, while local arrays cannot assign the initial values. We recommend that you do not use global variables when necessary, because 1: global variables occupy storage during all program execution.
Units, rather than opening up units only when needed. 2: This reduces the universality of the Han number, because the Han number depends on other external variables during execution. 3: too many global variables are used,
This will reduce program clarity and make it difficult to clearly determine the values of each instantaneous external variable. The external variable value will be changed during the execution of each Han number, leading to program errors! 4. If the external variable is not
Definition at the beginning of the file, so it only takes effect from the definition point to the end of the file
*/
In learning, you should pay more attention to the details. These things are similar in java. I hope you will be inspired!

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.