1 / 81

Introduction to Problem Solving

Introduction to Problem Solving. Programming in C. Problem Solving. How to get the computer solve a problem?. Describe the steps that the computer should take in order to solve the problem. Algorithm. Steps of Problem Solving. 1 . Understand the problem to be solved. Analysis.

vanya
Download Presentation

Introduction to Problem Solving

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Problem Solving Programming in C

  2. Problem Solving • How to get the computer solve a problem? • Describe the steps that the computer • should take in order to solve the problem Algorithm

  3. Steps of Problem Solving 1. Understand the problem to be solved Analysis 2. Devise a solution to the problem Algorithm Design

  4. Steps of Problem Solving 3. Verify that the solution is correct Desk check 4. Describe the solution using a programming language Programming

  5. Steps of Problem Solving 5. Verify that the program does state the solution correctly Testing

  6. Understand the problem • What is the problem? • What are the inputs? • What are the outputs? • What are examples of input output relationships?

  7. Understand the problem • What is the problem? • Compute the average of 3 numbers

  8. Understand the problem • What are the inputs? • Input: 3 values

  9. Understand the problem • What are the outputs • Output: The average of the input values

  10. Understand the problem • Input output relationship 3.1 5.3 1.2 Input: 3.2 Output:

  11. Understand the problem • Input output relationship 8.1 2.2 1.3 Input: 3.86 Output:

  12. Devise a solution to the problem • How is the computer to calculate the average of 3 values? Step 1. Accept the input values Computer

  13. Computer Compute the Average Devise a solution to the problem • How is the computer to calculate the average of 3 values? Step 1. Accept the input values Step 2. Compute the average

  14. Devise a solution to the problem • How is the computer to calculate the average of 3 values? Step 1. Accept the input values Step 2. Compute the average Step 3. Display the average Compute the Average

  15. Devise a solution to the problem • Refine Step 2 Step 1. Accept the input values Step 2. Compute the average Step 2.1.Add up the three values Step 2.2.Divide the result by 3 Step 3. Display the average

  16. Devise a solution to the problem • Refine Step 2.1 Step 2.1.Add up the three values • Let “A” denote the first value • Let “B” denote the second value • Let “C” denote the third value • Let “Sum” denote the sum of “A”, “B”, and “C” Sum = A + B + C;

  17. Let “Average” denote the average of “A”, “B” and “C” Devise a solution to the problem • Refine Step 2.2 Step 2.1.Divide the result by 3 Average = Sum / 3.0;

  18. A B C Verify that the solution is correct Step 1. Accept the input values, A, B, C 3.9 Step 2. Compute the average 4.1 Step 2.1.Sum = A + B + C; 1.3 Step 2.2.Average = Sum /3.0; Step 3. Display the average

  19. A B C Verify that the solution is correct Step 1. Accept the input values, A, B, C 3.9 Step 2. Compute the average 4.1 Step 2.1.Sum = A + B + C; 1.3 Step 2.2.Average = Sum /3.0; Step 3. Display the average

  20. A B C Sum Verify that the solution is correct Step 1. Accept the input values, A, B, C 3.9 Step 2. Compute the average 4.1 Step 2.1.Sum = A + B + C; 1.3 Step 2.2.Average = Sum /3.0; 9.3 Step 3. Display the average

  21. A B C Sum Average Verify that the solution is correct Step 1. Accept the input values, A, B, C 3.9 Step 2. Compute the average 4.1 Step 2.1.Sum = A + B + C; 1.3 Step 2.2.Average = Sum /3.0; 9.3 Step 3. Display the average 3.1

  22. A B C Sum Average Verify that the solution is correct Step 1. Accept the input values, A, B, C 3.9 Step 2. Compute the average 4.1 Step 2.1.Sum = A + B + C; 1.3 Step 2.2.Average = Sum /3.0; 9.3 Step 3. Display the average 3.1 Average is 3.1

  23. Describe the solution using a programming language Step 1. Accept the input values, A, B, C float A,B,C; Step 2. Compute the average scanf(“%f”,&A); Step 2.1.Sum = A + B + C; scanf(“%f”,&B); Step 2.2.Average = Sum /3.0; scanf(“%f”,&C); Step 3. Display the average

  24. float A,B,C; scanf(“%f”,&A); scanf(“%f”,&B); scanf(“%f”,&C); Describe the solution using a programming language Step 1. Accept the input values, A, B, C Step 2. Compute the average Step 2.1.Sum = A + B + C; Step 2.2.Average = Sum /3.0; Step 3. Display the average

  25. float A,B,C; float Sum; scanf(“%f”,&A); scanf(“%f”,&B); scanf(“%f”,&C); Sum = A + B + C; Describe the solution using a programming language Step 1. Accept the input values, A, B, C Step 2. Compute the average Step 2.1.Sum = A + B + C; Step 2.2.Average = Sum /3.0; Step 3. Display the average

  26. float A,B,C; float Sum; float Average; scanf(“%f”,&A); scanf(“%f”,&B); scanf(“%f”,&C); Sum = A + B + C; Average = Sum / 3.0; Describe the solution using a programming language Step 1. Accept the input values, A, B, C Step 2. Compute the average Step 2.1.Sum = A + B + C; Step 2.2.Average = Sum /3.0; Step 3. Display the average

  27. float A,B,C; float Sum; float Average; scanf(“%f”,&A); scanf(“%f”,&B); scanf(“%f”,&C); Sum = A + B + C; Average = Sum / 3.0; Describe the solution using a programming language Step 1. Accept the input values, A, B, C Step 2. Compute the average Step 2.1.Sum = A + B + C; Step 2.2.Average = Sum /3.0; Step 3. Display the average printf(“%f”,Average);

  28. Verify that the program does state the solution correctly • Enter the program • Compile • Run • Compare the output to expected output

  29. Selection • Facilitates the selection of one of a number of various alternatives based on a condition. If the door is open Then Enter the room Else Knock at the door

  30. Understand the problem • What is the problem? • Accept two integer values and decide whether • or not the larger number is divisible by the • smaller number

  31. Understand the problem • What are the inputs? • Input: 2 Integer Values

  32. Understand the problem • What are the outputs • Output: Yes If the larger number is divisible by the second number • Output: No If the larger number is not divisible by the second number

  33. Understand the problem • Input output relationship 4 2 Input: Yes Output:

  34. Understand the problem • Input output relationship 3 12 Input: Yes Output:

  35. Understand the problem • Input output relationship 3 7 Input: No Output:

  36. Devise a solution to the problem • How is the computer to decide whether or not the larger number is divisible by the smaller number? Step 1. Accept the input values Computer

  37. Devise a solution to the problem • How is the computer to decide whether or not the larger number is divisible by the smaller number? Step 1. Accept the input values Step 2. Compute and display the result Compute the result

  38. Devise a solution to the problem • Refine Step 2. Step 2. Compute and display the result • Let “A” denote the first value • Let “B” denote the second value • Let “Larger” denote the larger value • Let “Smaller” denote the smaller value

  39. Devise a solution to the problem • Refine Step 2. Step 2. Compute and display the result Step 2.1.If Larger is divisible by Smaller Then print “Yes” Otherwise, Larger is not divisible by Smaller and print “No”

  40. A B Verify that the solution is correct Step 1. Accept the input values, A, B 4 Step 2. Compute and display the result 2 Step 2.1.If Larger is divisible by Smaller Then print “Yes” Otherwise, Larger is not divisible by Smaller and print “No” Larger Smaller

  41. Verify that the solution is correct Step 1. Accept the input values, A, B 4 A Step 2. Compute and display the result 2 B Step 2.1.If Larger is divisible by Smaller Then print “Yes” Otherwise, Larger is not divisible by Smaller and print “No” 4 Larger 2 Smaller

  42. Verify that the solution is correct Step 1. Accept the input values, A, B 4 A Step 2. Compute and display the result 2 B Step 2.1.If Larger is divisible by Smaller Then print “Yes” Otherwise, Larger is not divisible by Smaller and print “No” 4 Larger 2 Smaller Output: Yes

  43. A B Verify that the solution is correct Step 1. Accept the input values, A, B 3 Step 2. Compute and display the result 12 Step 2.1.If Larger is divisible by Smaller Then print “Yes” Otherwise, Larger is not divisible by Smaller and print “No” Larger Smaller

  44. Verify that the solution is correct Step 1. Accept the input values, A, B 3 A Step 2. Compute and display the result 12 B Step 2.1.If Larger is divisible by Smaller Then print “Yes” Otherwise, Larger is not divisible by Smaller and print “No” 12 Larger 3 Smaller

  45. Verify that the solution is correct Step 1. Accept the input values, A, B 3 A Step 2. Compute and display the result 12 B Step 2.1.If Larger is divisible by Smaller Then print “Yes” Otherwise, Larger is not divisible by Smaller and print “No” 12 Larger 3 Smaller Output: Yes

  46. A B Verify that the solution is correct Step 1. Accept the input values, A, B 3 Step 2. Compute and display the result 7 Step 2.1.If Larger is divisible by Smaller Then print “Yes” Otherwise, Larger is not divisible by Smaller and print “No” Larger Smaller

  47. Verify that the solution is correct Step 1. Accept the input values, A, B 3 A Step 2. Compute and display the result 7 B Step 2.1.If Larger is divisible by Smaller Then print “Yes” Otherwise, Larger is not divisible by Smaller and print “No” 7 Larger 3 Smaller

  48. Verify that the solution is correct Step 1. Accept the input values, A, B 3 A Step 2. Compute and display the result 7 B Step 2.1.If Larger is divisible by Smaller Then print “Yes” Otherwise, Larger is not divisible by Smaller and print “No” 7 Larger 3 Smaller Output: No

  49. Describe the solution using a programming language int a,b; Step 1. Accept the input values, A, B Step 2. Compute and display the result scanf(“%d”,&a); Step 2.1.If Larger is divisible by Smaller Then print “Yes” Otherwise, Larger is not divisible by Smaller and print “No” scanf(“%d”,&b);

  50. int a,b; int largest,smallest scanf(“%d”,&a); scanf(“%d”,&b); if (a > b) { largest = a; smallest = b; } else { largest = b; smallest = a; } Describe the solution using a programming language Step 1. Accept the input values, A, B Step 2. Compute and display the result Step 2.1.If Larger is divisible by Smaller Then print “Yes” Otherwise, Larger is not divisible by Smaller and print “No”

More Related