890 likes | 1.35k Views
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.
E N D
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 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
Steps of Problem Solving 5. Verify that the program does state the solution correctly Testing
Understand the problem • What is the problem? • What are the inputs? • What are the outputs? • What are examples of input output relationships?
Understand the problem • What is the problem? • Compute the average of 3 numbers
Understand the problem • What are the inputs? • Input: 3 values
Understand the problem • What are the outputs • Output: The average of the input values
Understand the problem • Input output relationship 3.1 5.3 1.2 Input: 3.2 Output:
Understand the problem • Input output relationship 8.1 2.2 1.3 Input: 3.86 Output:
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;
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;
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);
Verify that the program does state the solution correctly • Enter the program • Compile • Run • Compare the output to expected output
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 inputs? • Input: 2 Integer Values
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
Understand the problem • Input output relationship 4 2 Input: Yes Output:
Understand the problem • Input output relationship 3 12 Input: Yes Output:
Understand the problem • Input output relationship 3 7 Input: No Output:
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”