Presentation is loading. Please wait.

Presentation is loading. Please wait.

1. Understanding the Problem 2. Brainstorming 3. Drawing an I/O (Input/Output) diagram 4. 5-step Process (or: Small iPods Make Copying Tough) Developing.

Similar presentations


Presentation on theme: "1. Understanding the Problem 2. Brainstorming 3. Drawing an I/O (Input/Output) diagram 4. 5-step Process (or: Small iPods Make Copying Tough) Developing."— Presentation transcript:

1 1. Understanding the Problem 2. Brainstorming 3. Drawing an I/O (Input/Output) diagram 4. 5-step Process (or: Small iPods Make Copying Tough) Developing a Solution 1

2 1. Understanding the problem However the problem may seem, it is crucial to fully understand the problem: Brainstorm! For example: Solve the area of a triangle What way would you go about doing this: On Paper? Preparing it for the machine? 2

3 2. Brainstorm What do you know about the problem? How do you want to solve it? height base side 1 side 2 angle Area = ½ * base * height Area = ½ * side2 * side 1 * sin(angle) 3

4 3. Drawing an I/O diagram Remember the Von Neumann architecture? The I/O diagram determines which input and output device the software will use, as well as decide which inputs values and output values it will use. The I/O diagram is a first step to organizing the brainstorming. 4

5 General I/O diagram INPUT SIDE OUTPUT SIDE 5 ? ? ? ? ? ? ? ?

6 Possible I/O diagram for the area of a triangle (option1) AREA OF TRIANGLE Side 1 and 2 Angle SCREEN Arrows represent the GIVENS (=INPUTS) Arrows represent the RESULTS (=OUTPUTS) External interface – What HARDWARE is used for each area 6 KEYBOARD

7 Possible I/O diagram for the area of a triangle (option2) AREA OF TRIANGLE Side 1 and 2 Angle PRINTER Arrows represent the GIVENS (=INPUTS) Arrows represent the RESULTS (=OUTPUTS) External interface – What HARDWARE is used for each area 7 DATA BASE 7

8 Possible I/O diagram for the area of a triangle (option3) AREA OF TRIANGLE base height SCREEN Arrows represent the GIVENS (=INPUTS) Arrows represent the RESULTS (=OUTPUTS) External interface – What HARDWARE is used for each area 8 KEYBOARD 8

9 Some Other More Complex Examples (and yet, the I/O is not so complex) 9 ATM MACHINE money Money Slot pin Deposit/with- draw/transfer… Touch Screen/ pin pad Account number Card receipt Receipt Slot Money Slot money 9

10 SIMCT (or: Small iPods Make Copying Tough) S – State the problem I – Inputs and Outputs (identify them) M – (Be able to) Manually solve the problem C – Computerize the solution Layout the algorithm Provide the code that implements the algorithm T – test, Test, TEST!! 10 Note: I and M are usually mixed up. You obvioulsy need to brainstorm, solve the problem, find obstacles to fix, resolve it again to eventually zoom in on the correct inputs/outputs..

11 Example Problem We want to have a computer program find the roots of quadratic equations: ax 2 + bx + c = 0 11

12 Example Problem (cont.) State the problem: “I want a computer program that will find the roots of a quadratic equation” Any quadratic equation? What if a=0? What if the roots are imaginary? 12

13 Example Problem (cont.) Restate the problem: “I want a computer program that will find the REAL roots of a quadratic equation for all REAL coefficients a, b, and c such that a≠0.” This shows a much more thorough consideration of the problem 13

14 Example Problem (cont.) Step 2: Inputs and Outputs What are the inputs for this problem? In other words, what information is needed FROM OUTSIDE THE PROGRAM for the desired program to solve the problem? 14

15 Example Problem (cont.) Inputs, continued… The only information needed from outside the program are the values of the coefficients. 15

16 Example Problem (cont.) Outputs: What are we expecting as output from this program? The roots, of course. But what about error messages – when the user puts in bad information? What about an “imaginary roots found” message? 16

17 Example Problem (cont.) Outputs, continued: - The roots, if real - Message if roots are imaginary - Error if the user inputs bad values 17

18 Example Problem (cont.) (Be able to) Manually Solve the Problem Not always practical to actually solve the problem. But you must be able to solve the problem manually, assuming you had the time and resources necessary. Otherwise, how will you tell the computer how to do it? 18

19 Example Problem (cont.) Manually solve the problem, continued… For this problem, you might manually solve the quadratic equation for various inputs. Pretend you are the program you will be writing. What should YOU do if the user provides you with good data; bad data; data that gives real roots; data that gives imaginary roots. 19

20 Example Problem (cont.) Computerize the solution Algorithm: Describe the steps you took when you manually solved the problem. This is more difficult than it seems. Humans perform actions subconsciously – we’re not even aware we are doing them! 20

21 Example Problem (cont.) Algorithm, continued… Layout the algorithm in “skeleton” form: % Collect the inputs from the user % Apply the inputs to the quadratic formula % Display the results 21

22 Example Problem (cont.) Now, “flesh it out” % Collect the inputs from the user % Print error message if bad inputs % Apply the inputs to the quadratic formula % Compute the discriminant (b 2 -4ac) % If discriminant < 0 % display ‘Imaginary roots’ % Otherwise % compute real roots % Display the results 22

23 Example Problem (cont.) Computerize, step 2: Code For each step of the algorithm, write code (in this class, MATLAB code) that will perform the actions you have specified. Avoid proceeding with the next step until you feel certain the step has been accomplished. (Actual coding steps will be learned this semester) 23

24 Example Problem (cont.) test, Test, TEST!! It is provably impossible to write a computer program to test another arbitrary computer program for correctness. So we usually resort to manual testing. For most programs, it is not possible to test a program too thoroughly because it is not possible to test all inputs. Choose your inputs to maximize confidence that the solution you have written will work correctly. 24

25 Example Problem (cont.) For our quadratic solver, we would want to choose inputs such as: a<0, b<0, c<0 a<0, b<0, c=0 a 0 a<0, b=0, c<0 a<0, b=0, c=0 a 0 a 0, c<0 a 0, c=0 a 0, c>0 a=0, b<0, c<0 a=0, b<0, c=0 a=0, b 0 a=0, b=0, c<0 a=0, b=0, c=0 a=0, b=0, c>0 a=0, b>0, c<0 a=0, b>0, c=0 a=0, b>0, c>0 a>0, b<0, c<0 a>0, b<0, c=0 a>0, b 0 a>0, b=0, c<0 a>0, b=0, c=0 a>0, b=0, c>0 a>0, b>0, c<0 a>0, b>0, c=0 a>0, b>0, c>0 25

26 Example Problem (cont.) Of course, with human ingenuity, we can design the program to avoid some of these: % If there was no error condition (such as a=0) % then compute the roots % Otherwise, print the error message only With this sort of technique, we can avoid having to test nearly 1/3 of the possibilities! 26

27 Example Problem (cont.) % Collect the inputs from the user % If no error condition % Compute the discriminant (b 2 -4ac) % If discriminant < 0 % display ‘Imaginary roots’ % Otherwise % compute real roots % Display the results % Otherwise % Print error message 27

28 % Collect the inputs from the user a = input(‘Enter coefficient a: ’); b = input(‘Enter coefficient b: ’); c = input(‘Enter coefficient c: ’); % If no error condition if a~=0 %means 1 not equal to zero, could have done (a 0) % Compute the discriminant (b 2 -4ac) discriminant = b^2-4*a*c; % If discriminant < 0 if discriminant<0 % display ‘Imaginary roots’ disp(‘Imaginary Roots’) % Otherwise else % compute real roots x1 = (-b + sqrt(discriminant)) / (2*a); x2 = (-b - sqrt(discriminant)) / (2*a); % Display the results fprintf(‘x1 = %.2f and x2 = %.2f\n’,x1,x2) end % Otherwise else % Print error message disp(‘error: a invalid’) end 28


Download ppt "1. Understanding the Problem 2. Brainstorming 3. Drawing an I/O (Input/Output) diagram 4. 5-step Process (or: Small iPods Make Copying Tough) Developing."

Similar presentations


Ads by Google