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.
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.
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 2. Devise a solution to the problem Algorithm Design
Steps of Problem Solving 3. Verify that the solution is correct Desk check 4. Describe the solution using a programming language Programming
Devise a solution to the problem • How is the computer to calculate the average of 3 values? Step 1. Accept the input values Computer
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
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
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
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;
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
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
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
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
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
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
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
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
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
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);
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
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
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
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
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
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
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”
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
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
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
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
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
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
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
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
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
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);
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”