0% found this document useful (0 votes)
4 views29 pages

Chapter 02

Chapter 2 outlines the programming fundamentals and the program development cycle, which includes steps such as requirement analysis, design, coding, testing, and maintenance. It emphasizes the importance of planning and provides a structured approach to writing a program through nine recommended steps, including defining the program's purpose, visualizing its operation, and using algorithms and flowcharts for design. Additionally, it discusses error checking and debugging processes to ensure the program functions correctly.

Uploaded by

tamgirma1999
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)
4 views29 pages

Chapter 02

Chapter 2 outlines the programming fundamentals and the program development cycle, which includes steps such as requirement analysis, design, coding, testing, and maintenance. It emphasizes the importance of planning and provides a structured approach to writing a program through nine recommended steps, including defining the program's purpose, visualizing its operation, and using algorithms and flowcharts for design. Additionally, it discusses error checking and debugging processes to ensure the program functions correctly.

Uploaded by

tamgirma1999
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

Chapter 2:

Programming Fundamentals
Program Development Cycle
• The programming process consists of several steps,
which include user requirement analysis, design,
creation (coding), testing, debugging activities, and
operate and maintain the program.

A good program always


begins with planning.
9 steps recommended for the process
of writing a program
1. Clearly define what the program is to do.
2. Visualize the program running on the computer.
3. Use design tools such as flowcharts, or pseudo code to create a
model of the program.
4. Check the model for logical errors.
5. Type the code, save it, and compile it.
6. Correct any errors found during compilation. Repeat Steps 5 and
6 as many times as necessary.
7. Run the program with test data for input.
8. Correct any errors found while running the program. Repeat
Steps 5 through 8 as many times as necessary.
9. Validate the results of the program.
1. Define what the program do
• This step requires that you identify the purpose of the program, the
information that is to be input, the processing that is to take place, and the
desired output.

Example: You want to code a program that calculates user's gross pay.

• Purpose: To calculate the user’s gross pay.


• Input: Number of hours worked, hourly pay rate.
• Process: Multiply number of hours worked by hourly pay rate. The result is
the user’s gross pay.
• Output : Display a message indicating the user’s gross pay.
2. Visualize the program running on the computer

• This step might give an overall view of the


program from the user point of view and
helps you during design phase.
Example: Visualize the out put of the
previous example output on screen.

• This phase is very important if the program


involves graphical user interface.
3. Design
• The output of this phase is an Algorithm

Algorithm:
An algorithm is a sequence of unambiguous
instructions for solving a problem, i.e., for
obtaining a required output for any
legitimate/valid input in a finite amount of time
[Levitin, p. 3].
An algorithm exhibits
– Sequence (Process)
Each task follows the previous one without
altering the order of the tasks.
– Decision (Selection)
Allows only one of two tasks to be carried out.
Eg. If…then…, If… then… else…
An algorithm exhibits
– Repetition (Iteration/Looping)
Enables a task to be repeated until a
controlling condition no longer holds

What if it loops infinitely???

It is not an algorithm
Tools to write Algorithm
1. Plain English (the step-by-step way)
– Uses natural English sentences( No variables and symbols)
2. Pseudocode (False code)
– Uses much restricted vocabulary
– Closer to a Programming Language
– Uses variables and symbols
– There are different types available
3. Flowchart
– Graphical method
– Uses various symbols for various actions
– Very easy to draw
– Very easy to read and understand
Pseudocode
• is a detailed yet readable description of what a computer
program or algorithm must do, expressed in a formally-
styled natural language rather than in a programming
language. Pseudocode is sometimes used as a detailed
step in the process of developing a program. It allows
designers or lead programmers to express the design in
great detail and provides programmers a
detailed template for the next step of writing code in a
specific programming language.
Peseudocode Example
• Calculating pay

• Election Control Structures


Begin
Input age
If age > 17
Display a message indicating you can vote.
Else
Display a message indicating you can't vote.
End
Flowcharts
• A flowchart is a diagram that
depicts the “flow” of a program. It
contains symbols that represent
each step in the program. The
figure shown here is a flowchart
for Calculating pay
• There are three types of symbols
in this flowchart: rounded
rectangles, a rectangle and
parallelograms.
Flowchart Symbols
• Basically there are five typist of symbols in
flowcharts
– Rounded rectangles (representing terminal points, start and end),
– Parallelograms represent input and output
– Rectangles represent process excluding input and output
– Diamonds represent decision/choices
– Circle s represent connectors.

Decision Connector
Terminal Points
• Rounded rectangles, or terminal points,
indicate the starting and ending points of
the flowchart.
Processing
• A rectangle depicts a process
such as a mathematical
computation or a variable
assignment.

Input/Output Operations
• Parallelograms designate
input or output operations.
Decision
• Diamond designates decision or choice
when only one of two options is expected
to be processed. (in c++, we can choose 1
of 2 or more options using if, else if and
else statements.)

No Yes
Is age
>17?
Connectors
• A connector symbol, which is a circle, is
used to break flowchart into multiple parts.
This is importance when we want to move
one page to another

Page 1 Page 2
Flowchart Structures
• There are four general flowchart
structures:
– Sequence
A sequence structure is a series of actions or steps,
performed in order.
– Decision
– Repetition
– Case
The Decision Structure
• In a decision structure, one of two possible
actions is performed, depending on a condition.
The diamond, represents a yes/no question.

if (x<y)
Process A;
else
Process B;
Exercise
• Convert the following pseudocode in to flowchart

Election Control Structures


Begin
Input age
If age > 17
Display a message indicating you can vote.
Else
Display a message indicating you can't vote.
End
Repetition Structure
• A repetition structure represents part of the program that repeats.
This type of structure is commonly known as a loop.

Process A will be
executed at least
once even if the
condition (x<y) is not
Process A will be executed true.
only if the condition (x<y) is (do while loop)
true. (for or while loop)
do
{
While (x<y) for (;x<y;) Process A;
{ { }
Process A; Process A; While (x<y)
} }
Exercise

• Draw a flow chart that shows the steps to display a


number and its square for integer numbers between 1
and 10 inclusive). It should also display table heading
as shown bellow.
x x squared
1 1
2 4
… …
10 100
…Exercise
Case structure
• In a case structure, one of several possible actions is
taken, depending on the contents of a variable. The
following flowchart segment shows the general form of a
case structure.
Example - Case structure

• Case structure can be


substituted by decision
structure at the expense
of increased complexity
4. Check the model for logical error
• Once you have done with the design phase you should
review the whole mode for logical errors.
• unlike syntax and runtime errors, logical errors will not be
reported by compiles or runtime environments and for
this reason most probably will not caught by developers(
those who convert the design into code). These errors
only produce unexpected/wrong outputs for valid inputs.

– For example if instead of saying multiply you might say add, this
error seams correct from compiler and runtime environment point
of view but gives unexpected/wrong result.
5. Type the code
• Now you are ready to convert your design in to program
Program= Algorithm + Data Structures
Data Structure: is the way data is represented in
computers.
• Write the code using a programming language you
prefer. Convert variables into appropriate data structures
of the right data types.

6. Save, compile and correct errors


• Do step 6 and 5 until the compiler reports no syntax error
7 & 8. Run the program and correct errors

• Now you have a machine code or executable file created


by the linker.
• Run the program and check for any runtime errors
repeat steps 5 to 7 until you get no error. These type of
errors usually crush the program and my be as a result
of violating mathematical rules or providing un expected
input. This errors are usually machine specific.
– For example providing a 0 input as a value for denominator will violet a
mathematical rule and generates runtime error; or trying to run an
executable code prepared for Ubunt(Linux) on Windows machine will
generate runtime error
9. Run the program and correct errors

• When you believe you have corrected all the run-time


errors, enter test data and determine whether the
program solves the original problem.

You might also like