02 Program Design PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Fundamental Programming

Program Design
Bimo Sunarfri Hantono

Electrical Engineering and Information Technology


Gadjah Mada University
Objective
• To describe the steps in the program development process
• To introduce current program design methodology
• To introduce procedural and object-oriented programming
• To introduce algorithms and pseudocode
• To describe program data
Algorithm
• Named after: Muhammad ibn Musa al-Khwarizmi of
Khowarezm (now Khiva in Uzbekistan) Circa 780-850 C.E.
(Common Era)
• Book on arithmetic:
• Hindu numeration, decimal numbers, use of zero, method for finding
square root
• Latin translation (c.1120 CE): “Algoritmi de numero Indorum”
• Book on algebra
• Hisab al-jabr w’al-muqabala
Algorithm
• A sequence of instructions specifying the steps required to
accomplish some tasks
• Examples
• A cooking recipe
• Assembly instructions for a model
• The rules of how to play a game
• Directions for driving from A to B
• A knitting pattern
• A car repair manual
• etc
How do we solve problems?
• Just Do
• Guess Work and Luck
• Trial and Error
• Experience
• Scientifically
Components of an Algorithm
• Values represent quantities, amounts or measurements. May
be numerical or alphabetical (or other things).
• Variables are containers for values, place to store values.
Variables may be restricted to contain a specific type of value.
• Instructions should be simple and unambiguous, the system
knows about and should be able to actually do. Sequences (of
instruction)
• Selections (between instructions)
• Repetitions (of instructions)
• Procedures
• Documentation
An Algorithm Conclusion
• An algorithm must:
• simple
• lucid
• precise
• unambiguous
• give the correct solution
• eventually end
7 Steps in Program Development
• Define the problem
• Outline the solution
• Develop the outline into an algorithm
• Test the algorithm for correctness
• Code the algorithm into a specific programming language
• Run the program on the computer
• Document and maintain the program
7 Steps in Program Development
• Define the problem
• The inputs,
• The outputs,
• The processing steps to produce the required outputs.
• Outline the solution
• Develop the outline into an algorithm
• Test the algorithm for correctness
• Code the algorithm into a specific programming language
• Run the program on the computer
• Document and maintain the program
7 Steps in Program Development
• Define the problem
• Outline the solution
• The major processing steps involved, The subtask (if any), The user
interface (if any), The major control structures (e.g. Repetition
loops), The major variables and record structures, The mainline
logic.
• Develop the outline into an algorithm
• Test the algorithm for correctness
• Code the algorithm into a specific programming language
• Run the program on the computer
• Document and maintain the program
7 Steps in Program Development
• Define the problem
• Outline the solution
• Develop the outline into an algorithm
• Algorithm, a set of detailed, unambiguous and ordered instructions
developed to describe the processes necessary to produce the
desired output from a given input.
• Pseudocode, Flowcharts and Nassi-Schneiderman
• Test the algorithm for correctness
• Code the algorithm into a specific programming language
• Run the program on the computer
• Document and maintain the program
7 Steps in Program Development
• Define the problem
• Outline the solution
• Develop the outline into an algorithm
• Test the algorithm for correctness
• This step is one of the most important in the development of a
program, and yet it is the step most often forgotten.
• The main purpose of desk checking the algorithm is to identify
major logic errors early, so that they may be easily corrected.
• Code the algorithm into a specific programming language
• Run the program on the computer
• Document and maintain the program
7 Steps in Program Development
• Define the problem
• Outline the solution
• Develop the outline into an algorithm
• Test the algorithm for correctness
• Code the algorithm into a specific programming language
• Run the program on the computer
• This step uses a program compiler and programmer designed test
data to machine test the code for syntax errors (those detected at
compile time) and logic errors (those detected at run time).
• This step may need to be performed several times until you are
satisfied that the program is running as required.
• Document and maintain the program
7 Steps in Program Development
• Define the problem
• Outline the solution
• Develop the outline into an algorithm
• Test the algorithm for correctness
• Code the algorithm into a specific programming language
• Run the program on the computer
• Document and maintain the program
• External documentation (such as hierarchy charts, the solution
algorithm and test data results) and internal documentation that
may have been coded in the program.
• Program maintenance refers to changes that may need to be made
to a program throughout its life.
Data Validation
• Data should always undergo a validation check before it is
processed by a program.
• Different types of data require different check, for example:
• Correct type: the input data should match the data type definition
stated at the beginning of the program.
• Correct range: the input should be within a required set of values.
• Correct length: the input data should be the correct length.
• Completeness: all required fields should be present.
• Correct date: an incoming data should be acceptable.
Example 1. Add Three Number
• A program is required to read three numbers, add them
together and print their total.
Example 1. Add Three Number
• A program is required to read three numbers, add them
together and print their total.

• Underline the nouns and adjectives used in the specification


• establish the input, output component and any object that are
required.
Example 1. Add Three Number
• A program is required to read three numbers, add them
together and print their total.

• Underline the nouns and adjectives used in the specification


• establish the input, output component and any object that are
required.

Defining Diagram:
Input Processing Output
Number1 Total
Number2
Number3
Example 1. Add Three Number
• A program is required to read three numbers, add them
together and print their total.

• Underline the verbs and adverbs used in the specification


• establish the action required.

Defining Diagram:
Input Processing Output
Number1 Total
Number2
Number3
Example 1. Add Three Number
• A program is required to read three numbers, add them
together and print their total.

• Underline the verbs and adverbs used in the specification


• establish the action required.

Defining Diagram:
Input Processing Output
Number1 Read three numbers Total
Number2 Add numbers together
Number3 Print total number
Example 2. Find average temperature
• A program is required to prompt the terminal operator for the
maximum and minimum temperature readings on a particular
day, accept those readings as integers, and calculate and
display to the screen the average temperature, calculated by
(maximum temperature + minimum temperature)/2.
Example 2. Find average temperature
• A program is required to prompt the terminal operator for the
maximum and minimum temperature readings on a particular
day, accept those readings as integers, and calculate and
display to the screen the average temperature, calculated by
(maximum temperature + minimum temperature)/2.
Defining Diagram:
Input Processing Output
Max_temp Avg_temp
Min_temp
Example 2. Find average temperature
• A program is required to prompt the terminal operator for the
maximum and minimum temperature readings on a particular
day, accept those readings as integers, and calculate and
display to the screen the average temperature, calculated by
(maximum temperature + minimum temperature)/2.
Defining Diagram:
Input Processing Output
Max_temp Prompt for temperatures Avg_temp
Min_temp Get temperatures
Calculate average temperature
Display average temperature
Example 3. Compute mowing time
• A program is required to read from the screen the length and
width of a rectangular house block, and the length and width
of the rectangular house that has been built on the block. The
algorithm should then compute and display the mowing time
required to cut the grass around the house, at the rate of two
square meters per minute.
Example 3. Compute mowing time
• A program is required to read from the screen the length and
width of a rectangular house block, and the length and width
of the rectangular house that has been built on the block. The
algorithm should then compute and display the mowing time
required to cut the grass around the house, at the rate of two
square meters per minute.

Input Processing Output


Block_length Mowing_time
Block_width
House_length
House_width
Example 3. Compute mowing time
• A program is required to read from the screen the length and
width of a rectangular house block, and the length and width
of the rectangular house that has been built on the block. The
algorithm should then compute and display the mowing time
required to cut the grass around the house, at the rate of two
square meters per minute.

Input Processing Output


Block_length Prompt for block measurements Mowing_time
Block_width Get block measurements
House_length Prompt for house measurements
House_width Get house measurements
Calculate mowing area
Calculate mowing time
Introduction to Program Data
• Three main discussion:
• Variables is the name given to a collection of memory cell, designed
to store particular data item. The value stored may change or vary
as the program executes.
• Constants is a data item with a name and a value that remain the
same during the execution of the program.
• Literals is a constant whose name is the written representation of its
value.
Data Types
• Should be clearly defined at the beginning of the program.
• Can be:
• Elementary data items
• Data structures
Elementary Data Items
• Contain single variable that is always treated as a unit.
• The most common elementary data types:
• Integer : representing a set of whole numbers, positive, negative or
zero. e.g. 3, 576, -5
• Real : representing a set of numbers, positive or negative, which
may include values before or after a decimal point. Sometimes
referred to as floating point numbers. e.g. 19.2, 1.92E+01, -0.01
• Character : representing the set of characters on the keyboard, plus
some special characters. e.g. ‘A’, ‘b’, ‘$’
• Boolean : representing a control flag or switch, which may contain
one of only two possible values; true or false.
Data Structures
• Data structure is an aggregate of other data items.
• Its component could be elementary data items or another data
structure.
• Data is grouped together in a particular way to reflects the
situation with which the program is concerned.
• The most common data structures
• String: a collection of characters that can be fixed or variable.
• Array: a data structure that is made up of a number of variables or
data items that all have the same data type and are accessed by the
same name.
• Record: a collection of data items or fields that all bear some
relationship to one another.
• File: a collection of records.
Files
• A popular method to enter and store information.
• Major advantage of using files are:
• Several different program can access the same data
• Data can be entered and reused several times.
• Data can be easily updated and maintained.
• The accuracy of the data is easier to enforce.
• Two different methods of storing data on files:
• Sequential or text files, where data is stored and retrieved
sequentially, may be opened to read or to write, but not both
operations on the same file.
• Direct or random-access files, where data is stored and retrieved
randomly, using a key or index, can be opened to read and write on
the same file.
Thank You

You might also like