Advertisement
riggnaros

grade report array outfile

Nov 8th, 2017
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <limits>
  4. #include <cmath>
  5. #include <iomanip>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9.  
  10. int testScoreArray[100];
  11. void selectSort(int testScoreArray[], int n);
  12. void fileOutput(int testScoreArray[]);
  13.  
  14. int main()
  15.  
  16. {
  17.     int n = 100;
  18.  
  19.     ifstream infile;
  20.     infile.open("testscoresarrayhomework.txt");
  21.  
  22.     for (int i = 0; i < 100; i++) {
  23.  
  24.         infile >> testScoreArray[i];
  25.     }
  26.  
  27.     selectSort(testScoreArray, n);
  28.     fileOutput(testScoreArray);
  29.  
  30.     infile.close();
  31.  
  32.     return 0;
  33. }
  34.  
  35. void selectSort(int testScoreArray[], int n)
  36. {
  37.     int pos_max, temp;
  38.  
  39.     for (int i = 0; i < n - 1; i++) {
  40.         pos_max = i;
  41.  
  42.         for (int j = i + 1; j < n; j++) {
  43.  
  44.             if (testScoreArray[j] > testScoreArray[pos_max])
  45.                 pos_max = j;
  46.         }
  47.  
  48.         if (pos_max != i) {
  49.             temp = testScoreArray[i];
  50.             testScoreArray[i] = testScoreArray[pos_max];
  51.             testScoreArray[pos_max] = temp;
  52.         }
  53.     }
  54. };
  55.  
  56. void fileOutput(int testScoreArray[])
  57. {
  58.  
  59.     ofstream outfile;
  60.     int gradeEvent = 1;
  61.     int previousGrade = 0;
  62.  
  63.     outfile.open("testscoresoutput.txt");
  64.     outfile << "Test Score Breakdown: ";
  65.     outfile << endl
  66.             << "Score / Occurance";
  67.  
  68.     for (int i = 0; i < 100; i++) {
  69.        
  70.         if (previousGrade == testScoreArray[i]){
  71.             gradeEvent++;
  72.         }
  73.  
  74.         if (previousGrade && previousGrade != testScoreArray[i]) {
  75.            
  76.             outfile << '\n' << previousGrade << " / " << gradeEvent;
  77.             gradeEvent=1;
  78.         }
  79.        
  80.  
  81.         previousGrade = testScoreArray[i];
  82.     }
  83.                 outfile << '\n' << previousGrade << " / " << gradeEvent;
  84.  
  85.  
  86.     outfile.close();
  87. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement