Use a sentinel controlled while loop that will ask the user to enter student
grades until a value of -1 is entered. Use a counter variable to count all the
grades that are passing grades, where 70 is the minimum passing grade. If there
are any grades that are out of the range 0-100, present an error messafe to the
user, and do not count that grade as passing. Show the percentage of the valid
grades that are passing.
I don't understand how to calculate the percentage in this. We did not go over it in class.
/*Use a sentinel controlled while loop that will ask the user to enter student
grades until a value of -1 is entered. Use a counter variable to count all the
grades that are passing grades, where 70 is the minimum passing grade. If there
are any grades that are out of the range 0-100, present an error messafe to the
user, and do not count that grade as passing. Show the percentage of the valid
grades that are passing.
*/
#include <stdio.h>
#include <stdlib.h>
// Create a sentinel controlled while loop asking user to enter grade
main() {
int score;
printf("Enter a test score (-1 to quit)");
scanf_s("%i", &score);
while (score <= 100 || score <= 1) {
printf("Enter a test score (-1 to quit):");
scanf_s("%i", &score);
}
// Enter void message for numbers greater than 100 entered.
void printErrorMessage(int score); {
if (score > 100);
printf("That is not a valid grade!\n");
}
printf("\nThe percentage of the correct grades are:\n");
system("pause");
}
percent is literally just a division by 100 of something. Generally its a part of a total divided by the total.
ok. so what?
so lets count the values that passed and do some stuff: (your trouble is you need more variables to track more info, like how many passed)
blah blah
while(…)
{
if (score >= 70)
numpassing++; //remember to zero initialize these.
totalscores++; //not in the condition..
}
blah blah
double pctpassing = (double)(numpassing)/(double)totalscores; //true %, a number between 0 and 1, multiply by 100 here for a 'human' percent to display it.