0% found this document useful (0 votes)
14 views28 pages

Topic 8 - Problem Solving Concepts - Part 4

Uploaded by

sara yamen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views28 pages

Topic 8 - Problem Solving Concepts - Part 4

Uploaded by

sara yamen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Dr. Awad Khalil Dr.

Mohamed Hussein
Faculty of Computers & Information Technology
Outline
 Repetitive Structure (Loops)
Basic Algorithmic Structures
Design of an Algorithm
 An algorithm is written as a step-by-step procedure
(sequence) in which choices can be made where
necessary (selection), and all or part of the
algorithm can be repeated (repetition).

 Thus, the basic structures of an algorithm are:


1- sequence
2- selection
3- repetition

 There several methods to represent an algorithm:

 Pseudocode
 Flow Chart
 Programming Languages

3
Repetition
Write an algorithm that will display
the numbers 1 - 10.
Using the knowledge you currently
possess, you would most likely write a
program that uses individual lines of
code that print out each number.  Display 1
 Display 2
 The pseudo code for such  Display 3
an answer looks like this.
 Display 4
 But, this not a practical
 Display 5
efficient algorithm,
 Display 6
especially, if the problem
is changed to display the  Display 7
numbers 1 – 100.  Display 8
 The algorithm for such  Display 9
problem is solved using:  Display 10
iteration, also known as
Repetition or loops.
Repetition

Simple Assignment Operators

count = count + 1;

time = time -1;

product = product * item;

total = total / number;

n = n % m;

5-5
Repetition – while loop
The while loop has the following structure:

while (loop repetition condition)


statement-list

 Loop repetition condition is the condition which


controls the loop.
 The statement-list is repeated as long as the loop
repetition condition is true.
 A loop is called an infinite loop if the loop
repetition condition is always true and never
reached to false.
 while loop is a pre-test loop as it may never
executed as the test of the condition is done
before making any round.
Repetition – while loop

while (Condition)
{
Statement list
T Statement }
Condition
list

7
Repetition – while loop
The algorithm to display the numbers 1-10, using a while
loop, would be as follows:

Algorithm
step1: x = 1
Step2: while ( x <= 100 )
{
OUTPUT x
x = x + 1
}
step3: STOP

 The variable x should be first initialized by 1


 The statements list between {} will be repeated as long
as the condition (x <= 10) is true.
 The variable x should be incremented every time the
loop is executed.
 step 1 x = 1
 step 2 while ( x <= 10)
 { output x
 x=x+2 }
 step 3 stop
1-start
2-N=1
3-while (N<=60)
{read mark
If(mark>=50)
Display “pass”
Else display “fail”
N=N+1
}
4-stop
 1-start
 2-input n=1
 3-while (n<=30)
 {input grade
If(grade>=50) then print “Pass” else print “Fail”
n=n+1
}
 4-stop
 1-start
 2-input total=0, n=1
 3- while (n<=10)
 { input price
total=total+price
n=n+1
}
4-print total
5-stop
Repetition – do-while loop
The do-while loop has the following structure:

do
{
statement-list
} while (loop repetition condition)

 Loop repetition condition is the condition which


controls the loop.
 The statement-list is repeated as long as the loop
repetition condition is true.
 A loop is called an infinite loop if the loop repetition
condition is always true and never reach to false.
 do-while loop is a post-test loop as it will be executed
at least once.
Repetition – do-while loop
The algorithm to display the numbers 1-10, using a do-
while loop, would be as follows:

Algorithm
step1: x = 1
Step2: do
{
OUTPUT x
x = x + 1
}while(x <= 10)
step3: STOP

 The variable x should be first initialized by 1


 The statements list between {} will be repeated as long
as the condition (x <= 100) is true.
 The variable x should be incremented every time the
loop is executed.
1- start
2- x=1, total=0
3- while(x<=100)
{input N, P
price=N*P
total=total+price
x=x+1
}
4- display total
5-if (total>10000) then total=total*0.8
6-display total
7- stop
 1- start
 2- x=1
 3- while(x<=100)
 { input N, R
 if (N<=40)
 Wage=N*R
 else
 Wage=40*R+(N-40)*2*R
 Display Wage
 x=x+1
 }
 4- stop
Repetition
Problem Statement:
Write an algorithm that prints the summation of a set of
integers entered by the user. the user will stop entering
the integers by input -1

Discussion
Without loops, solving this problem is impossible: In
essence, (without loops) you would need to have an
infinite number of read statements all in a row to perform
this task. Instead, we notice that as long as the user has
not entered –1, repeat the addition and read statements.
Remember, always look for indications that you will be
repeating something.
Repetition
The algorithm that prints the summation of a set of integers
entered by the user. The user will stop entering the integers by
input -1

Algorithm
step1: sum = 0
step2: INPUT x
step3: while ( x != -1)
{
sum = sum + x
INPUT x
}
step4: OUTPUT sum
step5: STOP

 The variable sum should be first initialized by 0


 The statements list between {} will be repeated as long as the
condition (x != -1) is true.
 The value -1 of the variable x is called a sentinel value that will
stop repetition of the loop.
Repetition
Important Notes
 Notice how we read user input once outside the loop and then again
inside the loop. The reason we do this is because the decision (x != -
1?) wouldn’t make any sense if x did not have a value.

 Before this decision can be made, you must input the first number
(outside the loop). If the condition is true, we can add this number to
the sum and read the next one. The second and subsequent times
user input is read, it is done inside the loop.

 The second important note is the (sum = 0) statement at the


beginning of the program. It is a very good idea to set all of your
variables in a program to some initial value before you use them;
if they are not given values by you, they will be left with whatever
“garbage” value the computer had stored in its place. This is
especially true of “accumulators” (variables which are used to
accumulate a value, such as a counter or a sum).

 For instance, if you remove the (sum = 0) statement from the


program and the computer had the number –5515 stored in sum’s
memory location, the sum would be meaningless (if the user
entered the first number as 5, the updated sum would then read –
5510 [-5515 + 5]).
Repetition
Write an algorithm to compute
and print the average of input
ages.

Step1: Sum = 0, Counter = 0


Step2: INPUT age
Step3: while (age <> 0)
{
Sum = Sum + age
Counter = Counter + 1
INPUT age
}

Step4: Average = Sum / Counter


Step5: OUTPUT Average
Step6: STOP
Repetition
Write an algorithm that calculates summation of integers
from a to b

Step1: Sum = 0
Step2: INPUT a, b
Step3: while (a <= b)
{
Sum = Sum + a
a = a + 1
}

Step4: OUTPUT Sum


Step5: STOP
Repetition
Write an algorithm that calculates the summation of the
even integers from a to b

Step1: Sum = 0
Step2: INPUT a, b
Step3: while (a <= b)
{
if ( a % 2 == 0 )
Sum = Sum + a
a = a + 1
}

Step4: OUTPUT Sum


Step5: STOP
 1- count=1
 2-While (count<=100)
 { input N,R
 Salary=N*R
 output salary
 count=count+1
}
 3-stop
Step1: sum=0,x=1
Step2: while(x<=1000)
{sum=sum+x
x=x+1
}
Step3: output sum
Step4: output average=sum/1000
Step5:stop
Repetition

Exercise

1. Write an algorithm that calculates the average of


the first 1000 natural numbers (Note: Natural
numbers are the positive numbers starting from 1).

2. Write an algorithm that calculates the total profit of


100 products in a store.

3. Write an algorithm that calculates the average price


of the products in a store.
step1: sum=0 , x=1

step2: while(x<=1000)

{ sum=sum+x

x=x+1 }

step3: output sum

step4: average=sum/1000

Step5 :output average

step6: stop
 1-start
 2-x=1 , input grade, sum=0
 3-while(x<=100)
 {sum=sum + grade
 Input grade
 x=x+1
 }
4- average=sum/100
5-display average.
6-stop

You might also like