You are on page 1of 20

Introduction to Computer Science

C++ Programming
Developing Algorithms &
Flow Charts
Part 1

Introduction
Software is developed with programming
languages
Before programming, it is necessary to:
understand the problem and its elementary
components
determine what functionality each component
must embody
how the components logically fit together to
accomplish the task at hand.
2

Programming
Classic Life Cycle

Design an Algorithm
An algorithm is not a solution but a process
to obtain a solution
Divide the problem into subproblems.
Design an algorithm for each subproblem.
Check the correctness of each algorithm
Can test using sample data
Some mathematical analysis might be
required
4

Design the Algorithm


Program logic usually follows a basic
pattern:
Obtain the input
Process the data
Store relevant data
Display the output
Try to avoid over-thinking the problem
5

Design the Algorithm


Benefits:
Requires thought to determine the need for and
relevance of an operation.
Formulates the process into a sequence of steps
Allows for the pre-verification of the solution
Defines the basic structure of the code to
accomplish each step of the process
Reduces the time to write and debug coding
Can be done 24/7
6

Design the Algorithm


Disadvantages:
Conflicts with the urge to write the code
May be necessary to generate formal
documentation

Design an Algorithm
Two common methods for designing an
algorithm [program logic]
1. Pseudo-code a non standardized
language to define the sequence of steps
to obtain the solution to the problem.
2. Flowcharts a standardized set of
symbols to visually describe the logical
steps required to arrive at the solution to
the problem.
8

Design the Algorithm


Pseudo-code
A self defined language
No formal rules
A combination of words and / or symbols
that convey meaning
Self determined level of detail

Design the Algorithm


Pseudo-code
Benefits:
Does not require digital technology
Is [programming] language independent,
including free use of symbols
Disadvantages:
May be difficult for someone else to understand
May not be digitally documentable
May require standardization
Standard logical structures are not evident
Tends to approach actual code
10

End of Part 1

11

Introduction to Computer Science


C++ Programming
Developing Algorithms &
Flow Charts
Part 2

Design an Algorithm
Flowcharting
A set of standard symbols used to describe
the variety of operations and interactions of
a logical set of steps.
Text within and accompanying the symbols
define the precise action to be taken.
The direction of flow is indicated by the use
of arrows.
Flowchart Symbols
13

Design an Algorithm
Flowcharting
Benefits:
The logic is visual you can see the flow
Standard logical structures are clearly identifiable
Can be drawn free-hand
Governed by standard rules
Disadvantages:
Governed by standard rules
May require software for formal documentation
14

Design an Algorithm
A Simple Problem

You want to put new flooring in each of


your bathrooms.
Before starting, you want to establish a
budget and determine the total cost.
Using a catalog, you select the type and
style of flooring.
Determine the total cost for the project.
Determine if your budget can support it.
15

Design an Algorithm
Pseudo-code
Algorithm to Calculate Bathroom Flooring Cost
1.
2.
3.
4.
5.

6.
7.
8.
9.

Get the number of bathrooms to be done [number]


Get the [budget]ed amount
Get the [price] per square foot of the flooring material
Set [count] and [total area] to zero
For each bathroom (count < number)
1. Get the [length] and [width] to determine the total square footage [area]
1. area = length x width
2. Add the area to the [total area] of all bathrooms
1. total area = total area + area
3. Add 1 to count
Calculate the total [cost] for the flooring
1. Total cost = total area x material price
Compare the total cost to your budgeted amount
If total cost <= budgeted amount
1. Display OK to proceed with the project message
Otherwise
1. Display over budget, reevaluate the project with different material from 3. above
16

Design an Algorithm
Flowchart
Flow Chart to Calculate Bathroom Flooring Cost
Start

Get [number]
of bathrooms
and [budget]
amount

{ Do Loop }
Get material
[price]

Display over
budget
message

[count] = 0
[total area] = 0

Total [cost] =
total area x price

{ While Loop }
count <
number

Total cost
> budget

T
Get [length]
and [width]

[area] = length x width


Add area to total area
Add 1 to count

Display OK
to proceed
message

Stop

17

End of Part 2

18

In-Class Lab #1
Design an algorithm to perform the following task:
Obtain a number from the user
Calculate and display the square of the number
Repeat the process until the square of the number is 0
Submit the pseudo-code via the WebStudy In-Class
Lab assignment Lab 01 in the Assignments tab.

19

Assignment #1
Design an algorithm to meet the following specifications:
Calculate and display the sales bonus for the entered sales amount.
Prompt the user and get the sales amount.
Calculate and display the bonus amount as follows:
If the dollar value of the sales amount is less than 25000, the bonus is
1% of the sales amount otherwise the bonus is 5% of the sales amount
Display the bonus amount.
Ask the user if another salesmans bonus is to be calculated.
If the response is Y[es] repeat the process for the next salesman; if N[o],
end the program.
Name the file: SalesBonus
Submit a pseudo-code document via the WebStudy Assignment
Assignment #1 in the Assignments tab.
20

You might also like