fork download
  1. // Kurt Feiereisel CSC5 Chapter 7, p.446, #11
  2. /*******************************************************************************
  3.  *
  4.  * Calculate Student's Grades
  5.  * _____________________________________________________________________________
  6.  * This program allows a user to enter student's names and test grades for each
  7.  * student. The program will then calculate and report the student's average
  8.  * of the test grades and a letter grade.
  9.  * _____________________________________________________________________________
  10.  * Formula:
  11.  * avg = total / TESTS
  12.  * INPUT:
  13.  * names[] : The names of the students
  14.  * testscore[] : The scores the student's recieved on their tests
  15.  * total : Each student's accumulated test scores
  16.  *
  17.  * OUTPUT:
  18.  * avg : Average for each student's test scores
  19.  * letter[] : Each student's letter grade based on their average
  20.  * test score
  21.  *
  22.  *
  23.  * ****************************************************************************/
  24. #include <iostream>
  25. #include <string>
  26. using namespace std;
  27.  
  28. // Initialize Constants
  29. const int STUDS = 5;
  30. const int TESTS = 4;
  31.  
  32. // Function Prototypes
  33. void students(string names[]);
  34. void testGrades(float testscore[][TESTS], string names[]);
  35. void calcAvg(float averages[], float testscore[][TESTS],string names[]);
  36. void letterGrade(char letter[], float averages[]);
  37. void displayArray(float averages[], char letter[], string names[]);
  38.  
  39.  
  40. int main()
  41. {
  42. // Declare Arrays;
  43. string names[STUDS]; // INPUT: student's names
  44. float testscore[STUDS][TESTS]; // INPUT: Student's test scores
  45. float averages[STUDS]; // OUTPUT: Average score for each student
  46. char letter[STUDS]; // OUTPUT: Student's letter grade based on
  47. // test average
  48.  
  49. // Call Functions
  50. students(names);
  51. testGrades(testscore, names);
  52. calcAvg(averages, testscore, names);
  53. letterGrade(letter, averages);
  54. displayArray(averages, letter, names);
  55. return 0;
  56. }
  57.  
  58. /*
  59.  * Definition of students
  60.  * This function accepts input as student's names.
  61.  */
  62. void students(string n[])
  63. {
  64. // Input Student's Names
  65. for (int index = 0; index < STUDS; index++)
  66. {
  67. cout << "Please enter students names:" << endl;
  68. cin >> n[index];
  69. }
  70. }
  71.  
  72. /*
  73.  * Definition of testGrades
  74.  * This function accepts input as Test scores for each test for each
  75.  * student.
  76.  */
  77.  
  78. void testGrades(float t[][TESTS], string n[])
  79. {
  80. // Input Test Scores
  81. for (int index = 0; index < STUDS; index++)
  82. {
  83. cout << "Enter " << n[index] << " test scores: \n";
  84. for (int testIndex = 0; testIndex < TESTS; testIndex++)
  85. {
  86. cout << "Test " << (testIndex + 1) << ": ";
  87. cin >> t[index][testIndex];
  88.  
  89. // Input Verification
  90. while( (t[index][testIndex] < 0) || t[index][testIndex] > 100)
  91. {
  92. cout << "Please enter a grade between 0 and 100." << endl;
  93. cin >> t[index][testIndex];
  94. }
  95. }
  96. cout << endl;
  97. }
  98. }
  99.  
  100. /*
  101.  * Definition of calcAvg
  102.  * This function will calculate the averages foe each student
  103.  */
  104. void calcAvg(float a[], float t[][TESTS], string n[])
  105. {
  106. // Initialize Variables
  107. float total;
  108. float avg;
  109.  
  110. // Accumulate total score for each student
  111. for(int index = 0; index < STUDS; index++)
  112. {
  113. total = 0;
  114. for(int count = 0; count < TESTS; count++)
  115. {
  116. total += t[index][count];
  117. }
  118. // Calcluate Average for each student
  119. avg = total / TESTS;
  120.  
  121. // Store average for student in average array
  122. a[index] = avg;
  123. }
  124. }
  125.  
  126. /*
  127.  * Definition of letterGrade
  128.  * This function will take the averages of each student and convert it to a
  129.  * letter grade.
  130.  */
  131. void letterGrade(char l[], float a[])
  132. {
  133. // Determine Letter Grade based on Average
  134. for(int index = 0; index < STUDS; index++)
  135. {
  136. if(a[index] <= 100 && a[index] >= 90)
  137. l[index] = 'A';
  138. else if(a[index] < 90 && a[index] >= 80)
  139. l[index] = 'B';
  140. else if(a[index] < 80 && a[index] >= 70)
  141. l[index] = 'C';
  142. else if(a[index] < 70 && a[index] >= 60)
  143. l[index] = 'D';
  144. else if(a[index] < 60 && a[index] >= 0)
  145. l[index] = 'F';
  146. else
  147. cout << "Invalid" << endl;
  148. }
  149. }
  150. /*
  151.  * Definition of displayArray
  152.  * This function will display the letter grade and average grade of each student
  153.  */
  154. void displayArray(float a[], char l[], string n[])
  155. {
  156. // Display Student's names, averages, and letter grades
  157. for(int index = 0; index < STUDS; index++)
  158. {
  159. cout << n[index] << "'s" << " results:" << endl;
  160. cout << "\tScore: " << a[index] << endl;
  161. cout << "\tGrade: " << l[index] << endl;
  162. }
  163. }
Success #stdin #stdout 0s 5304KB
stdin
Billy
Bob
Joe
Smith
Tom
99
97
96
88
89
84
86
78
75
73
74
68
65
67
64
59
53
56
53


stdout
Please enter students names:
Please enter students names:
Please enter students names:
Please enter students names:
Please enter students names:
Enter Billy test scores: 
Test 1: Test 2: Test 3: Test 4: 
Enter Bob test scores: 
Test 1: Test 2: Test 3: Test 4: 
Enter Joe test scores: 
Test 1: Test 2: Test 3: Test 4: 
Enter Smith test scores: 
Test 1: Test 2: Test 3: Test 4: 
Enter Tom test scores: 
Test 1: Test 2: Test 3: Test 4: 
Billy's results:
	Score: 95
	Grade: A
Bob's results:
	Score: 84.25
	Grade: B
Joe's results:
	Score: 72.5
	Grade: C
Smith's results:
	Score: 63.75
	Grade: D
Tom's results:
	Score: 40.5
	Grade: F