fork download
  1. // This program averages test scores. It asks the user for the
  2. // number of students and the number of test scores per student
  3. #include <iomanip>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. int main() {
  8. int numStudents;
  9. int numTests;
  10. double total;
  11. double average;
  12.  
  13. cout << fixed << showpoint << setprecision(1);
  14.  
  15. cout << "This program averages the number of test scores.\n";
  16. cout << "For how many students do you have scores? ";
  17. cin >> numStudents;
  18.  
  19. cout << "How many test scores does each student have? ";
  20. cin >> numTests;
  21.  
  22. for (int student = 1; student <= numStudents; student++)
  23. {
  24. total = 0;
  25. for (int test = 1; test <= numTests; test++)
  26. {
  27. double score;
  28. cout << "Enter Score " << test << " for ";
  29. cout << "student " << student << ": ";
  30. cin >> score;
  31. total += score;
  32. }
  33. average = total / numTests;
  34. cout << "The average score for student " << student;
  35. cout << " is " << average << ".\n\n";
  36. }
  37. return 0;
  38. }
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
This program averages the number of test scores.
For how many students do you have scores? How many test scores does each student have? The average score for student 1 is -nan.

The average score for student 2 is -nan.