0% found this document useful (0 votes)
11 views31 pages

3 - Program Design

The document outlines the fundamentals of program design, emphasizing the importance of the design phase in developing a program that meets specified requirements. It explains various program statements, including declaration, input/output, and assignment statements, and introduces algorithms as step-by-step procedures for problem-solving. Additionally, it covers the implementation phase, algorithm notations, and the process of debugging and testing C++ programs.

Uploaded by

Priome Takur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views31 pages

3 - Program Design

The document outlines the fundamentals of program design, emphasizing the importance of the design phase in developing a program that meets specified requirements. It explains various program statements, including declaration, input/output, and assignment statements, and introduces algorithms as step-by-step procedures for problem-solving. Additionally, it covers the implementation phase, algorithm notations, and the process of debugging and testing C++ programs.

Uploaded by

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

Programming I

(PRG-211)

Program Design
Presented By:
Ramsy J. BANDA
Computing and Information Technology Department
The Polytechnic

:265 (0) 881208662 :[email protected]


Introduction
 After program requirements are clearly stated in terms of input, output,
process and storage, the next stage is to come up with a blue-print of
the program you intend to build.
 Program design phase shows the steps that should be taken in order to
build a program that meet requirements.
 Though taken for granted by novice programmers, design phase should
be given long time in developing a program.
 There are different program statements and they are presented
differently in design.
Program Statements
A program statement:
­Is a small unit of code with a complete programing thought i.e. that
makes sense.
­Is a sentence of programming language.
­Ends with semicolon (;) in many languages such as C++, Java, PHP.
Three basic types of statements are:
i.Declaration Statements
ii.Input / Output Statements
iii.Assignments Statements
Program Statements (Cont’d)
a). Declaration Statements
i.Statements for declaring variables/constants
­Specifies data type and a name of the variable
­Example in C++: int age;
ii.Statements for declaring functions
­Specifies return value data type, function name and list of
parameters.
­Example in C++: float average(int a, int b);
(To be covered when will be looking at Functions Next Semester)
Semester
Program Statements (Cont’d)
b). Input / Output Statements
­Input Statements for getting data from input device or file
­In C++, cin>> is used to get data from standard input device such as
a keyboard and put it into a variable;
­Example in C++: cin>>age;

(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 => ”;

(This sends data from program to monitor )


Program Statements (Cont’d)
c). Assignments Statements
­Statements for copying /setting values (data) to variables
•Equals sign (=) is used in assignment statement
•A variable where data is copied/set to is on the left side of the
equals sign (=)
•On the right side of equals sign (=) is a valued to be copied/set.
This can be a literal value, another variable or an expression .
Program Statements (Cont’d)
 Examples:
i. Literal value: age = 20;
(This copies/sets 20 to variable age)
age

ii. Another variable: number2 = number1 ;


(This copies value of variable number1 to variable number2)
number2

iii. Expression: sum = number1 + number2;


(This copies result of calculation of number1 + number2 to
variable sum)
sum
Algorithm
What is an Algorithm?
It is a step by step procedure to find a solution to a problem
It is a sequence of steps that should be followed to solve a problem
A sequence of precise instructions which leads to a solution.

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

DISPLAY "Enter first number”

GET num1

DISPLAY "Enter second number”

GET num2

sum = num1 + 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.

You might also like