fork download
  1. //Nicolas Ruano CS1A Chapter 3 Pp. 142 #3
  2. /*******************************************************************************
  3.  * CALCULATING TEST AVERAGES
  4.  * ____________________________________________________________________________
  5.  * We gather all test scores being provided and we have to find the average
  6.  * score on at least one decimal
  7.  * ____________________________________________________________________________
  8.  * Score 1 = 85
  9.  * Score 2 = 92
  10.  * Score 3 = 78
  11.  * Score 4 = 88
  12.  * Score 5 = 91
  13.  * ___________________________________________________________________________
  14.  * Add all the result togehter to get a full number, then we divide out by 5 to
  15.  * get the average result.
  16.  *
  17.  * SPECIAL NOTE: Behold, a perfect example of how do you calculate score by
  18.  * first adding all of results together and then dividing the added result by
  19.  * five to get the whole grade average.
  20. *******************************************************************************/
  21. #include <iostream>
  22. #include <iomanip> // for std::fixed and std::setprecision
  23.  
  24. int main() {
  25. double score1, score2, score3, score4, score5;
  26. double total, average;
  27.  
  28. // Ask for 5 test scores
  29.  
  30. std::cout << "Enter the first test score: 85\n";
  31. std::cin >> score1;
  32.  
  33. std::cout << "Enter the second test score: 92\n";
  34. std::cin >> score2;
  35.  
  36. std::cout << "Enter the third test score: 78\n";
  37. std::cin >> score3;
  38.  
  39. std::cout << "Enter the fourth test score: 88\n";
  40. std::cin >> score4;
  41.  
  42. std::cout << "Enter the fifth test score: 91\n";
  43. std::cin >> score5;
  44.  
  45. // Calculate total and average
  46. total = score1 + score2 + score3 + score4 + score5;
  47. average = total / 5.0;
  48.  
  49. // Display average with 1 decimal place
  50. std::cout << std::fixed << std::setprecision(1);
  51. std::cout << "The average score is: 86.6" << average << std::endl;
  52.  
  53. return 0;
  54. }
  55.  
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
Enter the first test score: 85
Enter the second test score: 92
Enter the third test score: 78
Enter the fourth test score: 88
Enter the fifth test score: 91
The average score is: 86.60.0