0% found this document useful (0 votes)
101 views

Problem Solving: Algorithms and Flowcharts: CSC 110-Introduction To Computer Systems

Here are the steps to solve this problem: 1. Initialize two variables, sum_even and sum_odd, to store the running totals of even and odd integers 2. Use a for loop to iterate 15 times 3. Inside the loop: - Read an integer - Use the modulo operator (%) to check if it is even or odd - Add the integer to the appropriate running total variable 4. After the loop, print the final values of sum_even and sum_odd The key aspects are: - Using variables to store running totals - A for loop to iterate a fixed number of times - Modulo operator to classify numbers as even or odd - Updating the

Uploaded by

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

Problem Solving: Algorithms and Flowcharts: CSC 110-Introduction To Computer Systems

Here are the steps to solve this problem: 1. Initialize two variables, sum_even and sum_odd, to store the running totals of even and odd integers 2. Use a for loop to iterate 15 times 3. Inside the loop: - Read an integer - Use the modulo operator (%) to check if it is even or odd - Add the integer to the appropriate running total variable 4. After the loop, print the final values of sum_even and sum_odd The key aspects are: - Using variables to store running totals - A for loop to iterate a fixed number of times - Modulo operator to classify numbers as even or odd - Updating the

Uploaded by

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

PROBLEM SOLVING:

ALGORITHMS AND
FLOWCHARTS
CSC 110- Introduction To Computer
Systems
DECISION STRUCTURES

1. The expression A>B is a logical expression


2. it describes a condition we want to test
3. if A>B is true (if A is greater than B) we take the
action on left
4. print the value of A
5. if A>B is false (if A is not greater than B) we take the
action on right
6. print the value of B
DECISION STRUCTURES

Y N
is
A>B

Print Print
A B
IF–THEN–ELSE STRUCTURE

 The structure is as follows


If condition then
true alternative
else
false alternative
endif
IF–THEN–ELSE STRUCTURE

 The algorithm for the flowchart is as follows:


If A>B then
print A
else
print B
endif Y N
is
A>B

Print Print
A B
Relational Operators

Relational Operators
Operator Description
> Greater than
< Less than
= Equal to
 Greater than or equal to
 Less than or equal to
 Not equal to
Example 5

 Write an algorithm that reads two values, determines the largest


value and prints the largest value with an identifying message.
ALGORITHM …………………………………….. ???
Step 1: Input VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX  VALUE1
else
MAX  VALUE2
endif
Step 3: Print “The largest value is”, MAX
Example 5 (Flow Chart)

START

Input
VALUE1,VALUE2

Y is
N
VALUE1>VALUE2

MAX  VALUE1 MAX  VALUE2

Print
“The largest value is”,
MAX

STOP
NESTED IFS

 One of the alternatives within an IF–THEN–ELSE statement


- may involve further IF–THEN–ELSE statement
Example 6

 Write an algorithm that reads three numbers and prints the value of the largest
number.
Example 6
Step 1: Input N1, N2, N3
Step 2: if (N1>N2) then
if (N1>N3) then
MAX  N1[N1>N2, N1>N3]
else
MAX  N3[N3>N1>N2]
endif
else
if (N2>N3) then
MAX  N2[N2>N1, N2>N3]
else
MAX  N3 [N3>N2>N1]
endif
endif
Step 3: Print “The largest number is”, MAX
Example 6 (Class Activity)

 Flowchart: Draw the flowchart of the above Algorithm.


Example 7 (Class Activity)

 Write an algorithm and draw a flowchart to


a) read an employee name (NAME), overtime hours worked (OVERTIME),
hours absent (ABSENT) and
b) determine the bonus payment (PAYMENT).
Example 7

Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid

>40 hours $50


>30 but  40 hours $40
>20 but  30 hours $30
>10 but  20 hours $20
 10 hours $10
Step 1: Input NAME,OVERTIME,ABSENT
Step 2: if (OVERTIME–(2/3)*ABSENT > 40) then
PAYMENT  50
else if (OVERTIME–(2/3)*ABSENT > 30) then
PAYMENT  40
else if (OVERTIME–(2/3)*ABSENT > 20) then
PAYMENT  30
else if (OVERTIME–(2/3)*ABSENT > 10) then
PAYMENT 20
else
PAYMENT  10
endif
Step 3: Print “Bonus for”, NAME “is $”, PAYMENT
Example 7

 Flowchart: Draw the flowchart of the above algorithm?


Examples Conti… (Flow Chart?)

 Example 1. Design an algorithm and the corresponding flowchart for adding


the test scores as given below:
- 26, 49, 98, 87, 62, 75
 Convert it to repeat until?
 Convert it to repeat until number is not equal to -1?
Examples Conti… (Flow Chart?)

 Example 2: The program is to input a examination mark and test


it for the award of a grade. The mark is a whole number between
1 and 100. Grades are awarded according to the following
criteria:
- >= 80 Distinction
- >= 60 Merit
- >= 40 Pass
- < 40 fail
Examples Conti… (Flow Chart?)

 Example 3. A survey has been carried out to discover the most


popular sport. The results will be typed into the computer for
analysis. Write a program to accomplish this.
- A: Athletics
- S: Swimming
- F: Football
- B: Badminton
Examples Conti… (Flow Chart?)

 Example 4: The program is to input a number and find the factorial if number
is non-negative.
Examples Conti… (Flow Chart?)

 Design a program that reads a set of 15 integers and then finds and prints the
sum of the even and odd integers.

You might also like