{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Nested Loop Handout f2007

# Nested Loop Handout f2007 - Nested Loop Example A int main...

This preview shows pages 1–3. Sign up to view the full content.

Nested Loop Example A int main() { int numStudents, // Number of students numTests, // Number of tests per student total; // Accumulates total score for each student double average; // Average test score for each student cout << "This program averages test scores.\n" ; cout << "How many students are there? " ; cin >> numStudents; cout << "How many test scores does each student have? " ; cin >> numTests; cout << endl; for ( int student = 1; student <= numStudents; student++) // Outer loop { total = 0; for ( int test = 1; test <= numTests; test++) // Inner loop { int score; cout << "Enter score " << test << " for " << "student " << student << ": " ; cin >> score; total += score; } // End of inner loop average = total / numTests; cout << "The average for student " << student; cout << " is " << average << ".\n\n" ; } // End of outer loop return 0; }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Nested Loop Example B int main() { int numStudents, // Number of students numTests, // Number of tests per student total; // Accumulates total score for each student double average; // Average test score for each student int score; int student, test; cout << "This program averages test scores.\n" ; cout << "How many students are there? " ; cin >> numStudents; for
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 4

Nested Loop Handout f2007 - Nested Loop Example A int main...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online