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

Basics of Programming Lecture 02

The document covers the basics of programming, focusing on flowchart diagrams and algorithms for problem-solving in computer programming. It defines algorithms, discusses their properties, types, and provides examples of flowchart construction for various problems. Additionally, it highlights the importance and limitations of flowcharts in programming solutions.

Uploaded by

athumankasele9
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)
12 views

Basics of Programming Lecture 02

The document covers the basics of programming, focusing on flowchart diagrams and algorithms for problem-solving in computer programming. It defines algorithms, discusses their properties, types, and provides examples of flowchart construction for various problems. Additionally, it highlights the importance and limitations of flowcharts in programming solutions.

Uploaded by

athumankasele9
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/ 33

Basics of Programming

Lecture 02
Unit 2: Apply Flowchart Diagrams in Constructing Algorithms for
Computer Program Solutions
• Definition of a flowchart
• Common flowchart symbols and their meanings
• Identifying action flows for a given problem
• Constructing flowchart diagrams to represent problem-solving processes
• Limitations of using flowcharts in programming solutions
Definition of Algorithm
An algorithm is a well-defined sequence of steps or rules
designed to solve a problem or perform a particular task.
Algorithms are a foundational concept in computer science
and mathematics, providing a structured approach to
problem-solving. A good algorithm is usually efficient, clear,
and able to solve the problem with minimal computational
resources.
Design/planning a solution
Example 1:
Problem: Students in a class plan to go on a picnic and decide to share the
expenses among them.
Solution: Calculate the total expenses and the amount an individual has to give for
a picnic. This is a kind of problem-solving.
Example 2:
Problem: You’re watching a news channel on your TV and you want to change it to
a sports channel
Solution: you need to move to that channel by pressing that channel number on
your remote. This is a kind of problem-solving.
The above we can broadly say that a problem is a kind of barrier to achieving
something and problem solving is the process of getting that barrier removed by
performing some sequence of activities.
Algorithms – Properties/Characteristics
A well-constructed algorithm should have the following characteristics:
• Finiteness: The algorithm must always terminate after a finite number of
steps.
• Definiteness: Each step must be precisely defined; the actions to be
carried out must be rigorously and unambiguously specified for each case.
• Input: An algorithm has zero or more inputs, taken from a specified set of
objects.
• Output: An algorithm has one or more outputs, which have a specified
relation to the inputs. The output values are the solution to the problem.
• Effectiveness: All operations to be performed must be sufficiently basic
that they can be done exactly and in finite length.
Symbols for writing algorithm
• Symbols for different operations while writing algorithms:
• ‘+’ for Addition
• ‘-’ for Subtraction
• ‘*’ for Multiplication
• ‘/’ for Division
• ‘🡪 ’ for assignment.
For example, A 🡨 X*3 means A will have a value of X*3.
Algorithms - Examples
Problem 01: Find and display the area of a circle with radius r that is read
from the keyboard.
• Inputs to the algorithm: Radius r of the Circle.
• Expected output: Area of the Circle

Algorithm:
• Step 1: Start
• Step 2: Read\input the radius r of the circle
• Step 3: Area 🡨 PI*r*r // calculation of area
• Step 4: Print Area
• Step 5: Stop/End
Algorithms - Types
Algorithms and flowcharts can be classified into three types of control
structures.
• Sequence
• Branching (Selection)
• Loop (Repetition)
Sequence
A sequence of statements is placed one after the other above
or before another gets executed first. In flowcharts, this
sequence of statements is usually contained in the rectangular
process box.
Sequence Algorithm Example
Problem 02: Find and display the area of a circle with radius r that is read
from the keyboard.
• Inputs to the algorithm: Radius r of the Circle.
• Expected output: Area of the Circle

Algorithm:
• Step 1: Start
• Step 2: Read\input the radius r of the circle
• Step 3: Area 🡨 PI*r*r // calculation of area
• Step 4: Print Area
• Step 5: Stop/End
Branch
The branch refers to a binary decision based on some condition. If the
condition is true, one of the two branches is explored; if the condition
is false, the other alternative is taken.
• In pseudo-codes and programs it is usually represented by the ‘if-
then’ construct.
• In flowcharts, this is represented by the diamond-shaped decision
box.
This structure is also known as the selection structure.
Branching Example:
Problem 03: Write an algorithm to find the greater number between
two numbers

The algorithm:
• Step1: Start
• Step2: Read/input two numbers A and B
• Step3: If A greater than B then C=A
• Step4: if B is greater than A then C=B
• Step5: Print C
• Step6: End
Loop
• The loop allows a statement or a sequence of statements to be repeatedly
executed based on some loop condition.
• It is represented by the ‘while’ and ‘for’ constructs in most programming
languages, for unbounded loops and bounded loops respectively.
• (Unbounded loops refer to those whose number of iterations depends on the
eventuality that the termination condition is satisfied; bounded loops refer to
those whose number of iterations is known beforehand.)
• In the flowcharts, a back arrow hints at the presence of a loop.
• A trip around the loop is known as iteration.
• You must ensure that the condition for the termination of the looping must be
satisfied after some finite number of iterations to avoid an infinite loop.
• The loop is also known as the repetition structure.
Loop Examples:
Problem 04: Write an algorithm to find and print even numbers between
0 and 99.

Algorithm:
• Step 1. Start
• Step 2. I ← 2
• Step 3. Write I //in standard output
• Step 4. I ← I+2
• Step 5. If (I <=98) then go to line 3
• Step 6. End
Problem 05: Design an algorithm that generates even numbers between
1000 and 2000 and then prints them in the standard output. It should
also print the total sum.

Algorithm:
• Step 1. Start
• Step 2. I ← 1000 and S ← 0
• Step 3. Write I
• Step 4. S ← S + I
• Step 5. I ← I + 2
• Step 6. If (I < 2000) then go to line 3 else go to line 7
• Step 7. Write S
• Step 8. End
Note:
• It is common to find problems that require combining the use of these
control structures, for example, a loop within a loop (nested loops), a
branch within another branch (nested if), a branch within a loop, a loop
within a branch, and so forth.

• Complex algorithms may have a more complicated logic structure and


deep level of nesting, in which case it is best to demarcate parts of the
algorithm as separate smaller modules (functions).

• Beginners must train themselves to be proficient in using and


combining control structures appropriately, and go through the trouble
of tracing through the algorithm before they convert it into code.
Tools Used to Construct an Algorithm
Several tools are commonly used to construct and
represent algorithms, including:
• Flowcharts
• Pseudo Code
• Decision Tables
• UML (Unified Modeling Language) Diagrams etc.
Flowcharts
A flowchart is a visual representation (diagram) that
represents the sequence of steps in an algorithm needed to
perform a process or solve a problem.

It uses symbols to illustrate the flow of control, decision-


making, and process execution. Flowcharts help programmers
and developers plan, design, and communicate logic before
writing actual code.
There are various types of flowcharts while the common ones
are:
• Process flowchart
• Swimlane flowchart
• Data flow diagram
• Workflow diagram etc.
Importance of using Flowcharts
Effective Documentation: Flowcharts provide thorough documentation of a process,
making it easier to understand and revisit later, especially for training, onboarding, or
auditing purposes.
Clarity of the Process: Flowcharts provide a clear, step-by-step representation of a
process or project, reducing confusion and doubts.
Improved Problem-Solving: Flowcharts help in problem-solving by breaking down
issues and visually representing possible solutions, making it easier to find the
optimal one.
Effective Communication and Coordination: Flowcharts can replace team meetings
by visually presenting processes, expected performances, and deadlines, saving time.
Saves Time and Resources: By providing a clear guide, flowcharts help avoid errors,
thus saving time and resources that would otherwise be spent fixing issues later in
the process.
Flowcharts Symbols
Symbol Name Function
Start/End An oval represents a start or the end point

Arrow A line is connector that shows relationship


between the representative shapes
Input/output A parallelogram represents input or output

Process A rectangle represents a process

Decision A diamond indicates a decision

Connector Used to connect your flowchart between different


pages
Steps to Construct a Flowchart
To create a flowchart, follow these steps:
• Define the problem: Clearly understand the problem you want to solve.
• Identify inputs and outputs: Determine what data is required and what
results will be produced.
• Break down the logic: Identify the sequence of actions needed to solve
the problem.
• Choose appropriate flowchart symbols: Use standard symbols to
represent each step logically.
• Draw the flowchart: Arrange the symbols in the correct sequence and
connect them using arrows.
• Review and refine: Check for errors or missing steps and improve the
diagram if necessary.
Building algorithm in Flowchart
Problem 01: Create a flowchart that reads two numbers from
the user, calculates their sum, and displays the result.

Flowchart steps:
Step 1: Start – Begin the process.
Step 2: Input two numbers (let's say A & B).
Step 3: Add the numbers (A + B)
Step 4: Display the sum
Step 5: End/Stop
Flowchart for Problem 01

Start

Input A and B

A+B

Sum of A+B

Stop
Building algorithm in Flowchart
Problem 02: Finding the largest of two numbers.

Flowchart steps:
Step 1: Start – Begin the process.
Step 2: Input two numbers (let's say A & B).
Step 3: Compare the variables (Decision 1 (Is A > B?))
Step 4: If yes, A is greater.
Step 5: If no, B is the greater.
Step 6: End/Stop – The largest number is displayed, and the
process stops.
Flowchart for Problem 02
Start

Input A and B

Is A No
greater Output B is greater
than B

Yes

Output A is greater

Stop
Problem 03: Check whether a number is even or odd.

Flow of Actions:
1. Start
2. Input a number
3. Check if the number is divisible by 2
4. If true, print "Even number"
5. Else, print "Odd number"
6. End
Flowchart for Problem 03
Start

Enter a positive
number

Is a
number No Output: A number is an
divisible Odd number
by 2

Yes
Output: A number is
an Even number

Stop
Flowchart for Problem 03 (Simplified)
Start

Enter a positive
number (A)

Is A No Output A is an Odd
divisible
number
by 2

Yes
Output: A is an Even
number

Stop
Limitations of Flowcharts
Although flowcharts are useful, they have some drawbacks:
• Complexity: Large problems can make flowcharts too big and hard to
follow.
• Time-consuming: Drawing flowcharts for every program is not always
practical.
• Modification issues: Any small change may require a complete
redesign.
• Not Suitable for All Problems: Some algorithms are better
represented using pseudocode or decision tables.
Class Activity (Algorithm & Flowchart)
Problem 04: Finding the largest of three numbers form the user input.
Flowchart steps:
Step 1: Start – Begin the process.
Step 2: Input three numbers, let's say (A, B and C)
Step 3: Compare the variables (Decision 1 (Is A > B and A > C?))
Step 4: If yes, A is greater. If not, go to the next decision.
Step 5: Compare the variables (Decision 2 (Is B > A and B > C?))
Step 6: If yes, B is the largest.
Step 7: If no, C is the largest.
Step 8: End/Stop – The largest number is displayed, and the process stops.
Flowchart for Problem 04 (Class Activity)
Start

Enter three
no.s A, B &C

Is A > B No Is B > A No
C is greater
&A>C &B>C

Yes Yes

A is Greater B is Greater

Stop
Exercise 01 (Thursday 14:00-1500)
Exercise: ATM Withdrawal Process
• Design an algorithm and flowchart for an ATM withdrawal process where a
user can withdraw money if they have sufficient balance.

Problem Statement:
• A user inserts their ATM card and enters their PIN.
• If the PIN is correct, the system asks for the withdrawal amount.
• If the amount is less than or equal to the account balance, the transaction is
successful, and cash is dispensed.
• If the amount is greater than the account balance, the system displays an
error message.
• If the PIN is incorrect, the system allows up to three attempts before blocking
the card.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy