Nested LOOPS.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Nested Loops. Nested loops Just as a selection structure can be nested within another selection structure (or within a loop), a loop can also be nested.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
7. C program structure.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
A First Book of ANSI C Fourth Edition
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.
Python Repetition. We use repetition to prevent typing the same code out many times and to make our code more efficient. FOR is used when you know how.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
C Lecture Notes 1 Structured Program Development.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Iterations Very Useful: Ability to repeat a block of code Example:
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
EXAMPLE. Dr. Soha S. Zaghloul2 Write a complete program that searches for all the elements that are multiple of 7 in array X of type int and size 100.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Problem Solving and Program Design in C Chap. 5 Repetition and Loop Statement Chow-Sing Lin.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
REEM ALAMER REVISION FOR C LANGUAGE COURSE. OUTPUTS int main (void) { int C1, C2; int *p1, *p2; C1 = 8; p1 = &C1; C2 = *p1 / 2 + 5; p2 = &C2; printf ("C1.
Repetition statements
ARRAYS.
Decision making If.. else statement.
while Repetition Structure
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
REPETITION STATEMENTS
FUNCTIONS EXAMPLES.
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Looping.
Repetition and Loop Statements
The while Looping Structure
INPUT & OUTPUT scanf & printf.
Functions.
Structured Program
1) C program development 2) Selection structure
Chapter 3 - Structured Program Development
The while Looping Structure
The ‘while’ loop ‘round and ‘round we go.
Decision making If statement.
Chapter 3 - Structured Program Development
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
Week 6 CPS125.
CS150 Introduction to Computer Science 1
REPETITION STATEMENTS
Let’s all Repeat Together
ECE 103 Engineering Programming Chapter 19 Nested Loops
EPSII 59:006 Spring 2004.
Functions Extra Examples.
EECE.2160 ECE Application Programming
The while Looping Structure
The while Looping Structure
ICS103: Programming in C 5: Repetition and Loop Statements
Presentation transcript:

Nested LOOPS

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

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

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

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

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

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

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

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

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

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

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: 400 5 actual score: 320 GPA GPA = (320 * 5) / 400 = 4.0 out of 5 Dr. Soha S. Zaghloul 12

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

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

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

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

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