SHARE
TWEET

Untitled

a guest Jun 25th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**Kyaw Han
  2.  IDE : CodeBlock
  3.  
  4. */
  5.  
  6. #include <iostream>
  7. #include <fstream>
  8. #include <string>
  9.  
  10. using namespace std;
  11.  
  12. void welcome();
  13. void readData(ifstream &inFile, int scores [], string stuID [], int &size);
  14. void stuIDOrder (string stuID[], int scores[], int size);
  15. void outPutID(string stuID[], int scores[], int size, ofstream &outFile);
  16. void scoresOrders(string stuID[], int scores[], int size);
  17. void outPutScores(string stuID[], int scores[], int size, ofstream &outFile);
  18. void lowestScore (string stuID[], int scores[], double &lowest, int size);
  19. void highestScore (string stuID[], int scores[], double &highest, int size);
  20. void calcAverage (int scores[], int size, double total, double &average);
  21. void ending ();
  22.  
  23. int main()
  24. {
  25.     const int SIZE = 45;
  26.     int size = 0;
  27.     int scores[SIZE];
  28.     double lowest, highest, average, total;
  29.     string stuID[SIZE];
  30.     string name;
  31.  
  32.  
  33.  
  34.     ifstream inFile;
  35.     ofstream outFile;
  36.  
  37.     welcome ();
  38.  
  39.     cout << "Please enter the name of the file : " << endl;
  40.     cin >> name;
  41.  
  42.     inFile.open(name.c_str());
  43.  
  44.     if(!inFile)
  45.         cout << "There is no valid file!";
  46.  
  47.     readData(inFile, scores, stuID, size);
  48.  
  49.  
  50. //    for(int i = 0; i < size; i++)
  51. //    {
  52. //        cout << stuID[i] << " " << scores[i] << endl;
  53. //    }
  54.  
  55.  
  56.     stuIDOrder(stuID, scores, size);
  57.  
  58.     outPutID(stuID, scores, size, outFile);
  59.  
  60.     scoresOrders(stuID, scores, size);
  61.  
  62.     outPutScores(stuID, scores, size, outFile);
  63.  
  64.     highestScore (stuID, scores, highest, size);
  65.  
  66.     lowestScore(stuID, scores, lowest, size );
  67.  
  68.     calcAverage (scores, size, total, average);
  69.  
  70.     ending ();
  71.  
  72. }
  73. /**
  74. This function displays the welcoming the message and explain what this program can do
  75. */
  76. void welcome()
  77. {
  78.     cout << "        Welcome!!!        " << endl;
  79.     cout << "  This Program Will Line  " << endl;
  80.     cout << " Out Students id and their" << endl;
  81.     cout << "scores in ascending and   " << endl;
  82.     cout << "     descending order!    " << endl;
  83. }
  84.  
  85. /**
  86. This function reads the contents of the file in two parallel ways
  87. */
  88. void readData (ifstream &inFile, int scores [], string stuID [], int &size)
  89. {
  90.     while (inFile >> stuID[size] >> scores[size])
  91.     {
  92.         size++;
  93.     }
  94. }
  95. /**
  96. This function sorts the student ID in ascending orders
  97. */
  98. void stuIDOrder (string stuID[], int scores[], int size)
  99. {
  100.     int minIndex, minScores, start;
  101.     string minID;
  102.  
  103.     for(start = 0; start < (size-1); start++)
  104.     {
  105.         minIndex = start;
  106.         minID = stuID[start];
  107.         minScores = scores[start];
  108.  
  109.         for(int index = start + 1; index < size; index++)
  110.         {
  111.             if(stuID[index] < minID)
  112.             {
  113.                 minID = stuID[index];
  114.                 minScores = scores[index];
  115.                 minIndex = index;
  116.  
  117.             }
  118.  
  119.         }
  120.         stuID[minIndex] = stuID[start];
  121.         scores[minIndex] = scores[start];
  122.         stuID[start] = minID;
  123.         scores[start] = minScores;
  124.     }
  125.  
  126.  
  127.  
  128. }
  129. /**
  130. This function sorts the scores and students' ID in descending orders with highest scores to the lowest
  131. */
  132. void outPutID(string stuID[], int scores[], int size, ofstream &outFile)
  133. {
  134.     outFile.open("scoresOut1.txt");
  135.  
  136.     for(int i = 0; i < size; i++)
  137.     {
  138.         outFile << stuID[i] << " " << scores[i] << endl;
  139.     }
  140.     outFile.close();
  141. }
  142. void scoresOrders(string stuID[], int scores[], int size)
  143. {
  144.     int maxIndex, maxScores, start;
  145.     string maxStuID;
  146.  
  147.     for(start = 0; start < (size-1);start++)
  148.     {
  149.         maxIndex = start;
  150.         maxScores = scores[start];
  151.         maxStuID = stuID[start];
  152.  
  153.         for(int index = start + 1; index < size; index++)
  154.         {
  155.             if(scores[index] > maxScores)
  156.             {
  157.                 maxScores = scores[index];
  158.                 maxStuID = stuID[index];
  159.                 maxIndex = index;
  160.             }
  161.         }
  162.         stuID[maxIndex] = stuID[start];
  163.         scores[maxIndex] = scores[start];
  164.         stuID[start] = maxStuID;
  165.         scores[start] = maxScores;
  166.     }
  167.  
  168. }
  169. /**
  170. This function output student's ID and Scores into a file based on their scores from the highest to the lowest orders
  171. */
  172. void outPutScores(string stuID[], int scores[], int size, ofstream &outFile)
  173. {
  174.     outFile.open("scoresOut2.txt");
  175.  
  176.     for(int i = 0; i < size; i++)
  177.     {
  178.         outFile << scores[i] << " " << stuID[i] << endl;
  179.     }
  180.     outFile.close();
  181.  
  182. }
  183. /**
  184. This function finds the lowest scores and display their scores along with student's ID
  185. */
  186. void lowestScore (string stuID[], int scores[], double &lowest, int size)
  187. {
  188.     lowest = scores[0];
  189.  
  190.     for (int count = 1; count < size; count++)
  191.     {
  192.         if(scores[count] < lowest)
  193.             lowest = scores[count];
  194.     }
  195.     cout << "\nStudents with lowest scores : ";
  196.     for (int i = 0; i < size; i++)
  197.     {
  198.         if(scores[i]==lowest)
  199.             cout << stuID[i] << " ";
  200.     }
  201.     cout << "\n";
  202. }
  203. /**
  204. This functin finds the highest scores and display their scores along with student's ID
  205. */
  206. void highestScore (string stuID[], int scores[], double &highest, int size)
  207. {
  208.     highest = scores[0];
  209.  
  210.     for (int count = 1; count < size; count++)
  211.     {
  212.         if(scores[count] > highest)
  213.             highest = scores[count];
  214.     }
  215.     cout << "\nStudents with highest scores : ";
  216.     for (int i = 0; i < size; i++)
  217.     {
  218.         if(scores[i] == highest)
  219.             cout << stuID[i] << " ";
  220.     }
  221.  
  222. }
  223. /**
  224. This function calculates the average of the class' total and displays it
  225. */
  226. void calcAverage (int scores[], int size, double total, double &average)
  227. {
  228.     for(int i = 0; i < size; i++ )
  229.     {
  230.         total += scores[i];
  231.     }
  232.  
  233.     average = total/(size*1.0);
  234.  
  235.     cout << "\nAverage of the class : " << average << endl;
  236.  
  237. }
  238. /**
  239. This function displays and ending message and appreciates the users
  240. */
  241. void ending ()
  242. {
  243.     cout << "====================" << endl;
  244.     cout << "    THANK YOU!!     " << endl;
  245. }
  246. /** OUTPUT
  247.         Welcome!!!
  248.   This Program Will Line
  249.  Out Students id and their
  250. scores in ascending and
  251.      descending order!
  252. Please enter the name of the file :
  253. scores.txt
  254.  
  255. Students with highest scores : 2ABCD AK323 DR123
  256. Students with lowest scores : 9QWE9 SW111 TY4XZ
  257.  
  258. Average of the class : 81.6923
  259. ====================
  260.     THANK YOU!!
  261.  
  262. Process returned 0 (0x0)   execution time : 1.423 s
  263. Press any key to continue.
  264.  
  265.  
  266. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top