fork download
  1. //Maxwell Brewer CS1A Chapter 3, P. 143, #3
  2. //
  3. /***************************************************************
  4.  *
  5.  * AVERAGE TEST SCORES
  6.  * _____________________________________________________________
  7.  *
  8.  * This program will receive input from the user
  9.  * and output a calculation of average test scores.
  10.  * The user will input each test score and the
  11.  * program will output the average score..
  12.  * _____________________________________________
  13.  * INPUT
  14.  *
  15.  * Test scores 1-5
  16.  *
  17.  * OUTPUT
  18.  *
  19.  * Average test scores
  20.  *
  21.  ***************************************************************/
  22.  
  23. #include <iostream>
  24. #include <iomanip>
  25. #include <string>
  26.  
  27. using namespace std;
  28.  
  29. int main() {
  30.  
  31. // Initialization
  32.  
  33. const char msgTest1[40] = "Enter the score from test 1: \n";
  34. const char msgTest2[40] = "Enter the score from test 2: \n";
  35. const char msgTest3[40] = "Enter the score from test 3: \n";
  36. const char msgTest4[40] = "Enter the score from test 4: \n";
  37. const char msgTest5[40] = "Enter the score from test 5: \n";
  38. const char msgEnd [50] = "The average of the scores in the test is: ";
  39.  
  40. double test1, test2, test3, test4, test5;
  41. float sum;
  42. double average;
  43.  
  44.  
  45. // Display input prompts
  46.  
  47. cout << msgTest1;
  48.  
  49. cin >> test1;
  50. cout << msgTest2;
  51. cin >> test2;
  52. cout << msgTest3;
  53. cin >> test3;
  54. cout << msgTest4;
  55. cin >> test4;
  56. cout << msgTest5;
  57. cin >> test5;
  58.  
  59.  
  60. sum = test1 + test2 + test3 + test4 + test5;
  61. average = sum / 5;
  62.  
  63. // Output
  64.  
  65. cout << msgEnd << fixed << setprecision(1) << average;
  66.  
  67. return 0;
  68. }
Success #stdin #stdout 0.01s 5264KB
stdin
98.9
36.78
66.5
88.23
85.45
stdout
Enter the score from test 1: 
Enter the score from test 2: 
Enter the score from test 3: 
Enter the score from test 4: 
Enter the score from test 5: 
The average of the scores in the test is: 75.2