Presentation is loading. Please wait.

Presentation is loading. Please wait.

DCT 1123 Problem Solving & Algorithms

Similar presentations


Presentation on theme: "DCT 1123 Problem Solving & Algorithms"— Presentation transcript:

1 DCT 1123 Problem Solving & Algorithms
Nested Loop & Combination of selection & repetition

2 What is nested loop? A nested loop is a loop within a loop, an inner loop within the body of an outer one. The first pass of the outer loop triggers the inner loop, which executes to completion. The second pass of the outer loop triggers the inner loop again. This repeats until the outer loop finishes. Of course, a break within either the inner or outer loop would interrupt this process.

3 Syntax while(condition) { { statement(s); } statement(s); // you can put more statements. }

4 Combination of Selection & Repetition (Sample Problem)
Design an algorithm that will prompt for and receive pairs of numbers from an operator at a terminal and display their sum, product & average on the screen. If the calculated sum is over 200, an asterisk is to be displayed beside the sum. The program is terminate when a pair of zero values is entered.

5 Combination of Selection & Repetition (Sample Problem – Defining Diagram)
Input Processing Output Number_1 Prompt for numbers Sum Number_2 Get two numbers Product Calculate sum Average Calculate product ‘*’ Calculate average Display sum, product, average Display ‘*’

6 Combination of Selection & Repetition (Sample Problem – Solution Algorithms)
Set sum = 0 Input number1, number2 WHILE (number1 != 0 AND number2 != 0) sum = number1+number2 product = number1*number2 average = sum / 2 If sum > 200 THEN Display sum, ‘*’, product, average ELSE Display sum, product, average ENDIF ENDDO END

7 Sample Problem Design an algorithm that will prompt for and receive your age in years and months and calculate and display your age in months. If the calculated months figure is more than 500, three asterisks should also appear beside the month figure. Your program is to continue processing until a sentinel of 999 is entered.

8 Sample Problem A file student records contains 100 name and their gender (M or F). Design an algorithm that will read through the file and calculate the number of male (M) and female (F) in the records.


Download ppt "DCT 1123 Problem Solving & Algorithms"

Similar presentations


Ads by Google