Each time the outer loop is repeated the inner loop starts from the beginning 1 WHAT ARE NESTED LOOPS Dr Soha S Zaghloul 2 Analysis How to calculate the total scores of a single student ID: 632248
Download Presentation The PPT/PDF document "Nested LOOPS Nested loops consist of an ..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Nested LOOPSSlide2
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
Slide3
Analysis:
How to calculate the total scores of a single student?
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.Slide4
2
. Example (1) –
cnt’d
Dr. Soha S. Zaghloul
4
Calculation of the total score of a student:
#include <
stdio.h
>
#define SENTINEL -999
i
nt
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
Slide5
2
. Example (1) –
cnt’d
Dr. Soha S. Zaghloul
5
Repeat the program for 100 students
#include <
stdio.h
>
#define SENTINEL -999
i
nt
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
Slide6
Dr. Soha S. Zaghloul
6
3. Example (1) – check your parenthesis
#include <
stdio.h
>#define SENTINEL -999
i
nt
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
p
rintf
(“Student’s total score= %f”, sum);
?Slide7
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: WRONGSlide8
Analysis:
Calculate the total scores of a single student in a single course?
Repeat the same steps for 5 coursesRepeat 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
. Slide9
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 -999
i
nt
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
Slide10
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 -999
i
nt
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
Slide11
5. Example (2) –
cnt’d
Dr. Soha S. Zaghloul
11
Repeat the program for 100 students
#include <
stdio.h
>
#define SENTINEL -999
i
nt
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 Slide12
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 56. 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.Slide13
6
. Example (3) –
cnt’d
Dr. Soha S. Zaghloul
13
#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 Slide14
7. self-check exercise (1)
Dr. Soha S. Zaghloul
14
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 studentsSlide15
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);
}Slide16
9. self-check exercise (3)
Dr. Soha S. Zaghloul
16
Write a program that displays the multiplication table for numbers 0 to 9.Slide17
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.