Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.42 KB | None | 0 0
  1. //Daniel Springer and Timothy Bowman
  2. //4/30/2017
  3. //Bubble Grader
  4.  
  5.  
  6. #include <iostream>
  7. #include <string>
  8. #include <iomanip>
  9. #include <fstream>
  10. using namespace std;
  11.  
  12.  
  13. const int MAX_STUDENTS = 100;
  14. const int MAX_ANSWERS  = 100;
  15.  
  16. bool loadKey(ifstream & inFile, int answerCount, char correctAnswers[]){ // Timothy Bowman: Opens file and inputs key into an array.
  17.     string fn, ln;
  18.     inFile>>fn>>ln;
  19.         if(fn != "KEY" || ln != "KEY"){
  20.             cout << "Error bad key" << endl;
  21.             return false;
  22.         }
  23.     for (int i=0; i<answerCount; i++){
  24.         inFile >> correctAnswers[i];
  25.     }
  26.     return true;
  27. }
  28.  
  29. 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.
  30.         for (int stu=0; stu<studentCount; stu++){
  31.             string fn, ln;
  32.             inFile >> fNames[stu] >> lNames[stu];
  33.        
  34.                 for (int answ=0; answ<answerCount; answ++){
  35.                     inFile >> studentAnswers[stu][answ];
  36.                 }
  37.         }
  38.        
  39.  
  40. }
  41.  
  42. 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.
  43.     cout << "RAW DATA REPORT" << endl;
  44.     cout << "KEY, KEY        ";
  45.     for (int i=0; i<answerCount; i++){
  46.         cout << correctAnswers[i] << " ";
  47.     }
  48.  
  49.    
  50.     for (int stu=0; stu<studentCount; stu++){
  51.             string fn, ln;
  52.             cout  << "\n" << lNames[stu] << ", " << fNames[stu] << " ";
  53.     for (int answ=0; answ<answerCount; answ++){
  54.             cout <<studentAnswers[stu][answ] << " ";
  55.                 }
  56.         }
  57.        
  58.     cout << endl << endl;
  59.        
  60.  
  61. }
  62.  
  63. 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.
  64.  
  65.     cout << "UNSORTED ANSWERS WITH SCORES (*) = incorrect" << endl;
  66.     cout << "KEY, KEY        ";
  67.     for (int i=0; i<answerCount; i++){
  68.         cout << correctAnswers[i] << " ";
  69.     }
  70.     cout << "    SCORE";
  71.    
  72.     for (int stu=0; stu<studentCount; stu++){
  73.         int rightAnswers = answerCount ;
  74.             string fn, ln;
  75.             cout  << "\n" << lNames[stu] << ", " << fNames[stu] << " ";
  76.     for (int answ=0; answ<answerCount; answ++){
  77.             if (correctAnswers[answ] != studentAnswers[stu][answ]){
  78.                 cout << "*";
  79.                 rightAnswers--;
  80.             }
  81.             cout << studentAnswers[stu][answ] << " ";
  82.     }
  83.     cout << "  " << rightAnswers << "/" << answerCount;
  84.     correctCounts[stu] = rightAnswers;
  85.     }
  86.     cout << endl;
  87. }    
  88. void printGrades(string lNames[],string fNames[],int correctCounts[MAX_STUDENTS],int answerCount,int studentCount){
  89.  // Daniel Springer: outputs students percentage grade also made new variable because I needed double variables to make percentages.
  90.         for (int stu=0; stu<studentCount; stu++){
  91.             double answerCountB = answerCount;
  92.             double correctCountsB = correctCounts[stu];
  93.                 string fn, ln;
  94.                 cout  << "\n" << lNames[stu] << ", " << fNames[stu] << " ";
  95.                 double percentage = (correctCountsB/ answerCountB) *100 ;
  96.                 cout << correctCounts[stu] << "/" << answerCount << " =  " << setprecision(1) << fixed << percentage << " ";
  97.             if (percentage >=90)
  98.                 cout << "A";
  99.             else if (percentage >=80)
  100.                 cout << "B";
  101.             else if (percentage >=70)
  102.                 cout << "C";
  103.             else if (percentage >=60)
  104.                 cout << "D";
  105.             else
  106.                 cout << "F";
  107.         }
  108.        
  109.     cout << endl << endl;
  110. }
  111.    
  112. void sortByName(string lNames[],string fNames[],int correctCounts[MAX_STUDENTS],int studentCount){
  113.     cout << "\n" << "SORTED BY NAME"; // Daniel Springer: primarily Bob Bradley's sort function but added more variable to follow the first one.
  114.     for (int pass = studentCount-1; pass>=0; pass--){
  115.         int max=0;
  116.         for (int i=1; i <= pass; i++){
  117.             if (lNames[max] < lNames[i]){
  118.             max=i;
  119.             }
  120.         string temp = lNames[max];
  121.         string temp2 = fNames[max];
  122.         int temp3 = correctCounts[max];
  123.         lNames[max] = lNames[pass];
  124.         fNames[max] = fNames[pass];
  125.         correctCounts[max] = correctCounts[pass];
  126.         lNames[pass] = temp;
  127.         fNames[pass] = temp2;
  128.         correctCounts[pass]= temp3;
  129.         }
  130.     }
  131. }
  132.  
  133. void sortByScore(string lNames[],string fNames[],int correctCounts[MAX_STUDENTS],int studentCount){
  134.     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. */
  135.         int max=0;
  136.         for (int i=1; i <= pass; i++){
  137.             if (correctCounts[max] < correctCounts[i]){
  138.             max=i;
  139.             }
  140.         string temp = lNames[max];
  141.         string temp2 = fNames[max];
  142.         int temp3 = correctCounts[max];
  143.         lNames[max] = lNames[pass];
  144.         fNames[max] = fNames[pass];
  145.         correctCounts[max] = correctCounts[pass];
  146.         lNames[pass] = temp;
  147.         fNames[pass] = temp2;
  148.         correctCounts[pass]= temp3;
  149.         }
  150.     }
  151. }
  152.    
  153. void printGradeStats(int correctCounts[MAX_STUDENTS],int answerCount,int studentCount){  
  154.     cout << "GRADE STATS" << endl << "This shows how many times each grade was made:" << endl;
  155.         int AA=0; // Timothy Bowman: counts eachs students grade and give a total count of the letter grades.
  156.         int BB=0;
  157.         int CC=0;
  158.         int DD=0;
  159.         int FF=0;
  160.     for (int i=0; i<studentCount; i++){
  161.         double answerCountB = answerCount;
  162.         double correctCountsB = correctCounts[i];
  163.         double percentage = (correctCountsB/ answerCountB) *100 ;
  164.             if (percentage >= 90)
  165.                 AA++;
  166.             else if(percentage >= 80)
  167.                 BB++;
  168.             else if(percentage >= 70)
  169.                 CC++;
  170.             else if(percentage >= 60)
  171.                 DD++;
  172.             else
  173.                 FF++;
  174.            
  175.        
  176.    
  177. }  
  178.     cout << "A - " << AA << endl << "B - " <<  BB << endl <<"C - " <<  CC << endl <<"D - " <<  DD << endl<<"F - " <<  FF;
  179. }
  180.    
  181.    
  182.  
  183.  
  184. int main(){
  185.     // Bubble Grader Main Driver program created by Bob Bradley for CSCI 221 Program 4 Fall 2015.  Updated SU16.
  186.     // Use the code below as is.  You can comment out the code and then start uncommenting to get going.
  187.    
  188.     // Ask the user - Which file do you want to grade?
  189.     //  and read it into a test name variable
  190.    
  191.     cout << "Which file do you want to grade? " << endl;
  192.    
  193.     string testName;
  194.     cin >> testName;
  195.    
  196.     string fileName = testName + ".txt";
  197.    
  198.     ifstream inFile;
  199.     inFile.open(fileName.c_str());
  200.    
  201.     if (inFile.fail()){
  202.         cout << "File not_found. Quiting." << endl;
  203.         return 1;
  204.     }
  205.    
  206.     cout << "\n" << "Loading test file: " << fileName << endl;
  207.    
  208.     int studentCount;
  209.     int answerCount;
  210.    
  211.     inFile >> studentCount >> answerCount;
  212.     cout << "Student Count: " << studentCount << endl;
  213.     cout << "Number of answers: " << answerCount << endl;
  214.    
  215.    
  216.    
  217.     // Load the correctAnswers array from the file
  218.     // Function should return false if key row dows not start with KEY KEY
  219.     // Show error and quit the program if the answer key was not found
  220.  
  221.     char correctAnswers[MAX_ANSWERS];
  222.    
  223.     if (!loadKey(inFile, answerCount,   correctAnswers)){
  224.         cout << "Sorry, the answer key was not the first record" << endl;
  225.         return 0;
  226.     }
  227.     cout << "Answer key read." << endl << endl;
  228.        
  229.        
  230.    
  231.     // Load the student file with the number of students
  232.     //  and the names and answers for each student
  233.        
  234.    
  235.     string fNames[MAX_STUDENTS], lNames[MAX_STUDENTS];
  236.     char studentAnswers[MAX_STUDENTS][MAX_ANSWERS];
  237.  
  238.     loadStudentAnswerFile(inFile, answerCount, studentCount,    fNames, lNames, studentAnswers);
  239.  
  240.  
  241.     printRawData(fNames, lNames, studentAnswers, correctAnswers,  studentCount, answerCount);
  242.    
  243.    
  244.    
  245.    
  246.     // grade - print and compare each student's answer to the correct answer and show the score
  247.     //         and create the correct counts array, which will have how many answers each student got correct
  248.    
  249.    
  250.     int correctCounts[MAX_STUDENTS];
  251.    
  252.     gradeIt(fNames, lNames, studentAnswers, correctAnswers, studentCount, answerCount,   correctCounts);
  253.    
  254.     cout << "\n" << "UNSORTED GRADES";
  255.    
  256.    
  257.     printGrades(lNames, fNames, correctCounts, answerCount, studentCount);
  258.    
  259.    
  260.  
  261.     // sort and print by name - modify the sort code from the slides
  262.     sortByName(lNames, fNames, correctCounts, studentCount);
  263.    
  264.    
  265.     printGrades(lNames, fNames, correctCounts, answerCount, studentCount);
  266.    
  267.     cout << "\n" << "SORTED BY SCORE";
  268.     // sort and print by score - modify the sort code from the slides
  269.     sortByScore(lNames, fNames, correctCounts, studentCount);
  270.     printGrades(lNames, fNames, correctCounts, answerCount, studentCount);
  271.    
  272.    
  273.     // Grade Stats
  274.     // Go though the correct counts array, recaculate each student's grade and then
  275.     //   count how many As, Bs, Cs, Ds and Fs there are total.
  276.    
  277.     printGradeStats(correctCounts, answerCount, studentCount);
  278.    
  279.    
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement