Home > Back-end >  In C language array maximum minimum problems
In C language array maximum minimum problems

Time:03-19

Written in C to an array of maximum minimum

Why add min=[0] can seek a minimum

But Max didn't write or make

Why don't write Max=a [0] min=a [0] under the condition of

Max that min may not make

# define N 30
Int main ()
{
Int a [N], min=0, Max=0, I=0, sum=0, avg=0;
Printf (" please enter the 30 students achievement: ");
for (i=0; I & lt; N; I++)
{
Scanf_s (" % d ", & amp; A [I]);
}
for (i=0; I & lt; N; I++)
{
The sum +=a [I];
If (min & gt; A [I])
{
Min=a, [I].
}
If (Max & lt; A [I])
{
Max=a, [I].
}
}
Avg=sum/N.
Printf (" maximum=% d \ n ", Max).
Printf (" minimum value=% d \ n ", min);
Printf (" average=% d \ n ", avg);
return 0;
}

CodePudding user response:

 # include & lt; stdio.h> 
# define N 30
Int main ()
{
Int a [N], min, Max, I, sum, avg;
Printf (" please enter % d students grades \ n ", n);
for (i=0; I & lt; N; I++) {
While (1) {
Printf (" % d student achievement: ", I + 1);
fflush(stdout);
The rewind (sstdin);
If (1==scanf_s (" % d ", & amp; A [I]) {
If (0 & lt;=a [I] & amp; & A [I] <=100) break;
}
Printf (" input format error! Please re-enter the \ n ", n);
}
}
Sum=0;
Min=Max=a [0];
for (i=1; I & lt; N; I++) {
The sum +=a [I];
If (a [I] If (a [I] & gt; Max) Max=a [I];
}
Avg=sum/N.
Printf (" maximum=% d \ n ", Max).
Printf (" minimum value=% d \ n ", min);
Printf (" average=% d \ n ", avg);
return 0;
}

CodePudding user response:

Line 11 rewind (sstdin); Should be rewind (stdin);

CodePudding user response:

Because you all Numbers greater than zero, so can't compare

CodePudding user response:

Line 12 if (1==scanf_s (" % d ", & amp; A [I]) {should for the if (1==scanf_s (" % d ", & amp; A [I]) {

CodePudding user response:

Usually value initialized to minimum, the minimum can be initialized to the largest,

CodePudding user response:

The original poster can run this code, and see what the Max min avg output, look for feeling:
 
30 # define NInt main ()
{
Int a [N], min, Max, I, the sum=0, avg=0;
/* printf (" please enter the 30 students achievement: ");
for (i=0; I & lt; N; I++)
{
Scanf_s (" % d ", & amp; A [I]);
}
for (i=0; I & lt; N; I++)
{
The sum +=a [I];
If (min & gt; A [I])
{
Min=a, [I].
}
If (Max & lt; A [I])
{
Max=a, [I].
}
}
Avg=sum/N. */
Printf (" maximum=% d \ n ", Max).
Printf (" minimum value=% d \ n ", min);
Printf (" average=% d \ n ", avg);
system("pause");
return 0;
}

Maximum minimum size, are limited, within the scope of the size of the value is relative, is within the scope of a certain size, size must be in a specific area, then go to compare, for example to compare the height of a preschool children, if set initial value: Max=adult height, finally the Max also can find them?

CodePudding user response:

reference 5 floor wise men know already should good karma response:
usually value initialized to minimum, the minimum can be initialized to maximum,

This method is not insurance, and trouble (for different value type),
Personal opinion, at least one data, or the maximum and minimum values are initialized to 0 data comparison of insurance,

CodePudding user response:

I say this to make senses, empower them to fish,

CodePudding user response:

 min=Max=a, [0]. 

This sentence plus, initialize the maximum and the minimum,
Either, according to the upstairs to define a maximum as int can be said of the most-positive integer, min is defined as the minimum value of the int;
But int will be different in different compilers, such as 16-bit compilers and 32, 64 different identification of digits in the compiler; So, still have to consider this,
Therefore, min=Max=a [0]; Method is relatively simple initial words

CodePudding user response:

reference 1/f, zhao teacher reply:
 # include & lt; stdio.h> 
# define N 30
Int main ()
{
Int a [N], min, Max, I, sum, avg;
Printf (" please enter % d students grades \ n ", n);
for (i=0; I & lt; N; I++) {
While (1) {
Printf (" % d student achievement: ", I + 1);
fflush(stdout);
The rewind (sstdin);
If (1==scanf_s (" % d ", & amp; A [I]) {
If (0 & lt;=a [I] & amp; & A [I] <=100) break;
}
Printf (" input format error! Please re-enter the \ n ", n);
}
}
Sum=0;
Min=Max=a [0];
for (i=1; I & lt; N; I++) {
The sum +=a [I];
If (a [I] If (a [I] & gt; Max) Max=a [I];
}
Avg=sum/N.
Printf (" maximum=% d \ n ", Max).
Printf (" minimum value=% d \ n ", min);
Printf (" average=% d \ n ", avg);
return 0;
}

Bosses, but now we don't have to write so detailed thanks for sharing useful

CodePudding user response:

reference 3 floor truth is right or wrong response:
all Numbers greater than 0 because of you, so I can't compare out

Do the maximum minimum zha may not be greater than zero

CodePudding user response:

reference 5 floor wise men know already should good karma response:
usually value initialized to minimum, the minimum can be initialized to maximum,

It should be how to write the

CodePudding user response:

11 references Adorika response:
Quote: the truth is right or wrong reference 3 floor response:
all Numbers greater than 0 because of you, so I can't compare out

Do the maximum minimum zha may not be greater than zero?

For example you know
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related