unit b - chapter 5 problem solving methodology

14
GVNS I PUC Computer Science Notes Chapter 5 PROBLEM SOLVING METHODOLOGY Prepared by: Krishna S Shetty Page 1 of 14 UNIT B - Chapter 5 PROBLEM SOLVING METHODOLOGY BLUEPRINT 1 MARK 2 MARK 3 MARK 5 MARK TOTAL 1 1 1 1 11 One Mark Questions and Answers 1 Define Problem solving Problem solving can be defined as the task of expressing the solution of complex problems in terms of simple operations understood by the computer. Algorithm An algorithm is a step by step representation of a solution to a problem. Flowchart A flowchart is a pictorial (graphical) representation of a solution to a problem. Coding Converting the steps written using design tool to actual programming language such as C++. Debugging The process of detecting and correcting errors in a program is called debugging. Testing Testing is the process to check the program whether it gives the right output as per the requirement of the user. Documentation Documentation is the reference material which gives the details regarding the program. Maintenance Maintenance is periodic review of the program and update (modify) the program as per the new requirements of the user. Source Program Source program is the program written in high level language. Object Program The machine language program generated by the compiler is referred to as object program. Syntax Error The grammatical mistake in the statement of the program is called syntax error.

Upload: others

Post on 11-Dec-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: UNIT B - Chapter 5 PROBLEM SOLVING METHODOLOGY

GVNS I PUC Computer Science Notes Chapter 5 – PROBLEM SOLVING METHODOLOGY

Prepared by: Krishna S Shetty Page 1 of 14

UNIT B - Chapter 5 – PROBLEM SOLVING METHODOLOGY BLUEPRINT

1 MARK 2 MARK 3 MARK 5 MARK TOTAL

1 1 1 1 11

One Mark Questions and Answers

1 Define Problem solving Problem solving can be defined as the task of expressing the solution of complex problems in terms of simple operations understood by the computer. Algorithm An algorithm is a step by step representation of a solution to a problem. Flowchart A flowchart is a pictorial (graphical) representation of a solution to a problem. Coding Converting the steps written using design tool to actual programming language such as C++. Debugging The process of detecting and correcting errors in a program is called debugging. Testing Testing is the process to check the program whether it gives the right output as per the requirement of the user.

Documentation Documentation is the reference material which gives the details regarding the program. Maintenance Maintenance is periodic review of the program and update (modify) the program as per the new requirements of the user. Source Program Source program is the program written in high level language. Object Program The machine language program generated by the compiler is referred to as object program. Syntax Error The grammatical mistake in the statement of the program is called syntax error.

Page 2: UNIT B - Chapter 5 PROBLEM SOLVING METHODOLOGY

GVNS I PUC Computer Science Notes Chapter 5 – PROBLEM SOLVING METHODOLOGY

Prepared by: Krishna S Shetty Page 2 of 14

Semantic Error An error, which occurs due to improper use of statement in programming language is called semantic error. Logical Error The mistake in the logic of the program is called logical error. Run-time Error The error that occurs while executing (running) the program is called run-time error. Sequence Construct Sequence is the ability of a programming language to execute the statement of a program one after the other from the beginning to the end.

Selection Construct Selection is the ability of a programming language to allow the user to decide on the execution of statements based on a condition.

Iterative Construct Iteration is the ability of a programming language to allow the user to repeat the execution of statements again and again until condition is satisfied. Structured Programming The structured programming language allows a programmer to code a program by dividing the whole program into smaller units or modules. Module A self-contained collection of steps that solves a problem or sub problem is called as module.

Modularity Modularity is a technique where a given problem is divided into a number of independent program segments.

2 How are flowcharts classified? Flowcharts are classified into two types a) Program Flowchart b) System Flowchart

3 What are the different basic programming constructs? The different basic programming constructs are a)Sequence b)Selection c) Iteration

Page 3: UNIT B - Chapter 5 PROBLEM SOLVING METHODOLOGY

GVNS I PUC Computer Science Notes Chapter 5 – PROBLEM SOLVING METHODOLOGY

Prepared by: Krishna S Shetty Page 3 of 14

Two/Three Marks Questions with Answers 1. Write an algorithm to interchange the value of 2 variables using third

variable. start input a,b temp=a a=b b=temp output a, b end

2. Write an algorithm to interchange the value of 2 variables without using third variable. start input a,b a=a+b b=a-b a=a-b output a, b end

3. Write an algorithm to find the area and circumference of a circle. start input r pi=3.14 area=pi*r*r circum=2*pi*r print area, circum end

4. Write any three advantages of Structured Programming. The advantages of structured programming are:

Easier to read and understand.

Easy to detect and correct errors.

Easy to maintain the program. Five Marks Questions with Answers 1. Explain the stages of problem solving methodology.

The stages of problem solving methodology are:

Problem Definition

Problem Analysis

Use of Design Tool - Algorithm and Flowchart

Page 4: UNIT B - Chapter 5 PROBLEM SOLVING METHODOLOGY

GVNS I PUC Computer Science Notes Chapter 5 – PROBLEM SOLVING METHODOLOGY

Prepared by: Krishna S Shetty Page 4 of 14

Coding

Debugging and Testing

Documentation

Maintenance Problem definition - The first step is to understand the problem very well. Unless you understand the problem, you won’t be able to solve the problem. Problem analysis - Practically it’s the first step to solve the problem. In this step, you plan about input data and desired output. You also plan regarding required input and output devices. Use of design tool - algorithm and flowchart - Design tools such as algorithm and flowchart is used to solve the problem in a step-by-step procedure (process). In case of flowchart, these steps are presented in a pictorial (graphical) form. Coding - Converting the steps written using design tool to actual programming language such as C++ is coding. Debugging and testing - Debugging is the process to detect and correct the errors (bugs) in the program. Testing is the process to check the program whether it gives the right output as per the requirement of the user. Documentation - Documentation is the reference material which gives the details regarding the program. Maintenance - Maintenance is periodic review of the program and update (modify) the program as per the new requirements of the user.

2. Mention the advantages of algorithm.

We can solve the problem in a step-by-step process.

It’s written using human language such as English.

It is independent of programming language.

It is easy to detect and correct the error.

It is used to convert to Flowchart or coding.

3. Mention the advantages of flowchart.

We can solve the problem in a step-by-step process.

Steps are represented in graphical (pictorial) form.

It’s written using human language such as English.

It is independent of programming language.

It is used to convert to coding.

Page 5: UNIT B - Chapter 5 PROBLEM SOLVING METHODOLOGY

GVNS I PUC Computer Science Notes Chapter 5 – PROBLEM SOLVING METHODOLOGY

Prepared by: Krishna S Shetty Page 5 of 14

4. Write an algorithm to find the largest number in a input of three numbers. start input a, b, c if (a>b) then

if(a>c) then print a is largest

else print c is largest

else if (b>c) print b is largest

else print c is largest

endif end

5. Draw the different symbols used in flowchart along with their purpose.

Page 6: UNIT B - Chapter 5 PROBLEM SOLVING METHODOLOGY

GVNS I PUC Computer Science Notes Chapter 5 – PROBLEM SOLVING METHODOLOGY

Prepared by: Krishna S Shetty Page 6 of 14

9. What is an error? Explain the different types of errors. There are four types of errors. They are:

o Syntax errors o Semantic errors o Logical errors o Run-time errors

Syntax Error - The grammatical mistake in the statement of the program is called syntax error. Example int i Error – semicolon missing. Semantic Error - An error, which occurs due to improper use of statement in programming language is called semantic error. Example int i; i=i+1; Error – the variable i does not contain any value. Logical Error –The mistake in the logic of the program is called logical error. Example area=pi*radius; Error – formula used to find the area of a circle is wrong. Run-time Error – The error that occurs while executing (running) the program is called run-time error. Example int i=1; while(i<=10) { cout<<i; } Error – variable i is not incremented. 10. Explain simple if statement with general format (syntax) and suitable

example. Simple if helps to decide the execution of a particular statement based on a

condition. This statement is also called as one-way branch.

Page 7: UNIT B - Chapter 5 PROBLEM SOLVING METHODOLOGY

GVNS I PUC Computer Science Notes Chapter 5 – PROBLEM SOLVING METHODOLOGY

Prepared by: Krishna S Shetty Page 7 of 14

Syntax/General Format if (test condition) statement 1; statement n; Here, the condition is tested first. If the condition is true it performs

statement 1. After that it performs statement n. Statement n is the remaining statements found in the program.

Example Discount offer in the shopping mall

Billamount Discount Offer >=5000 10%

<5000 0% discount=0; if(billamount > = 5000 ) discount = billamount * (10/100); netamount = billamount – discount; In the above example, if the transaction amount is 5000 or above then 10% discount is given, else no discount is given.

11. Explain if else statement with general format (syntax) and suitable example. If else helps to decide whether a set of statements should be executed or

another set of statements should be executed based on condition. This statement is also called as two-way branch.

Syntax/General Format if (test condition) statement 1; else statement 2; statement n; Here, the condition is tested first. If the condition is true it performs

statement 1. If the condition is false it performs statement 2. After that it performs statement n. Statement n is the remaining statements found in the program.

Example Discount offer in the shopping mall

Page 8: UNIT B - Chapter 5 PROBLEM SOLVING METHODOLOGY

GVNS I PUC Computer Science Notes Chapter 5 – PROBLEM SOLVING METHODOLOGY

Prepared by: Krishna S Shetty Page 8 of 14

Billamount Discount Offer

>=5000 10% <5000 0%

if(billamount > = 5000 ) discount = billamount * (10/100); else discount=0; netamount = billamount – discount; In the above example, if the transaction amount is 5000 or above then 10% discount is given, else no discount is given.

12. Explain if else if statement with general format (syntax) and suitable example. If else if helps the programmer to decide the execution of a statement from

multiple statements based on a condition. There will be more than one condition to test. This statement is also called as multiple-way branch.

Syntax/General Format if (test condition 1) statement 1; else if (test condition 2) statement 2; else if (test condition n) statement n; else default statement; Here, condition 1 is tested. if it is true, statement 1 is executed Otherwise, condition 2 is tested. if it is true, statement 2 is executed and so

on. If none of the condition is satisfied (true), then it performs the default

statement. Example Discount offer in the shopping mall

Billamount Discount Offer

>=5000 30% >=2000 20%

<2000 0%

Page 9: UNIT B - Chapter 5 PROBLEM SOLVING METHODOLOGY

GVNS I PUC Computer Science Notes Chapter 5 – PROBLEM SOLVING METHODOLOGY

Prepared by: Krishna S Shetty Page 9 of 14

if( billamount > = 5000) discount = billamount * (30/100); else if (amount>=2000) discount = billamount * (20/100); else discount = 0; netamount=amount-discount; In the above example, if the transaction amount is 5000/2000 or above then 30%/20% discount is given accordingly, else no discount is given.

13. Explain nested if statement with general format (syntax) and suitable example. Nested if statement is if statement within another if statement. Syntax/General Format if (test condition 1)

if (test condition 2) statement 1; else statement 2; else if (test condition 3) statement 3; else statement 4; Here, condition1 is tested first. If it’s true, then condition2 is tested. If it is

true performs statement 1 or else performs statement 2. If condition 1 is false, condition 3 is tested. If it is true, performs statement 3 or else performs statement 4 and so on.

Example

Algorithm to find the greatest of three numbers if ( a>b )

if ( a > c ) print a else print c else if ( b > c ) print b else

Page 10: UNIT B - Chapter 5 PROBLEM SOLVING METHODOLOGY

GVNS I PUC Computer Science Notes Chapter 5 – PROBLEM SOLVING METHODOLOGY

Prepared by: Krishna S Shetty Page 10 of 14

print c

In the above example, conditions are tested as explained above and accordingly the greatest number is displayed.

14. Explain multiple selection construct (switch) statement with general

format (syntax) and suitable example. If there are more than two alternatives (options) to be selected, multiple selection construct or switch statement is used. Syntax/General Format switch ( expression ) { case label 1 : statement 1; break; case label 2 : statement 2; break; case label n : statement n; break; default : default statement; } Here, expression value is compared with case value. If it matches, then performs the statement found in that block or else performs default statement. Break is used to come out of the switch statement. Example Algorithm to display day for day number entered switch (dayno) {

case 1 : print “Sunday”; break; case 2 : print “Monday”; break; case 3 : print “Tuesday”; break; case 4 : print “Wednesday”; break; case 5 : print “Thursday”; break;

Page 11: UNIT B - Chapter 5 PROBLEM SOLVING METHODOLOGY

GVNS I PUC Computer Science Notes Chapter 5 – PROBLEM SOLVING METHODOLOGY

Prepared by: Krishna S Shetty Page 11 of 14

case 6 : print “Friday”; break; case 7 : print “Saturday”; break; default : print “invalid day number”;

} In the above example, if the day number matches with case number then day found in that block is displayed or else default message is displayed.

15. Explain while construct (loop) with general format (syntax) and suitable example. While is a pre-tested loop where the condition is tested first. It performs the statements found in that block as long the condition is true. Syntax/General Format while ( test condition) { statement 1; statement 2; } Example Algorithm to display numbers from 1 to 10 i=1; while(i<=10) { print i; i=i+1; }

In the above example, numbers from 1 to 10 are displayed.

16. Explain do while construct (loop) with general format (syntax) and

suitable example. Do while is a post- tested loop where the statements found in the block is

performed first then the condition is tested. It keeps performing the statements as long the condition is true.

Page 12: UNIT B - Chapter 5 PROBLEM SOLVING METHODOLOGY

GVNS I PUC Computer Science Notes Chapter 5 – PROBLEM SOLVING METHODOLOGY

Prepared by: Krishna S Shetty Page 12 of 14

Syntax/General Format do { statement 1; statement 2; } while(test condition); Example Algorithm to display numbers from 1 to 10 i=1; do { print i; i=i+1; } while(i<10);

In the above example, numbers from 1 to 10 are displayed.

17. Explain for construct (loop) with general format (syntax) and suitable example. For loop is the fixed execution loop. The condition is tested first. It performs the statements found in that block as long the condition is true. Syntax/General Format for (Expression 1; Expression 2; Expression 3) { Statement 1; } Where, Expression 1 represents Initialization Expression 2 represents Condition Expression 3 represents Increment/Decrement Example Algorithm to display numbers from 1 to 10 for (int i=1; i<=10; i++) print i; In the above example, numbers from 1 to 10 are displayed.

Page 13: UNIT B - Chapter 5 PROBLEM SOLVING METHODOLOGY

GVNS I PUC Computer Science Notes Chapter 5 – PROBLEM SOLVING METHODOLOGY

Prepared by: Krishna S Shetty Page 13 of 14

18. Differentiate between while and do while construct (loop). The difference between while and do while construct (loop) are:

while do while

This is pre- tested loop This is post tested loop

Doesn’t perform the statements if the condition if false.

Performs the statement atleast once even if the condition is false.

Minimum execution of loop is zero Minimum execution of loop is once.

Semi colon is not used to end the loop. Semi colon is used to end the loop.

while ( test condition) { statement 1; statement 2; }

do { statement 1; statement 2; }while(test condition);

19. Explain Top Down Analysis.

Top-down design involves dividing a problem into sub-problems and further dividing the sub problems into smaller sub-problems until it leads to sub-problems that can be implemented as program statements.

Example

Where A is the main problem and B, C, D are sub programs of A. B is

further divided as E and F. Same way, D is divided as G and H. The top-down approach is taken for program design; the programs can be developed easily and quickly with minimum number of errors. 20. What is a module? Write advantages of modularity.

Module is a self-contained collection of steps that solves a problem or sub problem.

Page 14: UNIT B - Chapter 5 PROBLEM SOLVING METHODOLOGY

GVNS I PUC Computer Science Notes Chapter 5 – PROBLEM SOLVING METHODOLOGY

Prepared by: Krishna S Shetty Page 14 of 14

Advantages of modularity are:

It reduces the complexity of large module.

Working on small module is always better.

Easy to detect and correct errors.

We can reuse the existing code.

Easy to update the module.

_________________