How to count the students who got the highest and lowest score?

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
      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=array[q];
	    } 
	  else if(array[q]<n)
	   {
	   	n=array[q];
	   } 	
       }


this should be the output

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

Last edited on
Make up your mind, try to always think what task each variable does,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// I assume your array starts at 0 not 1 (and it should be 0)
int h=array[0];
int l=array[0];
int m=0,n=0;
// h = highest , l = lowest , m = h count , n = l count , right?
for(int z=0; z<=20; z++){
	if(array[z] > h){
		h = array[z];
	}else if(array[z] < l){
		l = array[z];
	}    	
}

//int q=1; -> what's this for
for(q=0;q<=20;q++){
	if( array[q] == h ){
		m++;
	}
	if( array[q] == l ){
		n++;
	} 	
}

cout << m << " students got the highest score of " << h << endl;
cout << n << " students got the lowest score of " << l << endl;
Thank you sir I learned a lot from you.
Topic archived. No new replies allowed.