Upcoming SlideShare
×

# 3 solving problems

853 views

Published on

Published in: Education, Technology
1 Comment
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Hai
I have two hard disk capacity per each 160 GBs but both are not working how to repair in this one is bad disk showing option not formating also and another one not connecting the cable how to fix tall me solution please.fixing or any software or any tricks or etc
Send by my E-Mail ID : rafeeqahmedali@gmail.com or mansoorelahee@rediffmail.com

I have 500 GBs hard disk but that is read / write very slow moving coping etc in thsi bad sector is their witout any damage data how to remove bad sector i trid error and fixing option in propreity checkdisk. tell me soon any body please

Are you sure you want to  Yes  No
• Be the first to like this

Views
Total views
853
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
18
1
Likes
0
Embeds 0
No embeds

No notes for slide

### 3 solving problems

1. 1. 3 Solving problems I keep six honest serving men (They taught me all I knew) Their names are What and Why and When And How and Where and Who. Rudyard Kipling, English poet Overview In this unit we are going to:  investigate the types of problems that are suitable for computer solution  find out what an algorithm is  use pseudocode to represent algorithms  identify the types of error that can occur in a computer program  discuss the of copyright of programs and the rights and responsibilities associated with writing and owning software  look at how file sharing and open source software are changing the face of copyright. Computer suitable problems A software program is the instructions given to a computer in order to carry out a process. The process might be to play a music track, or calculate the total cost of the items being ordered by a customer from an online store, or display an explosion on screen. In each case the program is just a series of steps that have been arranged in the correct order to complete the task. Writing a computer program is a problem solving activity, where the problem is to develop the correct step-by-step instructions to carry out the process you want to achieve. Solving problems Anyone who has attempted to solve a problem, for example in Maths, knows it is not easy to know what to do, where to start, or even if the problem has a solution. Some people are good problem solvers, others are not. When we have a problem most of us want to dive in and have a go at solving it without really knowing what we are doing. This method is called trial and error or sometimes button © Kevin Savage 2011 – Licensed to Hillcrest Christian College for use in 2011-12
4. 4. Solving problems 67 Activity 3.1 – Problem solving 1. a Describe one systematic problem solving method. b What is meant by button mashing? Give an example where it might be successful. c What is reverse engineering? Find a real world example of where reverse engineering has been used. 2. a Explain the difference between an algorithmic solution to a problem and a heuristic solution. Your answer must clearly explain what heuristics are, and point out how they are different to an algorithmic approach. b Give an example of a problem that a human is more likely to be able to solve than a computer, saying why. c Give an example of a problem that a computer is more likely to be able to solve than a human, saying why. 3. Which of the following problems do think would be suitable for algorithmic solution, and which for heuristic solution?: a calculating GST b choosing the winner of a beauty contest c displaying a Facebook page d calculating the area of a wall e determining the meaning of life f choosing the most likely winner of the Melbourne Cup g finding out if a painting is fake. 4. Heuristics can also be described as rules of thumb, or ways you have found of doing things that have worked in the past. You may have experimented before and found a way that works for you, so when you come to a similar problem you apply these “rules of thumb” and develop a solution. You have been using heuristics. What are your heuristics for playing the game noughts and crosses (tic-tac-toe)? You might need to play several games with a friend to explore how you actually do play this game. 5. Imagine you are a TV executive producer and you have to plan a typical evening’s entertainment. You can choose from the following available programs:  The Tender Trap (USA; \$15 000; situation comedy; 24 min; rating .20)  News and Weather (Aust; \$25 000; 22 min; rating .45)  Perspective (UK; \$5 000; documentary; 54 min; rating .14)  Funny Money (Aust; \$25 000; game show; 23 min; rating .62)  What’s New (Aust; \$18 000; current affairs; 24 min; rating .33)  Midweek Movie (USA; \$1 500: various; approx. 82 min; rating .18)  George’s Dream (UK; \$4 000; situation comedy; 25 min; rating .16) © Kevin Savage 2011 – Licensed to Hillcrest Christian College for use in 2011-12
5. 5. Leading Technology 68  Who Knows? (Aust; \$1 500; discussion programme; 55 min; rating .06)  Cricket (Aust; \$55 000; live coverage; 5.30 - 8.30 only; rating .18)  Going, Gone (UK; \$3 000; antiques show; 25 min; rating .06)  Home Ties (Aust; \$30 000; soapie; 54 min; rating .56)  Alive Oh! (USA; \$26 000; situation comedy; 21 min; rating .43) The duration of the program indicates how many adverts can be fitted in e.g. a 24 min program will allow 6 min of adverts in a half hour time slot. The ratings indicate how popular the program is – for example .45 means 45% of people watching at that time are tuned into this program. As producer you have some constraints on what you can do:  your time slot to fill is 5 p.m. to 10 p.m.  your budget is \$130 000 (less if possible)  advertising revenue is highest during peak viewing times of 6 - 8 p.m.  you must maintain at least 50% Australian content of programs. Prepare the evening viewing schedule. 6. Your younger brother has a programmable car that understands the following instructions: left right forward x back x e.g. forward 100 left would move it forward 100 cm and then turn it to the left. Program the car to follow the following course and then reverse into the garage at the end: 100 200 300 60 50 120 7. Henri the new French chef has purchased a programmable microwave oven but is having a problem getting it to work. The instruction book lists six commands: on off high x min med x min low x min wait x min The wait command is to let food stand to allow the heat to penetrate. e.g. To scramble eggs, cooked on high for 5 min the program is: © Kevin Savage 2011 – Licensed to Hillcrest Christian College for use in 2011-12
6. 6. Solving problems 69 on high 5 min off a Describe to Henri what the following program would do: on high 2 min off wait 2 min on low 3 min off b Write a program for Henri to cook a frozen steak to the following requirements: defrost on low for 10 min, then cook on high for 7 min. c Write a program for Henri to cook a soufflé to the following requirements: cook on high for 3 min, stand for 2 min, cook on low for 2 min, stand for 1 min, cook on high for 2 min. 8. Logo is a programming language developed by Seymour Papert to introduce children to programming and problem solving. Logo uses simple commands (similar to the car in Q6) to move a ‘Turtle’ around the floor. Computerised versions employ a triangle on screen to represent the turtle. The turtle is equipped with a pen to draw lines as it moves. The following commands would draw a square: pendown forward 100 right 90 forward 100 right 90 forward 100 right 90 forward 100 right 90 penup The forward command moves the turtle forward 100 spaces and the right command turns it right the given number of degrees (here 90o ). For simplicity, a repeat command can be used to avoid writing the same lines over again. e.g. pendown repeat 4(forward 100, right 90) penup would have the same effect to draw the square. a Write a Logo program to draw an equilateral triangle. (Care: it is not as obvious as it seems.) b Write a Logo program to draw a simple house. © Kevin Savage 2011 – Licensed to Hillcrest Christian College for use in 2011-12
9. 9. Leading Technology 72 When we have all of the required steps worked out in pseudocode we can then get into the detail of coding the algorithms in the computer language we intend to use. Pseudocode shows the program steps that have to be followed in a simple logical way. Instructions such as get, write and = tell what has to happen, and the variables show the values acted upon. We will use pseudocode more widely later to describe the steps in a program. As a further example look at the following algorithm in pseudocode that involves any three input numbers: prompt for first read first prompt for second read second prompt for third read third total = first + second + third average = total / 3 write “The average of the three numbers is ”, average As prompted, the user enters three values that are stored in the variables first, second and third. These are then added together and the answer is stored in total. This result is divided by three and the answer is stored in average. Finally average is written to screen. This pseudocode also demonstrates the general nature of an algorithm. The one shown can work with any input number values. It also could easily be translated into Delphi Pascal, Visual Basic, C++, Java, PL/I or any other procedural computer language. Elements of an algorithm The basic elements of a computer algorithm are:  assignment of a value to a variable  procedure call  skip. The ability to store a value in a variable is known as assignment, i.e. the variable is assigned a value. e.g. count = 10 Here the variable count is assigned or given the value 10. We can store other values in variables: e.g. name = “Angelina Jolie” or colour = red or visible = true A variable is a named place in memory that can store a value. We name the variable so that it can be identified when it is used again. For this reason it is important to choose meaningful variable names so that it is easy to remember the sort of data they hold. (We will investigate the naming of variables in the next unit.) © Kevin Savage 2011 – Licensed to Hillcrest Christian College for use in 2011-12
10. 10. Solving problems 73 Although we use the = in pseudocode to show assignment this should not be read as “equals”, as in Maths. For example the following statement would be meaningless in algebra: count = count + 1 In computer terms this means increase the value held in the variable count by 1, and is read as: “count becomes count plus one”, or “assign count plus one to count”. In addition to variables that hold numbers and text computer algorithms can also work with other forms of data. Later we will look at arrays, records, and other data structures. Algorithms may also call or invoke other algorithms, i.e. one of the steps may be the name of another algorithm. Look at the following example which invokes the algorithm Tidy: Cleaning the house move to lounge Tidy move to main bedroom Tidy move to .... etc. The sub-algorithm Tidy is a self contained process or module. Modules are the basis of structured programming, a very important process we will learn about later. Note the module Tidy may have module calls of its own; line 3 (Vacuum) could be a call to another algorithm module. The final element of an algorithm is a skip. A skip means do nothing. At times we need to be able to make a choice to either carry out a set action or to take no action. The no action is the skip. We will see this when we investigate selection in the next unit. While algorithms consist of these basic elements, in programming we also need processes for input (get) and output (write or print). In addition there are also the composition methods of sequence, selection, iteration and recursion which we will also investigate in the next unit. Activity 3.2 – Step by step 1. a What is an algorithm? b Complete the following table of everyday examples of algorithms (the first is done for you): Process Algorithm Typical Steps knitting a jumper knitting pattern knit 3, purl 1, knit 2 making a cake playing a song on guitar chess game building a model plane Tidy pick up unnecessary objects from floor dust all level surfaces Vacuum straighten furniture .... etc. © Kevin Savage 2011 – Licensed to Hillcrest Christian College for use in 2011-12
11. 11. Leading Technology 74 2. Identify what is wrong with each of the following algorithms (ambiguous, out of sequence, or infinite): a Odd numbers number = 1 write number number = number + 2 go back to step 2 b Party time tidy room get food get drinks put music on dance, dance, dance! c Lesson time start word processor boot computer load file switch computer on save file edit file 3. Look at the following algorithms, determine what is wrong with each, and then rewrite them correctly: a Phone call dial lift handset listen speak hang up b Night out ask dad for money ask mum for car ask friend to go go on date c Planting tree get shovel get seedling put seedling in hole fertilise fill hole turn tap on water plant © Kevin Savage 2011 – Licensed to Hillcrest Christian College for use in 2011-12
12. 12. Solving problems 75 4. The algorithm in 3a above of course would not work if the phone was engaged, or the person you wished to speak to was not there. Rewrite the algorithm to cover these possibilities. 5. Develop algorithms for the following tasks: a Cross a road safely. b Pump up a bicycle tyre. c Run a bath of water at the correct temperature. d Wash a pile of dirty dishes by hand. e Get into the front passenger side of a car and place on the seatbelt. You may assume the car door is unlocked. f Add any two, two-digit numbers together. e.g. 1 6 or 4 9 + 5 7 + 7 8 7 3 1 2 7 6. I have written a novel on word processor that I have saved as chapter1, chapter2, etc. up to chapter6. I decide I wish to add another chapter between chapters 2 and 3 but still keep my numbering in sequence. If I save the new chapter as chapter3 I will lose my old chapter3 (which in turn I now want to be chapter4) as it will be overwritten. Solve this problem and write the solution as an algorithm. 7. Write an algorithm to mow a lawn in which some of the steps call other algorithms. Write these algorithms as well. Types of error There is a story from the early days of computing: A massive mainframe computer was having trouble running a complicated program. The programmers and technicians could not discover the problem, despite many hours of effort. They were about to give up when finally it was found that a fly had become trapped between two vital relays, and had disrupted the program. It is said that the term “a bug in the program” resulted from this. Nowadays any error in the running of a program is called a bug, and removing errors is called debugging. Debugging is a necessary evil in programming. Very few programmers enjoy doing it – but very, very few can write a program without bugs. Some errors are simple and will be picked up by the compiler, but some are insidious and result from flaws in the programmer’s thinking. These are the most difficult to detect and rectify. Fortunately there are techniques to assist in debugging. Programming errors fall into four categories:  syntax errors  runtime errors  logic errors, and © Kevin Savage 2011 – Licensed to Hillcrest Christian College for use in 2011-12
13. 13. Leading Technology 76  intent errors. Syntax errors are errors in the grammar of the computer language being used. These occur when we misspell, put things in the wrong way round, or get the punctuation mixed up. Runtime errors occur in a program when the program compiles correctly but fails to run to completion. This would happen if you tried to use a value that was not recognised; where an overload occurred (e.g. too large a number); or in a situation where division by zero is attempted. A logic error is where the thinking behind the algorithm is wrong. This is usually as a result of the programmer having not designed the algorithm correctly. Examples include: steps are in the wrong order, steps left out, > used instead of < , or faulty reasoning. If a program will not stop because it is caught in an infinite loop this is also as a result of a logic error. The final type of error is an intent error. In this case everything is done correctly, but the purpose behind the instructions is at fault. The program is doing what we tell it to do, and not what we wanted it to do! In programming this is as a result of not defining or specifying the problem properly and thus finding a solution to the wrong problem. These errors are listed in the order in which they are of increasing difficulty to detect and correct:  syntax errors are usually found by the programming language, and only need editing out  run time errors occur when the program is operating; the program will need some minor changes before re-compiling  logic errors are more difficult to spot; they are indicated by incorrect or inconsistent results, or the program not working at all; the specification needs to be checked and the algorithm rewritten  intent errors may not be detected until too late (if at all); they usually require the program to be rewritten. As can be seen, the latter two types of error are the most serious, and it is important to avoid them. This is why the need for careful definition and specification was stressed in the software development cycle. Eliminating bugs is an important part of programming. So far we have looked at detection and correction but equally important is limiting the number of errors and making them easier to find. As a program is being written, detailed documentation will assist in error detection. External descriptions of the program (written or diagrams) show the supposed flow of logic, while internal documentation in the form of comments and meaningful variable names help you see what is going on. (We will look at comments in the next unit.) One way of detecting errors in a program is to trace values through the program. This can be done on paper or using the trace utility that comes with the program. To run a trace it is necessary to identify the key variables and then see what values each holds as the program runs. For example the following algorithm might give an error message in some situations: prompt for first read first © Kevin Savage 2011 – Licensed to Hillcrest Christian College for use in 2011-12
14. 14. Solving problems 77 prompt for second read second answer = first / second write answer If the values input for first and second are 8 and 16 the following occurs: Line first second answer 1 8 - - 2 8 16 - 3 8 16 0.5 4 8 16 0.5 But what if 10 and 0 are entered?: Line first second answer 1 10 - - 2 10 0 - 3 10 0 error! 4 10 0 error! The error results from an attempted division by zero. The earlier a defect is found in an algorithm, or a program developed from an algorithm, the easier (and cheaper) it is to fix it. The testing of software to discover and correct faults is a non-trivial task. It often involves not only examining the program, but also running it in a variety of environments and conditions to find if it does what it is supposed to do. The method above of following the values step by step through an algorithm or program is called a trace table. Line by line the values held in variables can be checked to see if they hold what we expect them to hold. Using a trace table is one method of debugging a program. There are a variety of other testing protocols. including static (inspection) and dynamic (“on the fly”), black box and white box, alpha and beta, and so on. Activity 3.3 – Oops! 1. Syntax and runtime errors are easily corrected as they are identified during the compiling or running of a program. Logic and intent errors however are more difficult to correct. What are logic errors and what are intent errors and how are they best avoided? 2. What type of error, syntax, runtime, logic or intent, is in each of the following algorithms? a prompt for first get first prompt for second get second if first < second then © Kevin Savage 2011 – Licensed to Hillcrest Christian College for use in 2011-12
15. 15. Leading Technology 78 write “The smaller value is ”, second endif b first_number + second_number = total c value = 8 sum = 16 total = sum - value * 2 answer = sum / total d name = “Fred Bloggs e prompt for one get one prompt for two get two one = two two = one write “Swapped around the numbers are ”, one, two f prompt for first get first total = first + second prompt for second get second write “The sum of the two numbers is ”, total g prompt for amount get amount answer = amont + 10 write “The answer is”, answer h prompt for value get value answer = value * 10 write “The answer is”, value 3. Work out what happens in each of the following programs by tracing the variables, and then write down what you think will be output. a prompt for cost get cost tax = cost * 15 /100 write tax b a = 12 b = 3 c = a * b a = c + b b = a – b c = c – 14 assume \$20 is entered for cost © Kevin Savage 2011 – Licensed to Hillcrest Christian College for use in 2011-12
16. 16. Solving problems 79 write “The final values are ”, a, b, c c first = 15 second = 5 if (second * 7) < (first * 2) then write first else write second endif 4. There are a wide variety of testing protocols used to check a program does what it is supposed to do. Research and then write a paragraph about one of the following:  alpha and beta testing  static and dynamic testing  white box and black box testing  usability testing  security testing  destructive testing. Challenge 1. Prepare an algorithm to output the coins used to give change when an amount of less than \$1 is paid with a \$1 coin (e.g. 35c spent; change is 50c+10c+5c). 2. Write an algorithm to score a game of 10 pin bowling for one player. A game has ten frames with a maximum of two bowls per frame. If a player obtains a strike (10 pins with the first bowl) s/he scores 10 points plus the value of the next two bowls. If s/he scores a spare (10 pins with two bowls) s/he scores 10 points plus the value of the next bowl. Any other number of pins in a frame counts as is. A strike in the 10th frame results in two extra bowls, a spare in the 10th gains one extra bowl. 3. In order to transfer information over networks or the internet it is sometimes useful to either compress or encrypt the data. Data compression is the process of using fewer bits to represent the same information, usually to reduce file size and speed up transmission. Encryption is the converting or encoding of information into a different form so that its meaning is hidden. Both processes are very important and there are a wide range of algorithms and programs for either. Investigate either data compression or encryption and explore the use of the algorithms used for processing. Experiment with developing your own algorithm for either data compression or for encryption. © Kevin Savage 2011 – Licensed to Hillcrest Christian College for use in 2011-12