Home > Back-end >  Input n subjects (floating-point representation), statistics of the highest, lowest and average.
Input n subjects (floating-point representation), statistics of the highest, lowest and average.

Time:12-18

 

#include
Int main ()
{
int n;
int i;//loop variable
Float Max=1;
The float min=101;
Float score [5]={};
Float avg=0;
The scanf (" % d ", & amp; N);
for(i=0; i <5; I++)
{
The scanf (" % f ", score);
}
for(i=0; i <5; I++)
{
If (Max & lt; Score [I])
{
Max=score [I];
}
}
for(i=0; i <5; I++)
{
If (min & gt; Score [I])
{
Min=score [I];
}
}
for(i=0; I & lt; 5; I++)
{
Avg +=score [I];
}
Printf (" % % 2 f, 2 f. % 2 f ", Max, min, avg/5);
return 0;
}



Troublesome everybody have a look at what's the problem with this code?
Compile-time error, but the result is not correct

CodePudding user response:

Put the scanf (" % f ", score) to the scanf (" % f ", & amp; Score [I])

CodePudding user response:

Modified as follows, for your reference, the hope can help you:
 # include & lt; stdio.h> 
Int main (int arg c, char * argv [])
{
int n;
int i;//loop variable
Float Max;//Max=1;
The float min;//min=101;
Float score [5]={0};//score [5]={}; Initialized to 0
Float avg=0;
//the scanf (" % d ", & amp; N); It doesn't, put it n but it was useless to n
for(i=0; i <5; I++)
{
The scanf (" % f ", & amp; Score [I]);//the scanf (" % f ", score);
}

Min=Max=score [0];//Max min initialization here
for(i=0; i <5; I++)
{
If (Max & lt; Score [I])
{
Max=score [I];
}
//}
//for (I=0; i <5; I++)
//{
If (min & gt; Score [I])
{
Min=score [I];
}
}
for(i=0; i <5; I++)
{
Avg +=score [I];
}
Printf (" Max=% 2 f. Min=% 2 f avg.=% 2 f \ n ", Max, min, avg/5);
return 0;
}

CodePudding user response:

More refined, with a few less for:
 # include & lt; stdio.h> 
Int main (int arg c, char * argv [])
{
int n;
int i;//loop variable
Float Max;//Max=1;
The float min;//min=101;
Float score [5]={0};//score [5]={}; Initialized to 0
Float avg=0;
//the scanf (" % d ", & amp; N); It doesn't, put it n but it was useless to n
for(i=0; i <5; I++)
{
The scanf (" % f ", & amp; Score [I]);//the scanf (" % f ", score);
}

Min=Max=score [0];//Max min initialization here
for(i=0; i <5; I++)
{
If (Max & lt; Score [I])
{
Max=score [I];
}
//}
//for (I=0; i <5; I++)
//{
If (min & gt; Score [I])
{
Min=score [I];
}
//}
//for (I=0; i <5; I++)
//{
Avg +=score [I];
}
Printf (" Max=% 2 f. Min=% 2 f avg.=% 2 f \ n ", Max, min, avg/5);
return 0;
}
  • Related