0% found this document useful (0 votes)
31 views11 pages

Wa0008.

The document outlines the steps for problem-solving in computing, emphasizing the importance of analyzing the problem, developing an algorithm, coding, and testing/debugging. It introduces key concepts such as GIGO (Garbage In Garbage Out) and the characteristics of a good algorithm, as well as methods for representing algorithms like flowcharts and pseudocode. The document also discusses the flow of control in algorithms, including sequences and decision-making processes.

Uploaded by

Nisha Fathima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
31 views11 pages

Wa0008.

The document outlines the steps for problem-solving in computing, emphasizing the importance of analyzing the problem, developing an algorithm, coding, and testing/debugging. It introduces key concepts such as GIGO (Garbage In Garbage Out) and the characteristics of a good algorithm, as well as methods for representing algorithms like flowcharts and pseudocode. The document also discusses the flow of control in algorithms, including sequences and decision-making processes.

Uploaded by

Nisha Fathima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 11
4.2 STEPS FOR PROBLEM SOLVING q GIGO (Garbage In Garbage out) - ‘The correctness of the output that a computer gives depends upon the correctness of input provided. JA\[ | Problem Solving Steps Figure 4.1: Steps for pro Algorithm A set of exact steps which when followed, solve the problem or accomplish the required task. avehicle starts making a Strange w how to solve the problem, dentify from where the plem cannot be solveq shicle to a mechani. ving, not know hov we need to i Suppose while dri noise. We might right away. First, noise is coming? In case the Pro by us, then we need to take the vel 2 mecl The mechanic will analyse the problem to identify the source of the noise, make a plan about the work to be done and finally repair the vehicle in order to remove the noise. From the above example, it is explicit that, finding the solution to a problem might consist of multiple steps- When problems are straightforward andeasy, we can easily find the solution. But a complex problem requires a methodical approach to find the right solution. In other words, we have to apply problem solving techniques. Problem selving begins with the precise identification of the problem and ends with a complete working solution in terms of a program or software. Key steps required for solving a problem using a computer are shown in Figure 4.1 and are ‘blem solving discussed in following subsections. 4.2.1 Analysing the problem It is important to clearly understand a problem before we begin to find the solution for it. If we are not clear as to what is to be solved, we may end up developing a program which may not solve our purpose. Thus, we need to read and analyse the problem statement carefully in order to list the principal components of the problem and decide the core functionalities that our solution should have. By analysing a problem, we would be able to figure out what are the inputs that our program should accept and the outputs that it should produce. 4.2.2 Developing an Algorithm Itis essential to device a solution before writing a prog am code for a given problem. The solution is represented in natural language and is called an algorithm. We cab agine an alonrithm lile a very well-written recipe ! with clearly defined steps that, if followed, one "up preparing the dish. wie start with a tentative solution plan and keep nn refining the algorithm until the algorithm is able on Tapture all the aspects of the desired solution. For a given problem, more than one algorithm is possible wad we have to select the most suitable solution. The iyorithm is discussed in section 4.3. 4.2.3 Coding ‘After finalising the algorithm, we need to convert the algorithm into the format which can be understood by the computer to generate the desired solution. Different nigh level programming languages can be used for writing a program. It is equally important to record the details of the coding procedures followed and document the solution. at a later This is helpful when revisiting the programs stage. Coding is explained in detail in section 4.8. 4.2.4 Testing and Debugging The program created should be tested on various parameters. The program should meet the requirements ofthe user. It must respond within the expected time. It should generate correct output for all possible inputs. In the presence of syntactical errors, no output will be obtained. In case the output generated is incorrect, then the program should be checked for logical errors, if any. Software industry follows standardised testing methods like unit or component testing, integration testing, system testing, and acceptance testing while developing complex applications. This is to ensure that the software meets all the business and technical requirements and works as expected. The errors or defects found in the testing phases are debugged or rectified and the program is again tested. This continues till all the errors are removed from the program. Once the software application has been developed, tested and delivered to the user, still problems in terms ot functioning can pomeeD and need to be resolved pram times (21 Ee © maintenance of the solution, . ing the problems faced by the user, adish will em! Hx Conrurer S | What sequence of | steps will you follow to | compute the LCM of | two numbers? q ‘The origin of the term Algorithm is traced to Persian astronomer and mathematician, Abu Abdullah Muhammad ibn Musa Al-Khwarizmi (c. 850 AD) as the Latin translation of Al- Khwarizmi was called ‘Algorithm’, science: — CLASS XI ; | J rand even serving 4, i e use! jes of the Virion of features. ii uerit ‘ swering the qu eer request for addition or modifi (THM ah 4.3 ALGOR!' life we perform activities by followip, In our day-to-day Mie wr ‘examples of activities inclug, certain sequence of S00", making breakfast, riding , getting ready for scho ae a puzzle ani dso on. t, bicycle, pos ae ea follow a sequence of steps complete each activity, FI he) Suppose following are the steps required for an activity ‘riding a bicycle’: 1) remove the bicycle from the stand, 2) sit on the seat of the bicycle, 3) start peddling, 4) use breaks whenever needed and 5) stop on reaching the destination. Let us now find Greatest Common Divisor (GCD) of two numbers 45 and 54. Note: GCD is the largest number that divides both the given numbers. Step 1: Find the numbers (divisors) which can divide the given numbers Divisors of 45 are: 1, 3, 5, 9, 15, and 45 Divisors of 54 are: 1, 2, 3, 6, 9, 18, 27, and 54 ae Then find the largest common number from thesi two lists, Therefore, GCD of 45 and 54 is 9 Hence, it is clear that we need to follow a sequenct of steps to accomplish the task. Such a finite sequenc: of steps required to get the desired output is callec Ivrropuction To roadmap, the programmer ma 2 visualise the instructions to Beer enroute see developing a program which may not work as expected, such a roadmap is nothing but the algorithm which is the building block of a computer program. For example searching using a search engine, sending a eee finding a word in a document, booking a taxi through an app, performing online banking, playing computer games, all are based on algorithms. writing an algorithm is mostly considered as a first step to programming. Once we have an algorithm to solve a problem, we can write the computer program for giving instructions to the computer in high level language. If the algorithm is correct, computer will run the program correctly, every time. So, the purpose of using an algorithm is to increase the reliability, accuracy and efficiency of obtaining solutions. (A) Characteristics of a good algorithm + Precision — the steps are precisely stated or defined. * Uniqueness — results of each step are uniquely defined and only depend on the input and the result of the preceding steps. + Finiteness — the algorithm always stops after a finite number of steps. | — the algorithm receives some input. — the algorithm produces some output. + Inpu + Outpu (B) While writing an algorithm, it is required to clearly identify the following: + The input to be taken from the user * Processing or computation to be performe the desired result * The output desired by the user d to get 4.4 RepREesENTATION OF ALGORITHMS Using their algorithmic thinking skills, the software designers or programmers analyse the problem and identify the logical steps that need to be followed to reach a solution. Once the steps are identified, the necd is to ag SCIENCE Crass Xt i equired j = gop tong HE in wn these two common methods g write do There are on u, The r ved output Tee flowchart and pseuilocog, nti AoW ene es ; he following: hm while keeping iD mind the able solut oer howeases the logic of the det ils en, eit. showcase ‘ ail excluding any implementational a eae ereen| + it clearly reveals the flow of control during exeenton of the program 4.4.1 Flowchart — Visual Representation of Algorithm, A flowchart is a visual representation of an algorithn, A flowchart is a diagram made up of boxes, diamonds and other shapes, connected by arrows. Each shape represents a step of the solution process and the arrow represents the order or link among the steps. ‘There are standardised symbols to draw flowcharts, Some are given in Table 4.1. Table 4.1 Shapes or symbols to draw flow charts eal ee peed p Cc) Start/End Also called “Terminator” symbol. It indicates where the flow starts and ends. ‘Also called “Action Symbol,” it represents a process, action, or a single step. A decision or branching point, usually a yes/no or true/ false question is asked, and based on the answer, the path gets split into two branches. and desire representing Kither of the me! Also called data symbol, this parallelogram shape is used to input or output data Connector to show order of flow between shapes. Example 4.1: Write an algorithm to find the square of: number. Before developing the algori' i E gorithm, let us fi ify th input, process and output: Saseany Input: Number whose square is required * Process: Multiph i its square iply the number by itself to ge Output: Si Algorithm to faa hapa bar ae Step 1: Input a numbe: nit Step 2: Compute nu: Step 3: Print Square T and store it to num man num and store it in square algorithm to find square of a number can be The p represented pictorially using flowchart as shown in Figure 4.2. Start Taput num square = num*num Print square Figure 4.2: Flowchart to calculate square of a number Example 4.2: Draw a flowchart to solve the problem of a non-functioning light bulb bulb, 8° fSwitch On | Replace the Bulb Figure 4.3: Flowchart to solve the problem of a non-functioning light bulb C What will happen if an algorithm does not stop after a finite number of steps? Draw a flowchart that represents the attainment of your career goal. KE Compurer SCIENCE ~ CLASS *1 4.4.2 Pseudocode / ode (pronounced Soo-doh-kohd) is another an algorithm. It is considered as a that helps programmers to write algorithm. It is a detailed description of instructions that a computer must follow in a particular order. It is intended for human reading and cannot be executed directly by the computer. No specific standard for writing a pseudocode exists. The word “pseudo” means “not real,” so “pseudocode” means “not real code”. Following are some of the frequently used keywords while writing pseudocode: *° INPUT * COMPUTE * PRINT * INCREMENT * DECREMENT * IF/ELSE * WHILE * TRUE/ FALSE Example 4.3: Write an algorithm to display the sum of two numbers entered by user, using both pseudocode and A pseudoc way of representing non-formal language a pseudocode for flowchart. ‘ng a scoreboard Pseudocode for the sum of two numbers wil will be: hockey match. INPUT numl “ INPUT num2 COMPUTE Result = numl + num2 PRINT Result The flowchart for this algorithms is given in Figure 4.4. Intropuction To Example 4.4: Write an algorithm to calculate area and perimeter of a rectangle, using both pseudocode and flowchart. Pseudocode for calculating area and perimeter of a rectangle. INPUT length INPUT breadth COMPUTE Area = length * breadth PRINT Area COMPUTE Perim = 2 * PRINT Perim The flowchart for this algorithm is given in Figure 4.5. (length + breadth) Start Input length, breadth Area = length * breadth| Print Area i Perim = 2*(length + breadth) End Figure 4.5: Flowchart to calculate area and perimeter of a rectangle * (A) Benefits of Pseudocode Before writing codes in a high level language, a Pseudocode of a program helps in representing the basic functionality of the intended program. By Writing the code first in a human readable language, the Programmer safeguards against leaving out any important step. Besides, for non-programmers, actual Programs are difficult to read and understand, but Pseudocode helps them to review the steps to confirm ‘at the proposed implementation is going to achieve the desire output. of the your sision 4.5 Fiow or Controi The flow of control depicts the flow of events ag represented in the flow chart. The events can flow in a sequence, or on branch based on a decision or even repeat some part for a finite number of times. 4.5.1 Sequence If we look at the examples 4.3 and 4.4, the statements are executed one after another, i.e., in a sequence, Such algorithms where all the steps are executed one after the other are said to execute in sequence. However, statements in an algorithm may not always execute in a sequence. We may sometimes require the algorithm to either do some routine tasks in a repeated manner or behave differently depending on the outcomes of previous steps. In this section, we are going to learn how to write algorithms for such situations. 4.5.2 Selection Consider the map of a neighbourhood as shown in Figure 4.6. Let us assume that the pink building with the red roof is the school; the yellow painted house at the far end of the map is a house. INTRODUCTION TC With reference to Figure 4.6, let us answer the following questions A | «Js there a predefined route for walking from home to school? + Can we have a different route while coming back? ‘As seen from the map, there can be multiple routes between home and school. We might take the shortest route in the morning. But while coming back home in the afternoon, the shortest route might have heavy traffic. Therefore, we could take another route with less traffic. Hence, the above problem involves some decision-making based on certain conditions. Let us look at some other examples where decision making is dependent on certain conditions. For example, (i) Checking eligibility for voting. Depending on their age, a person will either be allowed to vote or not allowed to vote: + If age is greater than or equal to 18, the person is eligible to vote «If age is less than 18, the person is not eligible to vote (ii) Let us consider another example If a student is 8 years old and the student likes Maths put the student in Group A Otherwise Put the student in Group B In which group will these students go as per the above condition? Outcome * 8-year-old Ravi who does not like Maths: Group B * 8-year-old Priti who likes Maths: Group A * 7-year-old Anish who likes Maths: Group B In these examples, any one of the alternatives is selected based on the outcome of a condition. Conditionals are used to check possibilities. The program checks one or more conditions and perform operations (sequence of actions) depending on true or false value of the condition. These true or false values are called binary values. a perro yours Souvna rithm ag INTRODUCTION To PROBLEM Sot. pein Example 4.6: Let us write a pseudocode and draw a Notes flowchart where multiple conditions are checked to categorise a person as either child (<13), teenager (>=13 but <20) or need to adult (>=20),based on age specified: fulfilleq * Input: Age 1 write: + Process: Check Age as per the given criteria on 4 * Output: Print either “Child”, “Teenager”, “Adult” on . Pseudocode is as follows: INPUT Age tH 5 IF Age < 13 THEN PRINT "Child" ELSE IF Age < 20 THEN esented PRINT "Teenager" ional is ELSE PRINT "Adult" whether ‘The flowchart representation of the algorithm in shown in Figure 4.9 Cee) or not follows: shown 1 9: Flowchart to check multiple conditions . “ ons Example 4.7: Algorithm for a card game called “Drag and Wizards”. 7 Make two teams DRAGONS ee ws: The rules for the game are as folloy Team sf the card drawn is a diamond or a club, ets a point ; DRAGONS eer wn is a heart which is a number, *If the card drawn is a hi Team WIZARDS gets @ point or odd

You might also like