Skip to content

Instantly share code, notes, and snippets.

@StabbyMcDuck
Created August 4, 2014 03:52
Show Gist options
  • Save StabbyMcDuck/566ee00aa7cafbec8591 to your computer and use it in GitHub Desktop.
Save StabbyMcDuck/566ee00aa7cafbec8591 to your computer and use it in GitHub Desktop.
// Regina Imhoff
// Ecampus CS 161
// Assignment #6
// makes histogram of students' grades as entered by an instructor
#include <iostream> // includes and statements section
#include <string> // strings
#include <cstdlib> //c standard library
#include <stdio.h>
#include <string.h>
using std::cin; // user input
using std::cout; // machine output
using std::endl; // for line breaks
using std::string; //string
using std::getline; // getline
const int MINIMUM_GRADE = 0;
const int MAXIMUM_GRADE = 5;
const int GRADES = MAXIMUM_GRADE - MINIMUM_GRADE + 1; // Number of grades in the range between minimum and maximum, inclusive.
const int STOP = -1;
const string GRADE_RANGE = ("(" + MINIMUM_GRADE + "-" + MAXIMUM_GRADE + ")");
// Allows teacher to input a single student's grade.
int inputStudentGrade(){
int grade;
while(true) {
cout << "Enter a student's grade " << GRADE_RANGE << endl;
cout << "Enter " << STOP << " to stop entering grades" << endl;
cin >> grade;
if (grade == STOP ||
(grade >= MINIMUM_GRADE && grade <= MAXIMUM_GRADE)) {
break;
}
cout << "That is not a valid grade " << GRADE_RANGE
<< " or stop signal (" << STOP << ")" << endl;
}
return grade;
}
// Allows teacher to input grades for all students in a class.
int inputClassGrades(int gradeCount[GRADES]){
int grade;
while(true) {
grade = inputStudentGrade();
if (grade == STOP) {
break;
}
gradeCount[grade]++;
}
return gradeCount;
}
int main(){
int gradeCount[GRADES] = {0, 0, 0, 0, 0, 0};
inputClassGrades(gradeCount);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment