3 - Program Design
3 - Program Design
(PRG-211)
Program Design
Presented By:
Ramsy J. BANDA
Computing and Information Technology Department
The Polytechnic
(This gets data from keyboard (user) and puts it in a variable called age)
age
Program Statements (Cont’d)
Output Statements sending data to output device or file
In C++, cout<< is used to send data (info) to a standard output
device such as a monitor.
Example in C++: cout<<“Enter your age => ”;
Program
An algorithm expressed in a language the computer can understand.
Program Design
Programming is a creative process
No complete set of rules for creating a program
Program Design Process
Problem solving phase
•Result is an algorithm that solves the problem
Implementation Phase
•Result is the algorithm translated into a programming language
Problem Solving Phase
Be certain the task is completely specified
What is the input?
What information is the output?
How is the output organized?
Develop the algorithm before implementation
Experience shows this saves time in getting your program to run
Test the algorithm for correctness
Implementation Phase
Translate the algorithm into a programming language
Easier as you gain experience with the language
Compile the source code
Locates errors in using the programming language
Run the program on sample data
Verify correctness of results
Results may require modification of the algorithm and program.
Algorithm notations
An algorithm can be expressed in different notations:
Natural language
Pseudo code and flowcharts – Design phase
Programming language – Coding phase.
Algorithm by Natural Language
Steps to solve the problems are written using a natural language such
as English.
Natural language tends to be ambiguous. Same statement can have
multiple meaning hence implemented different by different
programmers.
Algorithms for complex problems should not be expressed using
natural language.
Algorithm by Natural Language (Cont’d)
Example:
Write an algorithm for a program that accepts two numbers and
displays their sum.
Solution
1.Enter/accept first number from a user
2.Enter/accept second number from the user
3.Add two numbers together
4.Display a result in (3)
Algorithm by Pseudocode
A pseudocode is an algorithm written in something similar to
programming language but in a more understandable format. It
resembles a programming language.
Mixture of C++ and ordinary English
It cannot be executed by a computer
There is no universal standard on how pseudocodes should be written.
Different programmer present them differently.
Allows making algorithm precise without worrying about the details
of C++ syntax.
Algorithm by Pseudocode (Cont’d)
Declaration statement
- USE VARIABLES: list-of-variables As data-type
e.g. USE VARIABLE: age, grade As Integer (which in C++ is int age, grade)
- or USE VARIABLES: list-of-variables DataType data-type
e.g. USE VARIABLE: age, grade of DataType Integer (which in C++ is int age, grade)
Output Statement
- DISPLAY message
e.g. DISPLAY “Enter your age ” (which in C++ is cout<<“Enter your age”;)
Algorithm by Pseudocode (Cont’d)
Input Statement
- GET variable-name e.g. GET age (which in C++ is cin>>age;)
Assignment Statement
- COMPUTE expression e.g. COMPUTE sum = number1 + number2
Note:
You do not necessarily need to use COMPUTE for assignment
statements that do not involve arithmetic expressions such as age =
20).
Algorithm by Pseudocode (Cont’d)
Example:
Write a pseudocode for a program that accepts two numbers and displays
their sum.
BEGIN
USE VARIABLES: num1,num2,sum As Real
DISPLAY “Enter first number”
GET num1
DISPLAY “Enter second number”
GET num2
COMPUTE sum = num1 + num2
DISPLAY “Sum of two numbers is ”, sum
END
Algorithm by Flowcharts
A flowchart is a graphical expression of an algorithm
It graphically shows how the steps are related to each other
Diagram that shows the logical flow of a program
There are many symbols used in drawing a flowchart.
The final visualization can then be easily translated into a program
Algorithm by Flowcharts (Cont’d)
Flowcharting symbols
Algorithm by Flowcharts (Cont’d)
The use of
decision
symbol will
be shown
later
Algorithm by Flowcharts (Cont’d)
Example:
Write a flowchart for a program that accepts two numbers and
displays their sum.
Algorithm by Flowcharts (Cont’d)
STAR
Solution T
GET num1
GET num2
DISPLAY sum
END
Exercise
For a program that:
i. Calculates the weekly gross pay for a worker, based on the total
number of hours worked and the hourly pay rate.
ii.Accepts a unit price and quantity of items sold and displays total
amount and VAT. (NB: Unit price is VAT exclusive. VAT rate is 16.5%.).
a. Write a natural language algorithm
b. Write a pseudocode
c. Draw a flowchart
Recap: Running a C++ Program
C++ source code is written with a text editor
The compiler on your system converts source code to object code
The linker combines all the object code into an executable program.
Recap: Running a C++ Program (Cont’d)
Linking
The object code of your C++ program must be combined with the
object code from routines (such as input and output routines) that your
program uses.
This process of combining object is called linking and is done by a
program called a linker.
For simple programs, linking may be done for you automatically.
Recap: Running a C++ Program (Cont’d)
To Run a Program
Obtain code
Compile the code
Fix any errors the compiler indicates and re-compile the code
Run the program
Test it with sample data.
Recap: Testing and Debugging
Bug
A mistake in a program
Debugging
Eliminating mistakes in programs
Recap: Program Errors
i. Syntax Errors
Violation of the grammar rules of the language
Discovered by the compiler
•Error messages may not always show correct location of errors
ii. Run-time errors
Error conditions detected by the computer at run-time
iii. Logic Errors (Warning)
Errors in the program’s algorithm.
Most difficult to diagnose.
Computer does not recognize an error.