Nested loops consist of an outer loop with one or more inner loops Each time the outer loop is...

17
NESTED LOOPS

Transcript of Nested loops consist of an outer loop with one or more inner loops Each time the outer loop is...

Page 1: Nested loops consist of an outer loop with one or more inner loops  Each time the outer loop is repeated, the inner loop starts from the beginning.

NESTED LOOPS

Page 2: Nested loops consist of an outer loop with one or more inner loops  Each time the outer loop is repeated, the inner loop starts from the beginning.

Nested loops consist of an outer loop with one or more inner loops

Each time the outer loop is repeated, the inner loop starts from the beginning.

1. WHAT ARE NESTED LOOPS

Dr. Soha S. Zaghloul 2

Page 3: Nested loops consist of an outer loop with one or more inner loops  Each time the outer loop is repeated, the inner loop starts from the beginning.

Analysis:1. How to calculate the total scores of a single

student?2. Repeat the same steps for 100 students.

2. EXAMPLE (1)

Dr. Soha S. Zaghloul 3

Write a complete program that calculates the total scores of each of 100 students in a course. The program should read first the student’s ID.

Page 4: Nested loops consist of an outer loop with one or more inner loops  Each time the outer loop is repeated, the inner loop starts from the beginning.

2. EXAMPLE (1) – CNT’D

Dr. Soha S. Zaghloul 4

Calculation of the total score of a student:#include <stdio.h>#define SENTINEL -999int main (void){ double score; // student’s score in an exam double sum; // accumulator to the scores of a single student char ID[15]; //student’s ID sum = 0.0; // initialize the accumulator score = 0.0; // initialize the loop control variable printf (“Enter Student ID> “); scanf (“%s”, ID); while (score != SENTINEL) { printf (“Enter student’s score> “); scanf (“%f”, &score); if (score != -999) sum += score; } // end while} // end main

Page 5: Nested loops consist of an outer loop with one or more inner loops  Each time the outer loop is repeated, the inner loop starts from the beginning.

2. EXAMPLE (1) – CNT’D

Dr. Soha S. Zaghloul 5

Repeat the program for 100 students#include <stdio.h>#define SENTINEL -999int main (void){ double score; // student’s score in an exam double sum; // accumulator to the scores of a single student char ID[15]; // student ID int i; // loop control variable for (i= 1; i <= 100; i++) { sum = 0.0; // initialize the accumulator score = 0.0; // initialize the loop control variable printf (“Enter Student ID> “); scanf (“%s”, ID); while (score != SENTINEL) { printf (“Enter student’s score> “); scanf (“%f”, &score); if (score != -999) sum += score; } // end while } // end for} // end main

Page 6: Nested loops consist of an outer loop with one or more inner loops  Each time the outer loop is repeated, the inner loop starts from the beginning.

Dr. Soha S. Zaghloul 6

3. EXAMPLE (1) – CHECK YOUR PARENTHESIS

#include <stdio.h>#define SENTINEL -999int main (void){ double score; // student’s score in an exam double sum; // accumulator to the scores of a single student char ID[15]; // student ID int i; // loop control variable for (i= 1; i <= 100; i++) { sum = 0.0; // initialize the accumulator score = 0.0; // initialize the loop control variable printf (“Enter Student ID> “); scanf (“%s”, ID); while (score != SENTINEL) { printf (“Enter student’s score> “); scanf (“%f”, &score); if (score != -999) sum += score; } // end while } // end for} // end main

printf (“Student’s total score= %f”, sum); ?

Page 7: Nested loops consist of an outer loop with one or more inner loops  Each time the outer loop is repeated, the inner loop starts from the beginning.

4. CORRECT NESTING LAYOUT

Dr. Soha S. Zaghloul 7

Figure 1 Figure 2

The brace opened first closes last: CORRECT

Figure 3 Figure 4

Intersecting braces: WRONG

Page 8: Nested loops consist of an outer loop with one or more inner loops  Each time the outer loop is repeated, the inner loop starts from the beginning.

Analysis:1. Calculate the total scores of a single student in a single course?2. Repeat the same steps for 5 courses3. Repeat the whole program for 100 students.

5. EXAMPLE (2)

Dr. Soha S. Zaghloul 8

Write a complete program that calculates the total scores of each of 100 students in each of five courses.

Page 9: Nested loops consist of an outer loop with one or more inner loops  Each time the outer loop is repeated, the inner loop starts from the beginning.

5. EXAMPLE (2) – CNT’D

Dr. Soha S. Zaghloul 9

Calculation of the total score of a student in one course

#include <stdio.h>#define SENTINEL -999int main (void){ double score; // student’s score in an exam double sum; // accumulator to the scores of a single student char ID[12]; //student’s ID char code[7]; // course code sum = 0.0; // initialize the accumulator score = 0.0; // initialize the loop control variable printf (“Enter Student ID> “); scanf (“%s”, ID); printf (“Enter Course Code> “); scanf (“%s”, code); while (score != SENTINEL) { printf (“Enter student’s score> “); scanf (“%f”, &score); if (score != -999) sum += score; } // end while printf (“Total score in course %s = %f”, code, score);} // end main

Page 10: Nested loops consist of an outer loop with one or more inner loops  Each time the outer loop is repeated, the inner loop starts from the beginning.

5. EXAMPLE (2) – CNT’D

Dr. Soha S. Zaghloul 10

Calculation of the total score of a student in five courses

#include <stdio.h>#define SENTINEL -999int main (void){ double score, sum; int course; // course counter char ID[12], code[7]; printf (“Enter Student ID> “); scanf (“%s”, ID); sum = 0.0; // initialize the accumulator for (course = 1; course <= 5; course++) { score = 0.0; // initialize the loop control variable printf (“Enter Course Code> “); scanf (“%s”, code); while (score != SENTINEL) { printf (“Enter student’s score> “); scanf (“%f”, &score); if (score != -999) sum += score; } // end while printf (“Total score for student %s in course %s = %f \n”, ID, code, score); } // end for (course=…} // end main

Page 11: Nested loops consist of an outer loop with one or more inner loops  Each time the outer loop is repeated, the inner loop starts from the beginning.

5. EXAMPLE (2) – CNT’D

Dr. Soha S. Zaghloul 11

Repeat the program for 100 students#include <stdio.h>#define SENTINEL -999int main (void){ double score, sum; int course; // course counter int student; // student counter char ID[12], code[7]; for (student = 1; student <= 100; student++) { printf (“Enter Student ID> “); scanf (“%s”, ID); sum = 0.0; for (course = 1; course <= 5; course++) { score = 0.0; printf (“Enter Course Code> “); scanf (“%s”, code); while (score != SENTINEL) { printf (“Enter student’s score> “); scanf (“%f”, &score); if (score != -999) sum += score; } // end while printf (“Total score for student %s in course %s = %f \n”, ID, code, score); } // end for (course=… } // end for(student…} // end main

Page 12: Nested loops consist of an outer loop with one or more inner loops  Each time the outer loop is repeated, the inner loop starts from the beginning.

Analysis:How to calculate the GPA? 1. The maximum score total of each course is 100 The maximum score for 4 courses is 400.

Calculate it out of 5 as follows:Example: the student got 320 out of 400 in all

courses. maximum: 400 5

actual score: 320 GPAGPA = (320 * 5) / 400 = 4.0 out of 5

6. EXAMPLE (3)

Dr. Soha S. Zaghloul 12

Update the program written in Example (2) so that to calculate the grade average point (GPA) of each student. The total of each course is calculated out of 100.

Page 13: Nested loops consist of an outer loop with one or more inner loops  Each time the outer loop is repeated, the inner loop starts from the beginning.

6. EXAMPLE (3) – CNT’D

Dr. Soha S. Zaghloul 13

#include <stdio.h>#define SENTINEL -999int main (void){ double score, sum; int course, student; // counter double GPA; //Grade Point Average char ID[12], code[7]; for (student = 1; student <= 100; student++) { printf (“Enter Student ID> “); scanf (“%s”, ID); sum = 0.0; for (course = 1; course <= 5; course++) { score = 0.0; printf (“Enter Course Code> “); scanf (“%s”, code); while (score != SENTINEL) { printf (“Enter student’s score> “); scanf (“%f”, &score); if (score != -999) sum += score; } // end while printf (“Total score for student %s in course %s = %f \n”, ID, code, score); } // end for (course=… GPA = (sum * 5) / 500; // number of courses = 5 printf (“GPA of student %s = %f”, ID, GPA); } // end for(student…} // end main

Page 14: Nested loops consist of an outer loop with one or more inner loops  Each time the outer loop is repeated, the inner loop starts from the beginning.

7. SELF-CHECK EXERCISE (1)

Dr. Soha S. Zaghloul 14

Update the program written in Example (3) so that to do the following:1. The user ends the courses entry using a sentinel2. The user ends the data entry of students using a sentinel3. Calculate the GPA of each student accordingly4. Calculate the average of GPAs for all students

Page 15: Nested loops consist of an outer loop with one or more inner loops  Each time the outer loop is repeated, the inner loop starts from the beginning.

8. SELF-CHECK EXERCISE (2)

Dr. Soha S. Zaghloul 15

Show the output displayed by the following nested loops:

for (i = 0; i < 2; ++i) { printf (“outer %4d\n”, i); for (j = 0; j < 3; ++j) { printf (“~~~~~inner%3d%3d\n”, i, j); } for (k = 2; k > 0; --k) printf (“~~~~~inner%3d%3d\n”, i, k); }

Page 16: Nested loops consist of an outer loop with one or more inner loops  Each time the outer loop is repeated, the inner loop starts from the beginning.

9. SELF-CHECK EXERCISE (3)

Dr. Soha S. Zaghloul 16

Write a program that displays the multiplication table for numbers 0 to 9.

Page 17: Nested loops consist of an outer loop with one or more inner loops  Each time the outer loop is repeated, the inner loop starts from the beginning.

10. SELF-CHECK EXERCISE (4)

Dr. Soha S. Zaghloul 17

Design an interactive input loop that scans pairs of integers until it reaches a pair in which the first integer evenly divides the second.