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

Chapter 1.1 Problem Solving, Pseudocode Flowcharts

The document outlines the process of problem-solving in programming, emphasizing the importance of analyzing problems, designing algorithms, and implementing solutions. It introduces pseudocode and flowcharts as tools for planning and visualizing algorithms. Several examples illustrate how to create algorithms and flowcharts for various programming tasks.

Uploaded by

iqraghouri440
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)
13 views

Chapter 1.1 Problem Solving, Pseudocode Flowcharts

The document outlines the process of problem-solving in programming, emphasizing the importance of analyzing problems, designing algorithms, and implementing solutions. It introduces pseudocode and flowcharts as tools for planning and visualizing algorithms. Several examples illustrate how to create algorithms and flowcharts for various programming tasks.

Uploaded by

iqraghouri440
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/ 25

Lecture 02

Problem Solving, Pseudocode


& Flowcharts

Instructor: Mr. Waqar Ashiq

1
Problem Solving

• Programming is a process of problem solving


• Problem solving techniques
− Analyze the problem
− Outline the problem requirements
− Design steps (algorithm) to solve the problem
• Algorithm:
− Step-by-step problem-solving process
− Solution achieved in finite amount of time

C++ Programming: From Problem Analysis to Program Design, Third Edition 2


Problem Solving Process
• Step 1 - Analyze the problem
− Outline the problem and its requirements
− Design steps (algorithm) to solve the problem
• Step 2 - Implement the algorithm
− Implement the algorithm in code
− Verify that the algorithm works
• Step 3 - Maintenance
− Use and modify the program if the problem
domain changes

3
C++ Programming: From Problem Analysis to Program Design, Third Edition
Example
Design algorithm to find the perimeter and area of a
rectangle.
Analyze the problem:
• To find the perimeter and area of a rectangle, we
need to know the rectangle’s length and width.
The perimeter and area of rectangle is given by the
following formula:
Perimeter = 2 . (length + width)
Area = length . width

4
Example (continues)

Design algorithm:
1. Get length of Rectangle.
2. Get width of Rectangle.
3. Find the perimeter using the following equation:
Perimeter = 2 . (length + width)
4. Find the area using the following equation:
Area = length . width

5
Levels of Program Development
6

1. Define the problem. →Human thought


2. Plan the problem solution. → writing the
algorithm [pseudo-natural language (English,
Arabic) or drawing the flowchart diagram).
3. Code the program. → High Level
Programming Language (C, C++, Java, …)
4. Compile the program. → Machine Code
5. Run the program.
6. Test and debug the program.
we learn that
7

 When planning for a problem solution, algorithms are


used to outline the solution steps using
 Simplified version of programming codes, written in plain
English, English like statements, called pseudocode.
or
 A flowchart , which is a graphical representation of
an algorithm.
Algorithm
8

 Pseudocode is a simplified version of programming


codes, written in plain English language and used to
outline a program before its implementation.
Pseudocode Example
9

 Write a Program to Print the Sum of two integer


Numbers

1. Start the program


2. Read the first number and save in the variable ( N1 )
3. Read the second number and save in the variable (
N2 )
4. Sum the both numbers and save the result in the
variable ( Sum ) ➔ Sum = N1 + N2
5. Print the variable ( Sum )
6. End the program
Flowchart
10

 A flowchart is a type of diagram that represents


an algorithm , showing the steps as boxes of various
kinds [ex: rectangles, diamonds, ovals], and their
order by connecting these with arrows.
Flowcharts Symbols

11
End Start Oval -> Start/End

Print n1 Read n1 Parallelogram -> Read/Print or


Input / Output
N2 = n1+3 N2 = 5 Rectangle -> Arithmetic
Operations / Processing
n1 > 3 Diamond -> Decision , can be
used with loops

Arrows -> Represent the Flow


Solution
12
start

Draw a flowchart for a program that calculates


Read L, W
and print the area and the perimeter of
a rectangle.
 Input
area = L * W
 Length
 width perimeter = 2 (L+W)
 Processing

 Area = length*width Print area


 Perimeter = 2*( length + width)
 Output Print perimeter
 Area
 Perimeter End
Example 2
13

 Draw the flow chart for a program that calculates


the total salary for an employee using this equation:
Total_Sal = Salary +Overtime
Solution
14

 Input start
 Salary
 Overtime Read Salary
 Processing
 Total_Sal = Salary +Overtime
Read Overtime
 Output
Total_Sal =
 Total_Sal
Salary +Overtime

Print Total_Sal

End
Example 3
15

 Draw a flowchart for a program that calculates and


prints the sum of the even integers from 2 to 30.
 Input
 No input.
 Processing
 Sum = 2+4+6+8+……+28+30.
 Output
 sum
Solution
16

Pesudocode:
◼ Start the program
◼ Create a variable to hold a counter from 2 to 30.
◼ Initialize the counter to 2.
◼ Create a variable to hold the sum.
◼ Initialize the sum to zero.
◼ Loop While the counter is less-than-or-equal to 30
◼ add the counter to the sum
◼ add two to the counter.
◼ repeat until the counter reach 30
◼ Print the sum.
◼ End of program
Solution
17

Start

Counter=2, Sum=0

yes
no
Counter≤30

yes
Sum= Sum + Counter

Counter=Counter+2

Print Sum

End
Example 4
18

 Draw a flowchart for a program that determine if


the temperature degree is above or below freezing.
 Input
 Temp.
 Processing
 Check if Temp is below the 32 → below freezing.
 Check if Temp is above the 32 → above freezing.
 Output
 Print “below freezing” or “above freezing”
Solution
19
Example 5
20

 Draw a flowchart for a program that accepts a


person’s initial bank balance followed by a
sequence of numbers representing transactions. A
positive number represents a credit entry in the
account and a negative number represents a debit
entry. The input is terminated by a zero entry. The
program should print the new balance.
Solution
21

 Input
 bank balance.
 transactions
 Processing
 balance = balance + transaction.
 Output
 balance
Solution
22

Start

Read balance

Read transaction

balance =balance + transaction

Transaction !=0
yes
no
Print balance

End
Example 6
23

 Draw a flowchart for a program that calculates the


Zakat, where the user enter the amount of money
then the program show the zakat.
◼ Zakat =(2.5/100) * amount.
 Zakat is not calculated if the amount is less than 1000
S.R
Solution
24

 Input
 amount.
 Processing
 Check if amount is below 1000 → Zakat =0.
 Check if amount is above 1000→ Zakat =(2.5/100) *
amount
 Output
 Zakat
Solution
25

Start

Read amount
yes
no
Amount > 1000

Zakat =0. Zakat =(2.5/100)*amount

Print Zakat

End

You might also like