2 9 5 4 6
The number you entered is: 2 9 5 4 6.
The maximum quantity is: 9
The minimum quantity is: 2
The difference between the maximum quantity and the minimum quantity is: 7.
The average value is: 5.20
Press any key to continue. ...
An array is a collection that stores a series of numbers, and the storage location of each element in the array is continuous in memory, such as:
int a[ 10];
An array a consisting of 10 int values is declared. You can use square brackets [] and subscripts to access each element. Note that the position of the first element starts from 0, so a[0] is the first element and a[9] is the last element. Therefore, if you declare an array of length n, you must use 0~N- 1 as the subscript.
Arrays can be initialized at declaration time, for example:
int a[5] = { 1,2,3,4,5 };
In this way, the subscripts of array A correspond to the five numbers 1~5 from 0 to 4 in turn. If you provide the size of the array and only initialize the elements smaller than this size, other uninitialized elements will be automatically set to 0, for example:
int b[6] = { 1,2,3,4,5 };
Then the sixth element b[5] will be automatically set to 0. In addition, you can initialize a certain number of elements when you declare an array without specifying the size of the array. The compiler will automatically calculate the number of elements to determine the size of the array, for example:
int c[] = { 1,2,3,4,5,6,7,8,9, 10, 1 1 };
Then the size of array C is 1 1, because it has 1 1 elements.
The code is as follows:
# include & ltstdio.h & gt
int main()
{
/* Array A is used to store the input, max and min store the maximum and minimum values, and dif stores the difference */
int a[5],I,max,min,dif
/* Total store sum, average store average */
double sum = 0,avg
/* Cyclic read input */
Printf ("Please enter 5 numbers: \ n");
for(I = 0; I<5; ++i)
{
scanf("%d ",& ampa[I]);
}
/* Initialize max and min as the first element of input */
max = min = a[0];
/* Calculate the sum and find the maximum and minimum values */
Printf ("The number you entered is");
for(I = 0; I<5; ++i)
{
printf("%d ",a[I]);
sum+= a[I];
Max = max> A [me]? max:a[I];
Min = min< A [me]? min:a[I];
}
/* Calculate the difference between the maximum and minimum values and the average value */
dif = max-min;
avg = sum/5;
/* Print results */
Printf ("\ nmaximum quantity is: %d", max);
Printf ("\ nminimum value is: %d", min);
Printf ("\ nThe difference between the maximum value and the minimum value is: %d", dif);
Printf(" \ n Average value: %.2f\n\n ",avg);
}