• Portfolio
  • Archive
    • Grad Students Halloween Party 2016
    • Praxis Showcase 2010 Highlights
    • 2015 Canada Blooms Near North Hardscapes
    • 2014 IEEE Toronto AGM Highlights
    • 2014 GSU Halloween Party
    • 2014 GSU Halloween Party 2
    • IEEE Day: Wine and Cheese
    • 2014 Akido Club Photoshoot
    • Canonball 1T5
    • 2014 Toronto Christmas Market
    • 2015 Nocturne
    • Cute-Baby-TBP
  • Stuff.
  • Contact Info

kmingk

Just Me.

  • Portfolio
  • Archive
    • Grad Students Halloween Party 2016
    • Praxis Showcase 2010 Highlights
    • 2015 Canada Blooms Near North Hardscapes
    • 2014 IEEE Toronto AGM Highlights
    • 2014 GSU Halloween Party
    • 2014 GSU Halloween Party 2
    • IEEE Day: Wine and Cheese
    • 2014 Akido Club Photoshoot
    • Canonball 1T5
    • 2014 Toronto Christmas Market
    • 2015 Nocturne
    • Cute-Baby-TBP
  • Stuff.
  • Contact Info

APS105 Tutorial 3

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);
}
Monday 10.05.15
Posted by Kei-Ming Kwong
Newer / Older