Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Daniel Springer and Timothy Bowman
- //4/30/2017
- //Bubble Grader
- #include <iostream>
- #include <string>
- #include <iomanip>
- #include <fstream>
- using namespace std;
- const int MAX_STUDENTS = 100;
- const int MAX_ANSWERS = 100;
- bool loadKey(ifstream & inFile, int answerCount, char correctAnswers[]){ // Timothy Bowman: Opens file and inputs key into an array.
- string fn, ln;
- inFile>>fn>>ln;
- if(fn != "KEY" || ln != "KEY"){
- cout << "Error bad key" << endl;
- return false;
- }
- for (int i=0; i<answerCount; i++){
- inFile >> correctAnswers[i];
- }
- return true;
- }
- void loadStudentAnswerFile(ifstream & inFile, int answerCount, int studentCount, string fNames[], string lNames[], char studentAnswers[ MAX_STUDENTS][ MAX_ANSWERS]){ // Daniel Springer puts names and answer of each student into arrays.
- for (int stu=0; stu<studentCount; stu++){
- string fn, ln;
- inFile >> fNames[stu] >> lNames[stu];
- for (int answ=0; answ<answerCount; answ++){
- inFile >> studentAnswers[stu][answ];
- }
- }
- }
- void printRawData(string fNames[],string lNames[],char studentAnswers[MAX_STUDENTS][MAX_ANSWERS],char correctAnswers[],int studentCount, int answerCount){ // Daniel Springer: outputs the Key's answers as well as outputs names and answers of students.
- cout << "RAW DATA REPORT" << endl;
- cout << "KEY, KEY ";
- for (int i=0; i<answerCount; i++){
- cout << correctAnswers[i] << " ";
- }
- for (int stu=0; stu<studentCount; stu++){
- string fn, ln;
- cout << "\n" << lNames[stu] << ", " << fNames[stu] << " ";
- for (int answ=0; answ<answerCount; answ++){
- cout <<studentAnswers[stu][answ] << " ";
- }
- }
- cout << endl << endl;
- }
- void gradeIt(string fNames[],string lNames[],char studentAnswers[MAX_STUDENTS][MAX_ANSWERS],char correctAnswers[],int studentCount,int answerCount,int correctCounts[MAX_STUDENTS]){ // Daniel Springer: outputs * with wrong answers as well as outputs grades of the students.
- cout << "UNSORTED ANSWERS WITH SCORES (*) = incorrect" << endl;
- cout << "KEY, KEY ";
- for (int i=0; i<answerCount; i++){
- cout << correctAnswers[i] << " ";
- }
- cout << " SCORE";
- for (int stu=0; stu<studentCount; stu++){
- int rightAnswers = answerCount ;
- string fn, ln;
- cout << "\n" << lNames[stu] << ", " << fNames[stu] << " ";
- for (int answ=0; answ<answerCount; answ++){
- if (correctAnswers[answ] != studentAnswers[stu][answ]){
- cout << "*";
- rightAnswers--;
- }
- cout << studentAnswers[stu][answ] << " ";
- }
- cout << " " << rightAnswers << "/" << answerCount;
- correctCounts[stu] = rightAnswers;
- }
- cout << endl;
- }
- void printGrades(string lNames[],string fNames[],int correctCounts[MAX_STUDENTS],int answerCount,int studentCount){
- // Daniel Springer: outputs students percentage grade also made new variable because I needed double variables to make percentages.
- for (int stu=0; stu<studentCount; stu++){
- double answerCountB = answerCount;
- double correctCountsB = correctCounts[stu];
- string fn, ln;
- cout << "\n" << lNames[stu] << ", " << fNames[stu] << " ";
- double percentage = (correctCountsB/ answerCountB) *100 ;
- cout << correctCounts[stu] << "/" << answerCount << " = " << setprecision(1) << fixed << percentage << " ";
- if (percentage >=90)
- cout << "A";
- else if (percentage >=80)
- cout << "B";
- else if (percentage >=70)
- cout << "C";
- else if (percentage >=60)
- cout << "D";
- else
- cout << "F";
- }
- cout << endl << endl;
- }
- void sortByName(string lNames[],string fNames[],int correctCounts[MAX_STUDENTS],int studentCount){
- cout << "\n" << "SORTED BY NAME"; // Daniel Springer: primarily Bob Bradley's sort function but added more variable to follow the first one.
- for (int pass = studentCount-1; pass>=0; pass--){
- int max=0;
- for (int i=1; i <= pass; i++){
- if (lNames[max] < lNames[i]){
- max=i;
- }
- string temp = lNames[max];
- string temp2 = fNames[max];
- int temp3 = correctCounts[max];
- lNames[max] = lNames[pass];
- fNames[max] = fNames[pass];
- correctCounts[max] = correctCounts[pass];
- lNames[pass] = temp;
- fNames[pass] = temp2;
- correctCounts[pass]= temp3;
- }
- }
- }
- void sortByScore(string lNames[],string fNames[],int correctCounts[MAX_STUDENTS],int studentCount){
- for (int pass = studentCount-1; pass>=0; pass--){ /*Daniel Springer: primarily Bob Bradley's sort function but added more variable to follow the first one. */
- int max=0;
- for (int i=1; i <= pass; i++){
- if (correctCounts[max] < correctCounts[i]){
- max=i;
- }
- string temp = lNames[max];
- string temp2 = fNames[max];
- int temp3 = correctCounts[max];
- lNames[max] = lNames[pass];
- fNames[max] = fNames[pass];
- correctCounts[max] = correctCounts[pass];
- lNames[pass] = temp;
- fNames[pass] = temp2;
- correctCounts[pass]= temp3;
- }
- }
- }
- void printGradeStats(int correctCounts[MAX_STUDENTS],int answerCount,int studentCount){
- cout << "GRADE STATS" << endl << "This shows how many times each grade was made:" << endl;
- int AA=0; // Timothy Bowman: counts eachs students grade and give a total count of the letter grades.
- int BB=0;
- int CC=0;
- int DD=0;
- int FF=0;
- for (int i=0; i<studentCount; i++){
- double answerCountB = answerCount;
- double correctCountsB = correctCounts[i];
- double percentage = (correctCountsB/ answerCountB) *100 ;
- if (percentage >= 90)
- AA++;
- else if(percentage >= 80)
- BB++;
- else if(percentage >= 70)
- CC++;
- else if(percentage >= 60)
- DD++;
- else
- FF++;
- }
- cout << "A - " << AA << endl << "B - " << BB << endl <<"C - " << CC << endl <<"D - " << DD << endl<<"F - " << FF;
- }
- int main(){
- // Bubble Grader Main Driver program created by Bob Bradley for CSCI 221 Program 4 Fall 2015. Updated SU16.
- // Use the code below as is. You can comment out the code and then start uncommenting to get going.
- // Ask the user - Which file do you want to grade?
- // and read it into a test name variable
- cout << "Which file do you want to grade? " << endl;
- string testName;
- cin >> testName;
- string fileName = testName + ".txt";
- ifstream inFile;
- inFile.open(fileName.c_str());
- if (inFile.fail()){
- cout << "File not_found. Quiting." << endl;
- return 1;
- }
- cout << "\n" << "Loading test file: " << fileName << endl;
- int studentCount;
- int answerCount;
- inFile >> studentCount >> answerCount;
- cout << "Student Count: " << studentCount << endl;
- cout << "Number of answers: " << answerCount << endl;
- // Load the correctAnswers array from the file
- // Function should return false if key row dows not start with KEY KEY
- // Show error and quit the program if the answer key was not found
- char correctAnswers[MAX_ANSWERS];
- if (!loadKey(inFile, answerCount, correctAnswers)){
- cout << "Sorry, the answer key was not the first record" << endl;
- return 0;
- }
- cout << "Answer key read." << endl << endl;
- // Load the student file with the number of students
- // and the names and answers for each student
- string fNames[MAX_STUDENTS], lNames[MAX_STUDENTS];
- char studentAnswers[MAX_STUDENTS][MAX_ANSWERS];
- loadStudentAnswerFile(inFile, answerCount, studentCount, fNames, lNames, studentAnswers);
- printRawData(fNames, lNames, studentAnswers, correctAnswers, studentCount, answerCount);
- // grade - print and compare each student's answer to the correct answer and show the score
- // and create the correct counts array, which will have how many answers each student got correct
- int correctCounts[MAX_STUDENTS];
- gradeIt(fNames, lNames, studentAnswers, correctAnswers, studentCount, answerCount, correctCounts);
- cout << "\n" << "UNSORTED GRADES";
- printGrades(lNames, fNames, correctCounts, answerCount, studentCount);
- // sort and print by name - modify the sort code from the slides
- sortByName(lNames, fNames, correctCounts, studentCount);
- printGrades(lNames, fNames, correctCounts, answerCount, studentCount);
- cout << "\n" << "SORTED BY SCORE";
- // sort and print by score - modify the sort code from the slides
- sortByScore(lNames, fNames, correctCounts, studentCount);
- printGrades(lNames, fNames, correctCounts, answerCount, studentCount);
- // Grade Stats
- // Go though the correct counts array, recaculate each student's grade and then
- // count how many As, Bs, Cs, Ds and Fs there are total.
- printGradeStats(correctCounts, answerCount, studentCount);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement