fork download
  1. //Natalie Zarate CSC 5 Chapter 9, P. 538, #4
  2. /*******************************************************************************
  3.  *
  4.  * DISPLAY SCORES
  5.  *
  6.  * This program will accept integer test scores and names for the user, then it
  7.  * will calculate the average score. After the average score is caluculated, the
  8.  * program will display the students scores in ascending order and the average
  9.  * score
  10.  *______________________________________________________________________________
  11.  * INPUT
  12.  * student_names[] - names of test-takers
  13.  * test_scores[] - student test scores
  14.  ******************************************************************************/
  15. #include <iostream>
  16. #include <string>
  17. #include <iomanip>
  18. using namespace std;
  19.  
  20. /*******************************************************************************
  21.  * Function GetStudentNames
  22.  *
  23.  * The function will get the names of the test-takers/students and store them in
  24.  * an array
  25.  * _____________________________________________________________________________
  26.  * INPUT
  27.  * num - number of students who took the test
  28.  *
  29.  * OUTPUT
  30.  * names - names of students/test-takers
  31.  * ****************************************************************************/
  32. string *GetStudentNames(int num);
  33.  
  34. /*******************************************************************************
  35.  * Function GetTestScores
  36.  *
  37.  * This Function will prompt the user for the test scores for a previously
  38.  * defined number of tests, then store it in an array.
  39.  * _____________________________________________________________________________
  40.  * INPUT
  41.  * num - number of tests submitted
  42.  *
  43.  * OUTPUT
  44.  * scores - score for each student
  45.  * ****************************************************************************/
  46. int *GetTestScores(int num);
  47.  
  48. /*******************************************************************************
  49.  * Function SortArray
  50.  *
  51.  * This function will sort test scores in ascending order.
  52.  * _____________________________________________________________________________
  53.  * INPUT
  54.  * names - names of students
  55.  * scores - students' scores
  56.  * num - number of tests submitted
  57.  * initial_ val - value of element that is being used as reference
  58.  * min_subscript - subscript of minimum value
  59.  * min_value - value of (current) minimum element
  60.  * bookmark - holder for name
  61.  *
  62.  * OUTPUT
  63.  * names - students' names
  64.  * scores - students' scores
  65.  * ****************************************************************************/
  66. void SortArray(string *names, int *scores, int num);
  67.  
  68. /*******************************************************************************
  69.  * Function GetAverageScore
  70.  *
  71.  * This function will compute the average score of the scores provided.
  72.  * _____________________________________________________________________________
  73.  * INPUT
  74.  * scores - students' test scores
  75.  * num - number of tests
  76.  * score_sum - sum of students' scores
  77.  *
  78.  * OUTPUT
  79.  * average - average test score
  80.  * ****************************************************************************/
  81. float GetAverageScore(int *scores, int num);
  82.  
  83. /*******************************************************************************
  84.  * Function OuputScores
  85.  *
  86.  * This function will display student names, test scores, and the average
  87.  * overall test score.
  88.  * _____________________________________________________________________________
  89.  * INPUT
  90.  * names - students' names
  91.  * scores - students' scores
  92.  * num - number of tests
  93.  * average - average test score
  94.  * ****************************************************************************/
  95. void OutputScores(string *names, int *scores, int num, float average);
  96.  
  97. int main()
  98. {
  99. string *student_names; // INPUT - names of test takers
  100. int *test_scores; // INPUT - student test scores
  101. int tests; // INPUT - number of tests
  102. float average_score; // INPUT - average test score
  103.  
  104. // Prompt user for number of test scores
  105. cout << "Enter Number of Test Scores: " << endl;
  106. cin >> tests;
  107.  
  108. // Call GetStudentNames
  109. student_names = GetStudentNames(tests);
  110.  
  111. // Call GetTestScores
  112. test_scores = GetTestScores(tests);
  113.  
  114. // Call SortArray
  115. SortArray(student_names, test_scores, tests);
  116.  
  117. // Call GetAverageScore
  118. average_score = GetAverageScore(test_scores, tests);
  119.  
  120. // Call OutputScores
  121. OutputScores (student_names, test_scores, tests, average_score);
  122.  
  123. // Deallocate memory
  124. delete [] student_names;
  125. delete [] test_scores;
  126.  
  127. return 0;
  128. }
  129.  
  130. /*******************************************************************************
  131.  * Function GetStudentNames
  132.  *
  133.  * The function will get the names of the test-takers/students and store them in
  134.  * an array
  135.  * _____________________________________________________________________________
  136.  * INPUT
  137.  * num - number of students who took the test
  138.  *
  139.  * OUTPUT
  140.  * names - names of students/test-takers
  141.  * ****************************************************************************/
  142. string *GetStudentNames(int num)
  143. {
  144. string student; // INPUT - student name
  145. string *names; // OUTPUT - names of students who took the test
  146.  
  147. // Dynamically allocate memeory
  148. names = new string[num];
  149.  
  150. //Populate array with names
  151.  
  152. for (int i = 0; i < num; i++)
  153. {
  154. // Prompt for name
  155. cout << "Enter Name of Student " << i + 1 << ":" << endl;
  156. cin >> *(names + i);
  157. }
  158.  
  159. //Return names
  160. return names;
  161. }
  162.  
  163. /*******************************************************************************
  164.  * Function GetTestScores
  165.  *
  166.  * This Function will prompt the user for the test scores for a previously
  167.  * defined number of tests, then store it in an array.
  168.  * _____________________________________________________________________________
  169.  * INPUT
  170.  * num - number of tests submitted
  171.  *
  172.  * OUTPUT
  173.  * scores - score for each student
  174.  * ****************************************************************************/
  175. int *GetTestScores (int num)
  176. {
  177. int *scores; // OUTPUT - all student's scores
  178.  
  179. //Dynamically allocate memory
  180. scores = new int[num];
  181.  
  182. for (int i = 0; i < num; i++)
  183. {
  184. // Prompt for test score
  185. cout << "Enter Test Score for Student " << i + 1 << ": " << endl;
  186. cin >> *(scores + i);
  187.  
  188. }
  189.  
  190. return scores;
  191. }
  192.  
  193. /*******************************************************************************
  194.  * Function SortArray
  195.  *
  196.  * This function will sort test scores in ascending order.
  197.  * _____________________________________________________________________________
  198.  * INPUT
  199.  * names - names of students
  200.  * scores - students' scores
  201.  * num - number of tests submitted
  202.  * initial_ val - value of element that is being used as reference
  203.  * min_subscript - subscript of minimum value
  204.  * min_value - value of (current) minimum element
  205.  * bookmark - holder for name
  206.  *
  207.  * OUTPUT
  208.  * names - students' names
  209.  * scores - students' scores
  210.  * ****************************************************************************/
  211. void SortArray(string *names, int *scores, int num)
  212. {
  213. int initial_val; // INPUT - reference value
  214. int min_subscript; // INPUT - subscript of minimum value
  215. int min_value; // INPUT - value of (current) minimum element
  216. string bookmark; // INPUT - holder for name
  217.  
  218. for(initial_val = 0; initial_val < num; initial_val++ )
  219. {
  220. min_subscript = initial_val;
  221. min_value = *(scores + initial_val);
  222. bookmark = *(names + initial_val);
  223.  
  224. for(int i = initial_val + 1; i < num; i++)
  225. {
  226. if (*(scores + i) < min_value)
  227. {
  228. min_value = *(scores + i);
  229. bookmark = *(names + i);
  230. min_subscript = i;
  231. }
  232. }
  233.  
  234. *(scores + min_subscript) = *(scores + initial_val);
  235. *(names + min_subscript) = *(names + initial_val);
  236. *(scores + initial_val) = min_value;
  237. *(names + initial_val) = bookmark;
  238. }
  239. }
  240.  
  241. /*******************************************************************************
  242.  * Function GetAverageScore
  243.  *
  244.  * This function will compute the average score of the scores provided.
  245.  * _____________________________________________________________________________
  246.  * INPUT
  247.  * scores - students' test scores
  248.  * num - number of tests
  249.  * score_sum - sum of students' scores
  250.  * OUTPUT
  251.  * average - average test score
  252.  * ****************************************************************************/
  253. float GetAverageScore(int *scores, int num)
  254. {
  255. int score_sum = 0; // INPUT - sum of students' scores
  256. float average; // OUTPUT - average test score
  257.  
  258. // Sum all values
  259. for (int i = 0; i < num; i++)
  260. {
  261. score_sum += *(scores + i);
  262. }
  263. // Compute average
  264. average = score_sum / num;
  265.  
  266. return average;
  267. }
  268.  
  269. /*******************************************************************************
  270.  * Function OutputScores
  271.  *
  272.  * This function will display student names, test scores, and the average
  273.  * overall test score.
  274.  * _____________________________________________________________________________
  275.  * INPUT
  276.  * names - students' names
  277.  * scores - students' scores
  278.  * num - number of tests
  279.  * average - average test score
  280.  * ****************************************************************************/
  281. void OutputScores(string *names, int * scores, int num, float average)
  282. {
  283. // Display Header
  284. cout << right;
  285. cout << "\n\nSTUDENT" << setw(15) << "SCORE" << endl;
  286. cout << "----------------------" << endl;
  287.  
  288. for (int i = 0; i < num; i++)
  289. {
  290. cout << left << setw(17) << *(names + i);
  291. cout << *(scores + i) << endl;
  292. }
  293.  
  294. }
Success #stdin #stdout 0.01s 5276KB
stdin
5
Kayley 
Maria 
Victoria
Gabby
Leslie
100
47
76
87
91
stdout
Enter Number of Test Scores: 
Enter Name of Student 1:
Enter Name of Student 2:
Enter Name of Student 3:
Enter Name of Student 4:
Enter Name of Student 5:
Enter Test Score for Student 1: 
Enter Test Score for Student 2: 
Enter Test Score for Student 3: 
Enter Test Score for Student 4: 
Enter Test Score for Student 5: 


STUDENT          SCORE
----------------------
Maria            47
Victoria         76
Gabby            87
Leslie           91
Kayley           100