如何计算得分最高和最低的学生? [英] How to count the students who got the highest and lowest score?

查看:69
本文介绍了如何计算得分最高和最低的学生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以显示最高和最低分数,但我无法显示得分最高和最低的学生人数。



  int  h = array [ 1 ]; 
int l = array [ 0 ];
int m = 0 ,n = 0 ;
for int z = 1 ; z< = 20 ; z ++)
{
if (array [z]> h)
{
h = array [z];
}
else if (array [z]< l)
{
l = array [z];
}
}

int q = 1 ;
for (q = 1 ; q< = 20 ; q ++)
{
if (array [g]> m)
{
M = M + 1;
}
else if (array [q]< n)
{
n = n + 1;
}
}
cout<< m<< 学生得到了最高得分<< h<<< Point(s) << ENDL;
cout<< n<< 学生得分最低< <<<< Point(s)<< endl;



这应该是输出



12名学生获得45分的最高分/>
8名学生得分最低1

解决方案

首先需要创建一个包含一些元素的正确数组,例如:

  const   int  STUDENT_COUNT =  20 ; 
int studentArray [STUDENT_COUNT];



然后你需要用分数填充它,你可以手动完成

  int  studentArray [STUDENT_COUNT] = { 21  13  20  20  14  7 ,...}; 



或使用一些生成随机数的函数

  for  int  i =  0 ; i< STUDENT_COUNT; ++ i)
{
< span class =code-keyword> int score = GetRandomScoreValue(); // 返回随机数的函数
studentArray [i] =得分;
}





然后你需要做的就是迭代数组寻找最高和最低分数。然后再次计算达到每个分数的学生数量。最后打印你的结果。


Quote:

int h = array [1];

那应该是

  int  h =  array  [ 0 ]; 





Quote:

for(q = 1; q< = 20; q ++)

应该是

  for (q =  0 ; q< =  20 ; q ++)





Quote:

if(array [g]> m)

应该是

  if  array  [g] == h)





Quote:

else if(array [q]< n)>

应该是

 其他  if  array  [q] == l)


将您的代码更改为:

  int  h = array [< span class =code-digit> 0 ];  //  使用0索引值初始化两个值 
int l = array [ 0 ];
int m = 0 ,n = 0 ;
for int z = 0 ; z< 20; z ++) // 数组应从索引0开始
{
if (array [z]> h)
{
h = array [z];
}
else if (array [z]< l)>
{
l = array [z];
}
}

int q = 0 ;
for (q = 0 ; q< 20; q ++) // 此处还有数组索引应该从0开始
{
if (array [g] == h) // 检查有多少学生得分等于最高
{
m = m + 1;
}
else if (array [q] == l) // 检查有多少学生得分等于最低
{
n = n + 1 ;
}
}
cout<< m<< 学生得到了最高得分<< h<<< Point(s) << ENDL;
cout<< n<< 学生得分最低< <<<< Point(s)<< endl;


I can display the highest and lowest score but I can't display the number of students who got the highest and lowest score.

int h=array[1];
int l=array[0];
int m=0,n=0;
for(int z=1; z<=20; z++)
 {
   if(array[z]>h)
     {
       h=array[z];
     }
   else if(array[z]<l)
     {
       l=array[z];
     }
 }

int q=1;
for(q=1;q<=20;q++)
 {
  if(array[g]>m)
   {
     m=m+1;
   }
  else if(array[q]<n)
   {
     n=n+1;
   }
 }
cout<<m<<" students got the highest score of "<<h<<" Point(s)"<<endl;
cout<<n<<" student got the lowest score of "<<l<<" Point(s)"<<endl;


this should be the output

12 students got the highest score of 45
8 students got the lowest score of 1

解决方案

You first need to create a proper array that has some elements, for example:

const int STUDENT_COUNT = 20;
int studentArray[STUDENT_COUNT];


Then you need to populate it with the scores, you could do it manually

int studentArray[STUDENT_COUNT] = { 21, 13, 20, 20, 14, 7, ... };


or by using some function that generates random numbers

for (int i = 0; i < STUDENT_COUNT; ++i)
{
    int score = GetRandomScoreValue(); // function to return a random number
    studentArray[i] = score;
}


etc.
Then all you need to do is iterate the array looking for the highest and lowest scores. Then do the same again counting the number of students who have reached each score. And finally print your results.


Quote:

int h=array[1];

That should be

int h=array[0];



Quote:

for(q=1;q<=20;q++)

should be

for(q=0;q<=20;q++)



Quote:

if(array[g]>m)

should be

if(array[g]==h)



Quote:

else if(array[q]<n)>

should be

else if(array[q]==l)


Change your Code to this:

int h=array[0];     //initialize both value with 0 index value
int l=array[0];
int m=0,n=0;
for(int z=0; z<20; z++)   //Array should start from index 0
 {
   if(array[z]>h)
     {
       h=array[z];
     }
   else if(array[z]<l)>
     {
       l=array[z];
     }
 }

int q=0;
for(q=0;q<20;q++)        // here also Array index should start from 0
 {
  if(array[g]==h)           //Checking how many student got marks equal to highest
   {
     m=m+1;
   }
  else if(array[q]==l)     //Checking how many student got marks equal to lowest
   {
     n=n+1;
   }
 }
cout<<m<<" students got the highest score of "<<h<<" Point(s)"<<endl;
cout<<n<<" student got the lowest score of "<<l<<" Point(s)"<<endl;


这篇关于如何计算得分最高和最低的学生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆