unit 2

17
F1001 PROGRAMMING FUNDAMENTALS PROBLEM SOLVING Programming Life Cycle Program Design 17 UNIT 2

Upload: rohassanie

Post on 22-May-2015

848 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Unit 2

F1001 PROGRAMMING FUNDAMENTALS

PROBLEM SOLVING

Programming Life Cycle

Program Design

17

UNIT

2

Page 2: Unit 2

F1001 PROGRAMMING FUNDAMENTALS

PROGRAMMING LIFE CYCLE

Life Cycle: Refers to the changes made from an old to a new program (the cycle starts again).

Programming Life Cycle: A framework or discipline, which uses certain techniques needed in

computer programming development.

Steps involved in programming life cycle:

STEP 1: PROBLEM ANALYSIS

Purpose:

To describe in detail a solution to a problem and information needed in solving the problem.

How?

Study and understand the problem.

Identify:

o The needed input.

o The required output.

18

PROBLEM ANALYSISPROBLEM ANALYSIS

PROGRAM DESIGNPROGRAM DESIGN

PROGRAM CODINGPROGRAM CODING

TESTING AND DEBUGGINGTESTING AND DEBUGGING

MAINTENANCEMAINTENANCE

DOCUMENTATIONDOCUMENTATION

Page 3: Unit 2

F1001 PROGRAMMING FUNDAMENTALS

o The needed process. For example: scientific formula or particular theories.

Problem 1: Write a program that will get 3 numbers as input from the users. Find the average and

display the three numbers and its average.

Problem Analysis:

Input: 3 numbers.

Process: 1. Add the three numbers

2. Divide the total of the three numbers with 3

Output: The three numbers and its average

STEP 2: PROGRAM DESIGN

Definition: It is a framework or flow that shows the steps in problem solving.

Methods to design a program:

1. Algorithm

Algorithm is a sequence of instructions to solve a problem, written in human language, and it

can solve any problems when it is used with the correct procedure.

A programmer writes the problem solving in the form of an algorithm before coding it into

computer language.

Example of algorithm to calculate the average of three numbers:

1. Set Total=0, Average=0;

2. Input 3 numbers

3. Total up the 3 numbers

Total= total of 3 numbers

4. Calculate average

Average=Total/3

5. Display 3 numbers and the average

19

INPUTPROCESS

OUTPUT

Page 4: Unit 2

F1001 PROGRAMMING FUNDAMENTALS

2. Flowchart

A graphical representation of data, information and workflow using certain symbols that are

connected to flow lines to describe the instructions done in problem solving.

It shows the flow of the process from the start to the end of the problem solving.

Explanation of basic flowchart symbol:

Simbol / Symbol Penerangan / Explanation Algorithma / Algorithm

Indicate the direction of data flow.

Used to connect a block to another

block.

Indicates operations / process

involved.

Variable = instruction

Carta alir / Flowchart Penerangan / Explanation

Enter data from keyboard

Calculate total

Display data and total

20

START

Input

Process

Output

END

Flow Lines

Process

Page 5: Unit 2

F1001 PROGRAMMING FUNDAMENTALS

Receive / read value

Display value

Input (list out variable names)

Output (result)

Execute decision based on

condition.

Test is performed and the program

flow continues, based on the result.

<execute instruction that does not

fulfil the condition>

Indicates the beginning and end of

a flowchart.

START

END

Show the continuing flowchart in

the same page.

21

Input / Output

Decision

Start / End

Flow Lines

On-page

connector

Flow Lines

START

Input

Condition

False statement

Output

END

False

True Statement

True

Page 6: Unit 2

F1001 PROGRAMMING FUNDAMENTALS

Symbol Explanation

Flowchart continues in the next

page.

There are few additional symbols of flowchart used in a complicated program: GOSUB and

RETURN. It’s usually used in a big program.

Symbol Explanation

22

Symbol Usage

Output

END

B

START

Input

Process

B

First page

Next page

Symbol UsageSymbol Usage

Off Page

Connector

Page 7: Unit 2

F1001 PROGRAMMING FUNDAMENTALS

Instruct the executed instruction to

move to the next function

Instruct the executed instruction to

return to the next instruction after the

GOSUB instruction, called the

function.

Example Flowchart:

Flowchart to calculate the total of fine for late returning of library books.

23

GOSUB

RETURN

Input

radius

START

END

GOSUB

area

area =22 / 7 * radius * radius

Output

area

area

RETURN

Symbol Usage

START

Input total_of_day

Fine = total_of_day * 0.20

Output Fine

END

Page 8: Unit 2

F1001 PROGRAMMING FUNDAMENTALS

3. Pseudocode

Steps in problem solving that is written half in programming code and half in human

language.

For example, some part uses C language code and some part uses Malay or English language.

Advantages:

Easily understood.

Easily maintained.

The codes are changeable.

Disadvantages:

Cannot be executed in the computer.

Example:

4. Structure Charts

Structure chart is an additional method in preparing programs that has many sub modules.

It consists rectangular boxes, which represents all the sub modules in a program and is connected

by arrows.

It illustrates the top-down design of a program and is also known as hierarchical chart because its

components are in hierarchical form.

The advantage is that it is easy to be drawn and to be changed.

Format:

24

START

Total=0, Average=0

Input a, b, c

Total = a + b + c

Average = Total / 3

Output a, b, c

Output Average

END

Symbol Usage

MainModule

SubModule 2 SubModule 3SubModule 1

SubModule 2.1 SubModule 2.2

Page 9: Unit 2

F1001 PROGRAMMING FUNDAMENTALS

Example 1:

Problem: To calculate the amount of water bill

Example 2:

Problem: To calculate area of a circle

Structure Charts:

25

Represent the modules in the program

Water bill

Get / read data

Compute the charge

Display instructions

for user

Determine the late charge

Display the bill

Print the bill

Represent the modules in the program

Area of a circle

Get / read data

Compute the area

Display instructions

for user

Define the area

Display the area

Print the

area

Page 10: Unit 2

F1001 PROGRAMMING FUNDAMENTALS

STEPS 3: PROGRAM CODING

Definition: Writing problem solving into certain programming language such as C, COBOL and

others.

Problem solving: Instructions before it is coded into programming language.

Purpose: To produce a program to develop a system.

STEP 4: TESTING AND DEBUGGING

Definition of Testing:

Using a set of data to discover errors and to ensure accuracy of the program.

Testing Process:

Diagram indicates the process of testing:

Example 1:

26

Example of syntax in C

language to display

instruction for user to

enter 3 numbers on

computer screen.

# include <stdio.h>

main( )

{

int a, b, c, Total;

double Average;

Total=0, Average=0

printf(" Enter 3 numbers:");

scanf("%d %d %d",&a, &b, &c);

Total = a + b + c;

Average = Total / 3;

printf("a = %d b = %d c = %d ", a, b, c);

printf("Average is = %f", Average);

return 0;

}

Input sample of data set

Executing Program Output (functioning well or

error discovered)

Page 11: Unit 2

F1001 PROGRAMMING FUNDAMENTALS

Assume that a program to find the average of 3 numbers has been coded. Then, execute the program.

Using a few numbers, test the program to verify whether the result is as expected.

Testing 1:

Input: 4,5,8

Testing 2:

Input: 7,8,6

An error is known as bug.

Debugging is a process of identifying and correcting error.

Two types of error:

1. Syntax Error (grammatical error)

Occurs when the rules of programming language are not applied.

Correction is done during the program coding.

The bug can be traced during the compilation.

Also known as compile-time error

Must be corrected before executing and testing the program.

2. Logic error

Cannot be traced by compiler.

Corrected during the problem solving process.

Also known as run time error.

Example output for average is 4, but when it runs, the output is 2.

27

From the output, has the program produced the expected result?

Syntax error will occur if

there is any mistake in the

program.

Example:

No semicolon at the end

of the instruction.

Syntax error will occur if

there is any mistake in the

program.

Example:

No semicolon at the end

of the instruction.

Line 9 where syntax error

occurred.

Syntax error message

SYNTAX ERRORSYNTAX ERROR

Page 12: Unit 2

F1001 PROGRAMMING FUNDAMENTALS

STEP 5: MAINTENANCE

Definition:

Activity that verifies whether the operational system is performing as planned or an activity to modify

the system to meet the current requirement.

The process of changing a system after it has been applied to maintain its ability. The changes may

involve simple changes such as error correcting.

How to do maintenance? By performing activities such as:

28

Input three numbers:4 4 4Average of the numbers: 2

The wrong

output LOGIC ERROR

- Logic error is an error that occurs

because logical statement in program

is wrong

- Example:

Sum is total of the 3 numbers.

Supposedly the average is sum / 3

because there are 3 numbers.

# include <stdio.h>

main( )

{

int a, b, c, Total;

double Average;

Total=0, Average=0

printf(" Enter 3 numbers:");

scanf("%d %d %d",&a, &b, &c);

Total = a + b + c;

Average = Total / 6;

printf("a = %d b = %d c = %d ", a, b, c);

printf("Average is = %f", Average);

return 0;}

Logic Error

Page 13: Unit 2

F1001 PROGRAMMING FUNDAMENTALS

o Testing – Test the ability of the system.

o Measurement – Access data time. Example, time to save, print and others.

o Replacement – Replace the old system to new system.

o Adjustment – Adding needs to new system.

o Repair – For example: An old system cannot update the new data

o Updating – Update the database.

Example: Maintenance of Telecommunication System TELEKOM.

o Telecommunication System TELEKOM provides communication service from one house to

another house. The telephone number is 7 digits.

o Assume that management will change the number of digit from 7 digits to 8 digits.

o Programmer will change the old system to a new system and test the system whether it is

functioning well or not.

o Then, the new systems overall capability will be tested. For example, the capability to identify the

caller’s and the user’s location and time taken to connect them (measurement) will be tested.

o Any old function that is no longer used will be replaced with a new function (replacement).

STEP 6: DOCUMENTATION

Definition: A written or graphical report of the steps taken during the development of a program.

Purpose: It will be useful in the future, in case of modification or maintenance.

Content of Documentation:

o Description of the program.

o Specification of program requirement

o Program design such as pseudocode and flowchart

o List of program and comments (to explain about the program).

o Test results.

o User’s manual book.

o Program capabilities and limitation.

29