02 Program Design

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32
At a glance
Powered by AI
The key takeaways are the 7 steps in program development, components of an algorithm, and different data types discussed such as variables, constants, literals, elementary data items and data structures.

The main steps in program development are: 1) Define the problem, 2) Outline the solution, 3) Develop the outline into an algorithm, 4) Test the algorithm for correctness, 5) Code the algorithm into a specific programming language, 6) Run the program on the computer, 7) Document and maintain the program.

The main components of an algorithm are: values, variables, instructions (sequences, selections, repetitions, procedures), and documentation.

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 nding square root Latin translation (c.1120 CE): Algoritmi de numero Indorum

Book on algebra
Hisab al-jabr wal-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 Scientically

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 specic 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


Dene the problem Outline the solution Develop the outline into an algorithm Test the algorithm for correctness Code the algorithm into a specic programming language Run the program on the computer Document and maintain the program

7 Steps in Program Development


Dene 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 specic programming language Run the program on the computer Document and maintain the program

7 Steps in Program Development


Dene 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 specic programming language Run the program on the computer Document and maintain the program

7 Steps in Program Development


Dene 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 specic programming language Run the program on the computer Document and maintain the program

7 Steps in Program Development


Dene 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 specic programming language Run the program on the computer Document and maintain the program

7 Steps in Program Development


Dene the problem Outline the solution Develop the outline into an algorithm Test the algorithm for correctness Code the algorithm into a specic 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 satised that the program is running as required.

Document and maintain the program

7 Steps in Program Development


Dene the problem Outline the solution Develop the outline into an algorithm Test the algorithm for correctness Code the algorithm into a specic 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 denition 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 elds 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 specication
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 specication
establish the input, output component and any object that are required.

Dening Diagram:
Input Number1 Number2 Number3 Processing Output Total

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 specication
establish the action required.

Dening Diagram:
Input Number1 Number2 Number3 Processing Output Total

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 specication
establish the action required.

Dening Diagram:
Input Number1 Number2 Number3 Processing Read three numbers Add numbers together Print total number Output Total

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. Dening Diagram:
Input Max_temp Min_temp Processing Output Avg_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. Dening Diagram:
Input Max_temp Min_temp Processing Prompt for temperatures Get temperatures Calculate average temperature Display average temperature Output Avg_temp

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 Block_length Block_width House_length House_width Processing Output Mowing_time

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 Block_length Block_width House_length House_width Processing Prompt for block measurements Get block measurements Prompt for house measurements Get house measurements Calculate mowing area Calculate mowing time Output 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 dened 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 oating 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 ag 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 reects the situation with which the program is concerned. The most common data structures
String: a collection of characters that can be xed 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 elds 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 les 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 les:


Sequential or text les, where data is stored and retrieved sequentially, may be opened to read or to write, but not both operations on the same le. Direct or random-access les, where data is stored and retrieved randomly, using a key or index, can be opened to read and write on the same le.

Thank You

You might also like