Skip to content

Instantly share code, notes, and snippets.

@dsonck92
Created March 26, 2019 14:18
Show Gist options
  • Save dsonck92/bc8f869e387c384e8051f39e86041d0d to your computer and use it in GitHub Desktop.
Save dsonck92/bc8f869e387c384e8051f39e86041d0d to your computer and use it in GitHub Desktop.
#include <stdio.h>
void get_grades(float *sum, int *num, int *passed) {
// Initialize all values to 0
(*num) = 0;
(*passed) = 0;
(*sum) = 0.0f;
// Loop through the maximum grades
for ((*num) = 0; (*num) < 10; (*num)++) {
// Store for the grade
float grade;
// Do forever
for (;;) {
// Ask for the grade
printf("[%02d] Grade: ", (*num) + 1);
scanf("%f", &grade);
// If the grade is below 0, stop asking completely
if (grade < 0.0f) {
return;
// If the grade is valid, break out of forever
} else if (grade >= 1.0f && grade <= 10.0f) {
break;
}
}
// If the grade is higher than 5.5
if (grade >= 5.5f) {
// The student has passed
(*passed) += 1;
}
// Add the grade to the sum
(*sum) += grade;
}
}
int main() {
printf("Please specify the grades between 1.0 and 10, -1 exits\n");
// Storage for all variables
int passed = 0;
int num = 0;
float sum = 0.0f;
// Ask for all the grades
get_grades(&sum, &num, &passed);
// Check if there is at least one grade, print the results
if (num > 0) {
printf("Average: %f\n", sum / (float)(num));
printf("Passed : %d\n", passed);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment