Successfully reported this slideshow.
Upcoming SlideShare
×

# Unit 2

1,194 views

Published on

Published in: Technology, Education
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### Unit 2

1. 1. F1001 PROGRAMMING FUNDAMENTALS UNIT 2PROBLEM SOLVING Programming Life Cycle Program Design 17
2. 2. F1001 PROGRAMMING FUNDAMENTALSPROGRAMMING 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: PROBLEM ANALYSIS PROGRAM DESIGN PROGRAM CODING TESTING AND DEBUGGING MAINTENANCE DOCUMENTATIONSTEP 1: PROBLEM ANALYSISPurpose:• 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. o The needed process. For example: scientific formula or particular theories. 18
3. 3. F1001 PROGRAMMING FUNDAMENTALS 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 INPUT PROCESS OUTPUTSTEP 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
4. 4. 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. Carta alir / Flowchart Penerangan / Explanation START • Enter data from keyboard • Calculate total Input • Display data and total Process Output END • Explanation of basic flowchart symbol:Simbol / Symbol Penerangan / Explanation Algorithma / Algorithm 20 Decision
5. 5. F1001 PROGRAMMING FUNDAMENTALS  Indicate the direction of data flow.  Used to connect a block to another block.Flow Lines  Indicates operations / process  Variable = instruction involved. Process  Receive / read value  Input (list out variable names)  Display value  Output (result)Input / Output <execute instruction that does not  Execute decision based on fulfil the condition> condition.  Test is performed and the program flow continues, based on the result.  Indicates the beginning and end of  START START a flowchart.  END Start / End Flow Lines Input  Show the continuing flowchart in the same page. On-page connector True Condition True Statement False False statement Outpu t 21 END
6. 6. F1001 PROGRAMMING FUNDAMENTALS Symbol Usage Symbol Explanation  Flowchart continues in the next page.Off PageConnector START B Input Outpu t Process END B Next page First page Symbol Usage 22
7. 7. F1001 PROGRAMMING FUNDAMENTALS• 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  Instruct the executed instruction to move to the next function GOSUB  Instruct the executed instruction to return to the next instruction after the GOSUB instruction, called the RETURN function. START area Input area =22 / 7 * radius * radius radius GOSUB Output area area END RETURN Symbol Usage 23
8. 8. F1001 PROGRAMMING FUNDAMENTALS • Example Flowchart: Flowchart to calculate the total of fine for late returning of library books. START Input total_of_day Fine = total_of_day * 0.20 Output Fine Symbol Usage 3. Pseudocode • Steps in problem solving that is END half in programming code and half in human written 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: START Total=0, Average=0 Input a, b, c Total = a + b + c Average = Total / 3 Output a, b, c Output Average END4. Structure Charts • Structure chart is an additional method in preparing programs that has many sub modules. 24
9. 9. F1001 PROGRAMMING FUNDAMENTALS • 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: MainModule SubModule 1 SubModule 2 SubModule 3 • Example 1: SubModule 2.1 SubModule 2.2  Problem: To calculate the amount of water bill Water bill Display Determine Get / read Compute Display Printinstructions the late data the charge the bill the bill for user charge Represent the modules in the program • Example 2:  Problem: To calculate area of a circle  Structure Charts: Area of a circle Display Print instructions Get / read Compute Define Display the for user data the area the area the area area 25 Represent the modules in the program
10. 10. F1001 PROGRAMMING FUNDAMENTALSSTEPS 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. # include <stdio.h> main( ) { int a, b, c, Total; Example of syntax in C double Average; language to display Total=0, Average=0 instruction for user to printf(" Enter 3 numbers:"); enter 3 numbers on scanf("%d %d %d",&a, &b, &c); computer screen. Total = a + b + c; Average = Total / 3; printf("a = %d b = %d c = %d ", a, b, c); printf("Average is = %f", Average); return 0; } 26
11. 11. F1001 PROGRAMMING FUNDAMENTALSSTEP 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: Input sample of Output (functioning well or Executing Program error discovered) data set• Example 1: 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: From the output, has the program produced the expected result? 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. Syntax error will occur if  Corrected during the problem solving process. there is any mistake in the  Also known as run time error. program.  Example output for average is 4, but when it runs, the output is 2. Example: No semicolon at the end of the instruction. Syntax error message Line 9 where syntax error 27 occurred. SYNTAX ERROR
12. 12. F1001 PROGRAMMING FUNDAMENTALS - Logic error is an error that occurs# include <stdio.h> because logical statement in programmain( ) is wrong{ - Example:int a, b, c, Total; Sum is total of the 3 numbers.double Average; Supposedly the average is sum / 3Total=0, Average=0 because there are 3 numbers.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} The wrong Input three numbers: 444 LOGIC ERROR output Average of the numbers: 2 28
13. 13. F1001 PROGRAMMING FUNDAMENTALSSTEP 5: MAINTENANCEDefinition:• 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: 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: DOCUMENTATIONDefinition: 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: 29
14. 14. F1001 PROGRAMMING FUNDAMENTALSo Description of the program.o Specification of program requiremento Program design such as pseudocode and flowcharto List of program and comments (to explain about the program).o Test results.o User’s manual book.o Program capabilities and limitation. 30