0% found this document useful (0 votes)
9 views49 pages

CMPE113 Lecture3

The document outlines various algorithm exercises, including flowchart creation and algorithm writing for tasks such as summing numbers, calculating letter grades, finding the greatest and smallest of three numbers, and computing averages. Each exercise includes steps for input, process, and output, along with specific algorithms and flowcharts. The content is structured for educational purposes, aimed at teaching algorithm design and flowcharting techniques.

Uploaded by

armangezerer1
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)
9 views49 pages

CMPE113 Lecture3

The document outlines various algorithm exercises, including flowchart creation and algorithm writing for tasks such as summing numbers, calculating letter grades, finding the greatest and smallest of three numbers, and computing averages. Each exercise includes steps for input, process, and output, along with specific algorithms and flowcharts. The content is structured for educational purposes, aimed at teaching algorithm design and flowcharting techniques.

Uploaded by

armangezerer1
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/ 49

Algorithm Exercises

Asst. Prof. Dr. Damla TOPALLI,


Atilim University
Recall: Flowcharts
Start/End Input OR

Compute Output

Decision

10/10/2021 Asst. Prof. Dr. Damla Topallı 2


Recall: Exercise from Week-2
Write an algorithm and draw its flowchart to add 5
numbers. The numbers are to be input one at a time.

• What are the inputs?


• What will be the process?
• What is the output?

10/10/2021 Asst. Prof. Dr. Damla Topallı 3


Recall: Exercise from Week-2

INPUT PROGRAM OUTPUT

5 Numbers Sum of 5 numbers


Calculate Sum

10/10/2021 Asst. Prof. Dr. Damla Topallı 4


Recall: Exercise from Week-2 Lets’ use: S for Sum
C for Count
Algorithm: X for the input number
Step1. Start Rewrite the algorithm.
Step2. Initialize sum=0 and count=0
S1: Start
Step3. Input number
S2: S 0, C0
Step4. Add number to sum
S3: Input X
Step5. Increment the counter by 1.
S4: S S + X
Step6. If count<5 go to Step3
S5: C C+1
else
S6:If C<5, then go to S3.
Print sum
else print S
Step7. Stop
S7: Stop

10/10/2021 Asst. Prof. Dr. Damla Topallı 5


Solution Flowchart:

S1: Start
S2: S 0, C0
S3: Input X
S4: S S + X
S5: C C+1
S6:If C<5, then go
to S3.
else print S
S7: Stop
10/10/2021 Asst. Prof. Dr. Damla Topallı 6
10/10/2021 Asst. Prof. Dr. Damla Topallı 7
Exp.1 Calculate Letter Grade
Write an algorithm and draw its
flowchart to find the letter grade of
a student’s grade out of 100.

10/10/2021 Asst. Prof. Dr. Damla Topallı 8


Calculate Letter Grade

INPUT PROGRAM OUTPUT

grade Show Letter grade


Calculate Letter grade

10/10/2021 Asst. Prof. Dr. Damla Topallı 9


Calculate Letter Grade
1. Start
2. Read the grade
3. If grade < 50 then set letter_grade = FF go to step 12
4. Else If grade < 60 then set letter_grade = FD go to step 12
5. Else If grade < 65 then set letter_grade = DD go to step 12
6. Else If grade < 70 then set letter_grade = DC go to step 12
7. Else If grade < 75 then set letter_grade = CC go to step 12
8. Else If grade < 80 then set letter_grade = CB go to step 12
9. Else If grade < 85 then set letter_grade = BB go to step 12
10. Else If grade < 90 then set letter_grade = BA go to step 12
11. Else set letter_grade = AA
12. Print letter grade
13. stop
10/10/2021 Asst. Prof. Dr. Damla Topallı 10
1. Start
2. Read the grade
3. If grade < 50 then set letter_grade = FF goto step 12
4. Else If grade < 60 then set letter_grade = FD goto step 12
5. Else If grade < 65 then set letter_grade = DD goto step 12
6. Else If grade < 70 then set letter_grade = DC go to step 12
7. Else If grade < 75 then set letter_grade = CC go to step 12

8. Else If grade < 80 then set letter_grade = CB go to step 12

9. Else If grade < 85 then set letter_grade = BB go to step 12

10. Else If grade < 90 then set letter_grade = BA go to step 12

11. Else set letter_grade = AA


12. Print letter grade

13. stop

10/10/2021 Asst. Prof. Dr. Damla Topallı 11


1. Start
2. Read the grade
3. If grade < 50 then set letter_grade = FF go to step 12
4. Else If grade < 60 then set letter_grade = FD go to step 12
5. Else If grade < 65 then set letter_grade = DD go to step 12
6. Else If grade < 70 then set letter_grade = DC go to step 12
7. Else If grade < 75 then set letter_grade = CC go to step 12
8. Else If grade < 80 then set letter_grade = CB go to step 12
9. Else If grade < 85 then set letter_grade = BB go to step 12
10. Else If grade < 90 then set letter_grade = BA go to step 12
11. Else set letter_grade = AA
12. Print letter grade
13. stop

10/10/2021 Asst. Prof. Dr. Damla Topallı 12


10/10/2021 Asst. Prof. Dr. Damla Topallı 13
Exp.2 Gratest of three Numbers
• Draw a flowchart to find the greatest number among
the three numbers.

• What are the inputs?


• What will be the process?
• What is the output?

10/10/2021 Asst. Prof. Dr. Damla Topallı 14


Gratest of three Numbers

INPUT PROGRAM OUTPUT

Three number Show greatest


Find greatest

10/10/2021 Asst. Prof. Dr. Damla Topallı 15


Gratest of three Numbers
1.Start
2.Input three numbers (A, B, C)
3.Is A > B and A > C , display A is
Largest, Go to Step 5
4.Is B > A and B > C, display B is
Largest, Go to Step 5
5.Else display C is Largest
6.Stop

10/10/2021 Asst. Prof. Dr. Damla Topallı 16


Gratest of three Numbers
1.Start
2.Input three numbers (A, B, C)
3.Is A > B and A > C , display A is
Largest, Go to Step 5
4.Is B > A and B > C, display B is
Largest, Go to Step 5
5.Else display C is Largest
6.Stop

10/10/2021 Asst. Prof. Dr. Damla Topallı 17


10/10/2021 Asst. Prof. Dr. Damla Topallı 18
Exp 3. Smallest of three Numbers
Write the algorithm and draw the flowchart to find the
smallest number among the three numbers.

• What are the inputs?


• What will be the process?
• What is the output?

10/10/2021 Asst. Prof. Dr. Damla Topallı 19


Smallest of three Numbers

INPUT PROGRAM OUTPUT

Three number Show Smallest


Find smallest

10/10/2021 Asst. Prof. Dr. Damla Topallı 20


Smallest of three Numbers
1.Start
2.Input three numbers (A, B, C)
3.Is A < B and A < C , display A is
Smallest, Go to Step 5
4.Is B < A and B < C, display B is
Smallest, Go to Step 5
5.Else display C is Smallest
6.Stop

10/10/2021 Asst. Prof. Dr. Damla Topallı 21


Smallest of three Numbers

1.Start
2.Input three numbers (A, B, C)
3.Is A < B and A < C , display A is
Smallest, Go to Step 5
4.Is B < A and B < C, display B is
Smallest, Go to Step 5
5.Else display C is Smallest
6.Stop
10/10/2021 Asst. Prof. Dr. Damla Topallı 22
10/10/2021 Asst. Prof. Dr. Damla Topallı 23
Exp 4. Average of 25 Scores
Write the algorithm and draw the flowchart to calculate
the average from 25 exam scores

• What are the inputs?


• What will be the process?
• What is the output?

10/10/2021 Asst. Prof. Dr. Damla Topallı 24


Average of 25 Scores

INPUT PROGRAM OUTPUT

25 scores Message average


Calculate average

10/10/2021 Asst. Prof. Dr. Damla Topallı 25


Average of 25 Scores
1. Start
2. Sum = 0, C = 0
3. Read score (S)
4. Sum = sum + S
5. C=C+1
6. If C NOT equal to 25 go to step 3
7. Av = sum / 25
8. Print av
9. Stop
10/10/2021 Asst. Prof. Dr. Damla Topallı 26
Average of 25 Scores
1. Start
2. Sum = 0, C = 0
3. Read score (S)
4. Sum = sum + S
5. C=C+1
6. If C NOT equal to 25 go to step 3
7. Av = sum / 25
8. Print av
9. Stop
10/10/2021 Asst. Prof. Dr. Damla Topallı 27
10/10/2021 Asst. Prof. Dr. Damla Topallı 28
Exp 5. Odd or Even
Write an algorithm and draw a flowchart to finding a
given number is odd or even.

• What are the inputs?


• What will be the process?
• What is the output?

10/10/2021 Asst. Prof. Dr. Damla Topallı 29


Odd or Even

INPUT PROGRAM OUTPUT

number Check number is Message if


even or odd even or odd

10/10/2021 Asst. Prof. Dr. Damla Topallı 30


Odd or Even
1. Start
2. Enter a Number (N)
3. Calculate reminder R by N % 2 (modulo operation: N mod 2)
4. If R equal to zero, display number is Even
5. Else display number is Odd
6. Stop

10/10/2021 Asst. Prof. Dr. Damla Topallı 31


Odd or Even
1. Start
2. Enter a Number (N)
3. Calculate reminder R by N % 2 (modulo
operation: N mod 2)
4. If R equal to zero, display number is Even
5. Else display number is Odd
6. Stop

10/10/2021 Asst. Prof. Dr. Damla Topallı 32


10/10/2021 Asst. Prof. Dr. Damla Topallı 33
Exp 6. Sum odd numbers
Write and algorithm and draw a flowchart to find the sum of odd
integers from 1 to 99 inclusive.

10/10/2021 Asst. Prof. Dr. Damla Topallı 34


Sum odd numbers

INPUT PROGRAM OUTPUT


Show sum
No input
Calculate sum

10/10/2021 Asst. Prof. Dr. Damla Topallı 35


Sum odd numbers
Write and algorithm and draw a flowchart to find the sum of odd
integers from 1 to 99 inclusive.
1. Start
2. Sum = 0, number = 1
3. Sum = sum + number
4. Number = number + 2
5. If number less than 100 go to step 3
6. Print sum
7. Stop

10/10/2021 Asst. Prof. Dr. Damla Topallı 36


Sum odd numbers
1. Start
2. Sum = 0, number = 1
3. Sum = sum + number
4. Number = number + 2
5. If number less than 100 go to step 3
6. Print sum
7. Stop

37
10/10/2021 Asst. Prof. Dr. Damla Topallı
Write and algorithm and draw a flowchart
to find the sum of odd integers from 1 to
99 inclusive.
1. Start
2. Sum = 0, number = 1
3. Sum = sum + number
4. Number = number + 2
5. If number less than 100 go to step 3
6. Print sum
7. Stop

10/10/2021 Asst. Prof. Dr. Damla Topallı 38


10/10/2021 Asst. Prof. Dr. Damla Topallı 39
Exp 7. Factorial
Write an algorithm and draw a flowchart that will input a positive
integer and will find the factorial of this integer.

10/10/2021 Asst. Prof. Dr. Damla Topallı 40


Factorial

INPUT PROGRAM OUTPUT

N Show factorial
Calculate factorial

10/10/2021 Asst. Prof. Dr. Damla Topallı 41


Factorial
Write an algorithm and draw a flowchart that will input a positive integer
and will find the factorial of this integer.
1. Start
2. Read Number
3. Set fact=1, i=1
4. Check if i<=Number if false go to step 7
5. Fact=Fact*i;
6. i=i+1
7. Display Fact
8. Stop

10/10/2021 Asst. Prof. Dr. Damla Topallı 42


Factorial
Write an algorithm and draw a flowchart that will
input a positive integer and will find the factorial of
this integer.
1. Start
2. Read Number
3. Set fact=1, i=1
4. Check if i<=Number if false go to step 7
5. Fact=Fact*i;
6. i=i+1
7. Display Fact
8. Stop

10/10/2021 Asst. Prof. Dr. Damla Topallı 43


10/10/2021 Asst. Prof. Dr. Damla Topallı 44
Exp 8. Calculate F
Write an algorithm and draw its flowchart to compute the
function F below for an unknown number of set of data.
Each number is given on a separate input record. The last
data record has the value 555 and defines the end of data.
This is called a sentinel value. Do not include this value in
the calculations.
F = x2 -1 if x <= 0
square route of (x) if 0 < x < 1
x if x >= 1

10/10/2021 Asst. Prof. Dr. Damla Topallı 45


Calculate F

INPUT PROGRAM OUTPUT


x Message F
Calculate F

10/10/2021 Asst. Prof. Dr. Damla Topallı 46


Calculate F
Algorithm
1. Start
2. Input X
3. If X=555 then Go to step 9.
4. If X <=0 then F= X2 +1
5. If 0 < x < 1 then F= 𝑋
6. If x >= 1 then F=X
7. Output X and F
8. Go to Step 2
9. Stop
10/10/2021 Asst. Prof. Dr. Damla Topallı 47
Calculate F
Algorithm
1. Start
2. Input X
3. If X=555 then Go to step 7.
4. If X <=0 then F= X2 +1
5. If 0 < x < 1 then F= 𝑋
6. If x >= 1 then F=X
7. Output X and F
8. Go to Step 2
9. Stop
10/10/2021 Asst. Prof. Dr. Damla Topallı 48
Calculate F
Algorithm How it works? Trace for the values:
X F
0.25
-4
20
555

10/10/2021 Asst. Prof. Dr. Damla Topallı 49

You might also like