PROGRAM SOLVING AND PROGRAM PLANNING Need for problem solving and planning a program

13
PROGRAM SOLVING AND PROGRAM PLANNING Need for problem solving and planning a program Designed by Parul Khurana, LIECA.

Transcript of PROGRAM SOLVING AND PROGRAM PLANNING Need for problem solving and planning a program

PROGRAM SOLVING AND PROGRAM PLANNING

Need for problem solving and planning a program

Designed by Parul Khurana, LIECA.

Introduction Scenario

• Suppose you are asked by your mathematicsteacher to solve an arithmetic problem andyou are not familiar with the steps involved insolving that problem.

• In such a situation, you will not be able tosolve the problem.

Designed by Parul Khurana, LIECA.

Introduction Scenario

• The same principle applies to writing theprogram. A programmer cannot write instructionsfor computer unless the programmer knows howto solve the problem manually.

• Thus, to write an effective and efficient program,it is necessary that the programmer must writeeach and every instruction in the propersequence. However, the sequence of instructions,referred to as Logic, of a program can be verycomplex. Designed by Parul Khurana, LIECA.

Problem Solving

• Therefore, it is very important that beforeattempting to write a program, theprogrammers must solve the problemmanually.

• While solving a problem, the programmer willbe able to learn about the various steps to beperformed and the sequence in which thesesteps are to be performed.

Designed by Parul Khurana, LIECA.

Problem Solving

• To demonstrate how the programmer shouldproceed, let us consider a familiar example offinding the roots of a quadratic equation oftype:

ax2 + bx + c = 0 provided a ≠ 0

• We hope that all of you are familiar withquadratic equation.

Designed by Parul Khurana, LIECA.

Problem Solving

• The roots of such an equation are given by theformula:

where b2-4ac isknown as the discriminant.

• Depending on the sign of the discriminant,there are three mutually exclusive possibilitiesfor the roots:

Designed by Parul Khurana, LIECA.

Problem Solving

• Case 1: If b2-4ac < 0, then the roots areimaginary, and we can compute real part andimaginary part separately as:

Real part = and imaginary part =

• Once we know the real and imaginary part ofthe root, we can determine the actual roots as( real part ± i imaginary part).

Designed by Parul Khurana, LIECA.

Problem Solving

• Case 2: If b2-4ac = 0, then the roots are realand equal, and is computed as:

root =

Designed by Parul Khurana, LIECA.

Problem Solving

• Case 3: If b2-4ac > 0, then the roots are realand distinct and are computed as:

root1 = and root2 =

• Therefore, we will proceed as

Designed by Parul Khurana, LIECA.

Problem Solving

• For a given set of values of a, b and c,compute expression b2-4ac. Now compare thecomputed value of b2-4ac with zero.

• If it is less than zero, compute the roots asshown in Case 1{ Link }.

• If it is equal to zero, compute the roots asshown in Case 2 { Link }.

• And if it is greater than zero, compute theroots as shown in Case 3 { Link }.

Designed by Parul Khurana, LIECA.

Program Planning

• Planning is very important in every walk oflife. Even for our routine kind of things, wehave to plan.

• Planning should not be in the mind only, youmust write it down, i.e., you must documentit.

Designed by Parul Khurana, LIECA.

Program Planning

• Likewise, if you know the steps to be followed forsolving a given problem but you have notdocumented them, you may forget to apply someof the steps or you may apply some of the stepsin wrong sequence.

• Obviously, you will get a wrong answer. Similarly,while writing a program, if the programmerleaves out some of the instructions or writes theinstructions in wrong sequence, the computer willgive a wrong answer.Designed by Parul Khurana, LIECA.

Program Planning• Therefore, in order to write an efficient and effective

program, it is necessary that the programmer mustwrite each and every instruction in the propersequence.

• However, the sequence of instructions, referred to asa logic, of a program can be very complex.

• Hence, in order to ensure that the programinstructions are appropriate for the given problemand are in the correct sequence, programs must beplanned before they are written.Designed by Parul Khurana, LIECA.