Click here to Skip to main content
14,774,472 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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
Posted
Comments
[no name] 24-Jul-14 19:57pm
   
Learn to use the debugger and you will see where you are going wrong. And give your variables real names.
Mohibur Rashid 24-Jul-14 23:38pm
   
wait, how does it even compile? next thing is you if you want to check if they are similar than you will have to use == sign not < or > sign
Stefan_Lang 25-Jul-14 4:26am
   
Some hints:
1. Use longer variable names, even if it's only for index values. If you're a lazy typer, use an intelligent editor that supports the C/C++ programming language, or an IDE that has such an editor built-in. These editors help a lot with the typing due to autocomplete features. Once you've changed the names of the variables l, h, n and m to something more telling, you may already be able to spot a few errors without further aid! Just try it!

2. Specifically watch out for index value ranges, and for using comparisons like <, <=, >, or >=, because it's easy to accidentally fall short by one element or do one step too many. The easiest way to avoid such errors is consider the highest and lowest possible value, and check if the code would work correctly for these. Or check the two consecutive values that would give a different result for your comparison, and verify that the comparison matches the exact point you wanted.

3. arrays in C/C++ always start at index 0! Revisit your code and make sure it does use correct index values.

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;
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900