Mainly covers a basic loop and checks and conditions to get out of the loop.
- Can use "continue" to get out of the loop, but usually there are ways to do it without in a cleaner way.
/* * File: main.c * Author: user * * Created on October 3, 2015, 2:03 AM */ #include <stdio.h> #include <stdlib.h> /* * Create a program which repeatedly asks a user for a grade between 0 and 100 * (inclusive) and computes the minimum grade, the maximum grade and the average * grade.The grades may be entered as floating point numbers * and the program should ask for another grade when presented with on greater * than 100.Any grade less than 0 should case the program to terminate.Prior * to termination, the program should print the min, max, avg grade. The program * cannot ask the user for the number of grades directly. * * Inputs *inputGrade - double (0-100) >0 is invalid/less than 0 ends program * Outputs *minGrade - double smallest grade *maxGrade - double largest grade *avgGrade - double average grade */ int main(int argc, char** argv) { // Declare and Initialize variables double inputGrade, minGrade, maxGrade, avgGrade, sumGrades; int numGrades; minGrade = 100; maxGrade = 0; numGrades = 0; sumGrades = 0; // Loop through and repeatedly ask user for grades // determine running min/max/avg while we loop // Exit conditions: inputGrade Less than 0 printf ("Enter a grade: "); scanf ("%lf", &inputGrade); while (inputGrade >= 0) { // can be another way to check for valid input //if (inputGrade > 100) { //// ask user for input //continue; //} // Check Valid Input if (inputGrade <= 100) { // Calculate running statistics if (inputGrade > maxGrade) { maxGrade = inputGrade; // calculate running Max } if (inputGrade < minGrade) { minGrade = inputGrade; // calculate running Min } // Running total of grades and number of grades --> get average at the end numGrades += 1; sumGrades += inputGrade; } printf ("Enter a grade: "); scanf ("%lf", &inputGrade); } // compute average avgGrade = sumGrades / numGrades; // Output results if (numGrades > 0) { printf ("Number of grades: %d\n", numGrades); printf ("Minimum Grade: %lf\n", minGrade); printf ("Maximum Grade: %lf\n", maxGrade); printf ("Average Grade: %lf\n", avgGrade); } else { printf ("Didn't enter grades\n") } return (EXIT_SUCCESS); }