Problem Solving Using Computer Chapter 2 Prepared by Kabin Devkota Udita Bista C Programming Problem Solving Steps 1. Problem Analysis 2. Algorithm development and flowchart 3. Pseudocode 4. Coding 5. Compilation and Execution 6. Debugging and testing 7. Program Documentation 1. Problem Analysis client's problem are properly analyzed gives a clear, concise problem statement should specify the following tasks: a. well defined objectives b. input requirements c. output requirements d. processing requirements e. feasibility analysis 2. Algorithm development and flowchart Algorithm A set of ordered steps required to develop a program written without any programming language specific symbols/syntax Guidelines use plain language do not use any programming language syntax every job to be done should be described clearly single entry and exit point Properties of excellent algorithm a. b. c. d. e. finiteness : finite number of steps definiteness : action of each step should be defines clearly without ambiguity input : inputs should be defined precisely output : algorithm should result in one or more output effectiveness : should be more effective among different ways of solving problems Flowchart algorithm in a diagrammatic form that illustrates the sequence of operations to be performed facilitates communication between programmers and business people Advantages of using flowcharts a. Communication b. Effective analysis c. Proper documentation d. Efficient coding e. Proper debugging f. Efficient program maintenance Symbols used in flowchart Basic guidelines to be followed to draw a flowchart i. Standard flowchart symbols should be used according to the purpose. ii. There should be a logical start and stop. iii. It should be clear, neat and easy to follow. There should not be any room for ambiguity in understanding the flowchart. iv. The direction of the flow of a procedure or system is from left to right or top to bottom v. English like language should be used in flowcharts, not specific programming language vi. It is useful to test the validity of the flowchart be passing through it with a simple test data add two numbers Algorithm Step 1 : Start Step 2 : Declare variable a,b,sum Step 3 : Read values of a and b Step 4: Add a and b and assign the result to sum sum a+b Step 6: Print sum Step 7: Stop Flowchart 3. Pseudocode natural language construct modelled to look like statements available in many programming language mixture of structured english and actual code that is used in program development pseudocode is made up of statements written to depict the steps, in the correct sequence, required to solve a specific problem begin read values of a and b calculate a+b sum = a+b print sum end Why is pseudocode necessary? helps to understand the program specifications in advance programmers will have encountered most of the possible problems and know the possible solutions boosts software development techniques by reducing time and increasing efficiency easier to read and understand Write algorithm, pseudocode, flowchart to read marks of a student in a subject and display whether he is pass or fail in the exam. The pass mark is 40 Algorithm Pseudocode Step 1: Start Step 2: Declare variable marks Step 3: Read marks Step 4: check marks if marks greater than or equal to 40 then display pass else display fail Step 5: Stop begin read marks in a subject IF marks >= 40 THEN print pass ELSE print fail ENDIF end Flowchart 4. Coding process of transforming algorithm or flowchart into computer understandable form can be done in any higher or low level language Tips for good programming follow the standards of coding i.e. proper indentations and comments always use meaningful names and labels keep modules short make modules general such that it can be used for other purpose 5. Compilation and execution the process of converting a program written in high level language(source code) to an executable program source code is converted to object code through a compiler then, object code is linked with the library functions through linker; this gives real values to all the symbolic address in the object code, thereby producing an executable program Source program Compiler Object code/file Runtime library/ library function Linker Executable program 6. Debugging and testing process of detecting and removing errors in a program Types of errors a. Syntax error: results when rules of language(i.e. syntax) is violated b. Run-time error: errors such as mismatch of datatypes, referencing an out of range array elements c. Logical error: errors related to logic of the program execution; actions as taking the wrong path, failure to consider a particular condition and incorrect evaluation of statements d. Latent error: hidden errors that show up only when particular set of data is used(eg. a=(x+y)/(p-q), this generates error when p=q) Debugging techniques a. Error isolation: locating errors by deleting certain portion of code and executing it again to check if the error appears or not b. Tracing: printing values of important variables at different stages of a program and deciding whether values are correct or not c. Watch values: watching value of a variable or expresison continuously as the program executes d. Breakpoints: a temporary stopping point within programs; can check for errors until the breakpoints e. Stepping: process of executing one instruction at a time Testing process of executing a program or running a system with the intent of finding errors improves quality verification and validation a. Human Testing code inspection by programmer and test group review by peer group b.Computer based testing(compiler testing and run-time testing) running programs with test data to check whether they give correct or not Testing procedure i. Black Box Testing it is an external test called functional test in which the program is tested to check whether it performs properly or not it is the test of input/output behavior without considering the internal structure of the test objects ii. White Box Testing it is a line-by-line testing method in which interior errors are examined and recovered it is concerned with the correctness of each statement 7. Program documentation all the processes must be properly documented for future reference it is the process of making a program well structured by using appropriate comments and other techniques Merits of documentation it increases the interaction between software and individual who manage it and improves user efficiency it is very important to make a software more attractive Write algorithm and flowchart to find the greatest number among three numbers Algorithm First Way Step 1: Start Step 2: Declare variables a, b, c Step 3: Read a, b, c Step 4: Check a, b, c if a > b and a > c then display a is greatest else if b > a and b > c then display b is greatest else display c is greatest Step 5: Stop Algorithm Second Way Step 1: Start Step 2: Declare variables a, b, c Step 3: Read a, b, c Step 4: Check a, b, c if a > b then if a > c then display a is greatest else display c is greatest else if b > c then display b is greatest else display c is greatest Step 5: Stop Write algorithm and flowchart to check if a number if exactly divisible by 5 but not by 7 Algorithm Step 1: Start Step 2: Declare variables n Step 3: Read n Step 4: Check n if n mod 5 is equal to 0 and n mod 7 is not equal to 0 then display yes else display no Step 9: Stop Write algorithm and flowchart to calculate the factorial of a given number Algorithm Step 1: Start Step 2: Declare variables n, fact Step 3: Read n Step 4: if n = 0, display 1 Step 5: Set value of fact = 1 Step 6: Calculate fact as fact = fact * n Step 7: n <-- n - 1 Step 8: Repeat steps 6 and 7 until n becomes 0 Step 9: Display fact Step 10: Stop Write algorithm and flowchart to display a fibonacci series upto a given number of terms Algorithm Step 1: Start Step 2: Declare variables a, b, c, n Step 3: Read n Step 4: Set the values of a <-- 0 and b <-- 1 Step 5: Display a and b Step 6: Calculate c using c <-- a + b Step 7: Display c Step 8: Set the values of a and b as a <-- b, b <-- c Step 9: Repeat steps 6 to 8 (n-3) times Step 10: Stop Flowchart Write algorithm and flowchart to swap values of two variables Algorithm Step 1: Start Step 2: Declare variables a,b,temp Step 3: Set values of a and b Step 4: Display a and b Step 5: Perform the operations temp <-- a a <-- b b <-- temp Step 6: Display a and b Step 7: Stop Flowchart Write algorithm and flowchart to convert an alphabet which is in capital to small and vice versa Algorithm Step 1: Start Step 2: Declare variable c Step 3: Read character c Step 4: Display c Step 5: If c is greater than or equal to 97 and c is less than or equal to 122 then convert c into small using c <-- c - 32 else if c is greater than or equal to 65 and c is less than or equal to 90 then c <-- c + 32 else display invalid input and go to step 7 Step 6: Display c Step 7: Stop Flowchart