I think in this case between C and C++ would be reading file (or wherever you read the data input from).
First thing to ask is what mark system is it? I guess it's 1-10 marks system.
I gonna write the program as there would be pure data in input file (100 numbers separated by '\n' or ' '.
Here are definitions for the code I gonna write. Definitions are great so you can easily change some options in your code:
Code:
#define MAX_MARK 10
#define STUDENTS 100
Another thing is that this matters as there is no vector in C, so we must use array. Let's say:
Code:
int m[MAX_MARK + 1]
We use
int because we gonna count how many students got mark i (where is stands in m[i]). We also write 11 instead of 10 because 4 bytes waste will save us some readability in code.
so now we must fill this array. First we open the file (I'll skip file opening, I would use freopen(). It's debugging-friendly) and then read the data:
Code:
int t; //temporary variable
for( int n = 1; n <= STUDENTS; n++ ){
scanf( "%i", t );
m[t]++;
}
There is another option to read all students to another array and then use the loop to go through, but isn't it a waste of time and memory?
So know we have counted how many students go mark i. Next we gonna find the lowest mark to pass (as I understand that's what you need):
Code:
int passed = 0;
int i; //declaring this variable so
//it's value will be saved after loop
for( i = MAX_MARK; i >= 1; i++ ){
passed += m[i];
if( passed >= 75 )
break;
}
After this loop we should have i as the mark that is the lowest to pass. Now I though that we could define that 75, but whatever, this code is very short and actually there might be no declarations, that's up to you.
About that break. Some people say that continue and break should be avoided, but I find them very useful. If we wanted to avoid them, we could use while() or do...while() loop:
Code:
int passed = 0;
int i = MAX_MARK;
while( passed < 75 ){
passsed += m[i];
i--;
}
i++;
Code:
int passed = 0'
int i = MAX_MARK;
do{
passed += m[i];
i--;
}while( passed < 75 );
i++;
As you can see in those cases I use only passed < 75 and there is no i >= 1 because if it reaches 1, it means it will have counted all 100% students, so it will stop anyway.
If you wanted to avoid those i++ after both loops, you could move i-- before passed += m[i] and set i to MAX_MARK+1 at the beginning, like this:
Code:
int passed = 0;
int i = MAX_MARK + 1;
while( passed < 75 ){
i--;
passsed += m[i];
}
Code:
int passed = 0'
int i = MAX_MARK + 1;
do{
i--;
passed += m[i];
}while( passed < 75 );
Hope it helped.
P.S. if you wrote all the code into 1 function, then int t (where we read from file) could be changed to i, but that's not a good idea. I think that would be a bad programming style. I would even move that part to another function and use a reference or make m array a global one to fill it.