Presentation is loading. Please wait.

Presentation is loading. Please wait.

Nested LOOPS.

Similar presentations


Presentation on theme: "Nested LOOPS."— Presentation transcript:

1 Nested LOOPS

2 Nested loops consist of an outer loop with one or more inner loops
1. WHAT ARE NESTED LOOPS 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 2

3 How to calculate the total scores of a single student?
2. Example (1) 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. Analysis: How to calculate the total scores of a single student? Repeat the same steps for 100 students. Dr. Soha S. Zaghloul 3

4 Calculation of the total score of a student:
2. Example (1) – cnt’d Calculation of the total score of a student: #include <stdio.h> #define SENTINEL -999 int 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 Dr. Soha S. Zaghloul 4

5 Repeat the program for 100 students
2. Example (1) – cnt’d #include <stdio.h> #define SENTINEL -999 int 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 Repeat the program for 100 students Dr. Soha S. Zaghloul 5

6 3. Example (1) – check your parenthesis
#include <stdio.h> #define SENTINEL -999 int 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); ? Dr. Soha S. Zaghloul 6

7 4. Correct nesting layout
Figure 1 Figure 2 Figure 3 Figure 4 The brace opened first closes last: CORRECT Intersecting braces: WRONG Dr. Soha S. Zaghloul 7

8 5. Example (2) Write a complete program that calculates the total scores of each of 100 students in each of five courses. Analysis: Calculate the total scores of a single student in a single course? Repeat the same steps for 5 courses Repeat the whole program for 100 students. Dr. Soha S. Zaghloul 8

9 Calculation of the total score of a student in one course
5. Example (2) – cnt’d Calculation of the total score of a student in one course #include <stdio.h> #define SENTINEL -999 int 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 Dr. Soha S. Zaghloul 9

10 5. Example (2) – cnt’d Calculation of the total score of a student in five courses #include <stdio.h> #define SENTINEL -999 int 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 Dr. Soha S. Zaghloul 10

11 Repeat the program for 100 students
5. Example (2) – cnt’d Repeat the program for 100 students #include <stdio.h> #define SENTINEL -999 int 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 Dr. Soha S. Zaghloul 11

12 How to calculate the GPA?
6. Example (3) 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. 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: actual score: GPA GPA = (320 * 5) / 400 = 4.0 out of 5 Dr. Soha S. Zaghloul 12

13 6. Example (3) – cnt’d #include <stdio.h> #define SENTINEL -999
int 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 Dr. Soha S. Zaghloul 13

14 7. self-check exercise (1)
Update the program written in Example (3) so that to do the following: The user ends the courses entry using a sentinel The user ends the data entry of students using a sentinel Calculate the GPA of each student accordingly Calculate the average of GPAs for all students Dr. Soha S. Zaghloul 14

15 8. self-check exercise (2)
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); Dr. Soha S. Zaghloul 15

16 9. self-check exercise (3)
Write a program that displays the multiplication table for numbers 0 to 9. Dr. Soha S. Zaghloul 16

17 10. self-check exercise (4)
Design an interactive input loop that scans pairs of integers until it reaches a pair in which the first integer evenly divides the second. Dr. Soha S. Zaghloul 17


Download ppt "Nested LOOPS."

Similar presentations


Ads by Google