Introduction to Problem SolvingS1.2.1 Bina © 1998 Liran & Ofir Introduction to Problem Solving Programming in C
Introduction to Problem SolvingS1.2.2 Bina © 1998 Liran & Ofir Problem Solving l Describe the steps that the computer should take in order to solve the problem should take in order to solve the problem n How to get the computer solve a problem? Algorithm
Introduction to Problem SolvingS1.2.3 Bina © 1998 Liran & Ofir Steps of Problem Solving 1. Understand the problem to be solved Analysis 2. Devise a solution to the problem Algorithm Design
Introduction to Problem SolvingS1.2.4 Bina © 1998 Liran & Ofir Steps of Problem Solving 3. Verify that the solution is correct Desk check 4. Describe the solution using a programming language language Programming
Introduction to Problem SolvingS1.2.5 Bina © 1998 Liran & Ofir Steps of Problem Solving 5. Verify that the program does state the solution correctly solution correctly Testing
Introduction to Problem SolvingS1.2.6 Bina © 1998 Liran & Ofir Understand the problem n What is the problem? n What are the inputs? n What are the outputs? n What are examples of input output relationships?
Introduction to Problem SolvingS1.2.7 Bina © 1998 Liran & Ofir Understand the problem n What is the problem? l Compute the average of 3 numbers
Introduction to Problem SolvingS1.2.8 Bina © 1998 Liran & Ofir Understand the problem n What are the inputs? l Input: 3 values
Introduction to Problem SolvingS1.2.9 Bina © 1998 Liran & Ofir Understand the problem n What are the outputs l Output: The average of the input values
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Understand the problem n Input output relationship Input: Output: 3.2
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Understand the problem n Input output relationship Input: Output: 3.86
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Devise a solution to the problem n How is the computer to calculate the average of 3 values? Step 1. Accept the input values Computer
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Devise a solution to the problem n How is the computer to calculate the average of 3 values? Step 1. Accept the input values Step 2. Compute the average Computer Compute the Average
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Devise a solution to the problem n How is the computer to calculate the average of 3 values? Compute the Average Step 3. Display the average Step 1. Accept the input values Step 2. Compute the average
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Devise a solution to the problem n Refine Step 2 Step 3. Display the average 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
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Devise a solution to the problem n Refine Step 2.1 Step 2.1. Add up the three values l Let “A” denote the first value l Let “B” denote the second value l Let “C” denote the third value l Let “Sum” denote the sum of “A”, “B”, and “C” Sum = A + B + C;
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Devise a solution to the problem n Refine Step 2.2 Step 2.1. Divide the result by 3 l Let “Average” denote the average of “A”, “B” and “C” Average = Sum / 3.0;
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Verify that the solution is correct A Step 1. Accept the input values, A, B, C 3.9 B 4.1 C 1.3 Step 3. Display the average Step 2. Compute the average Step 2.1. Sum = A + B + C; Step 2.2. Average = Sum /3.0;
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Verify that the solution is correct Step 3. Display the average 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; C B A
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Sum Verify that the solution is correct Step 3. Display the average 9.3 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; C B A
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Verify that the solution is correct Step 3. Display the average 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; Average Sum C B A
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Verify that the solution is correct Step 3. Display the average 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; Average Sum C B A Average is 3.1
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Describe the solution using a programming language Step 3. Display the average 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; float A,B,C; scanf(“%f”,&A); scanf(“%f”,&B); scanf(“%f”,&C);
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Describe the solution using a programming language Step 3. Display the average 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; float A,B,C; scanf(“%f”,&A); scanf(“%f”,&B); scanf(“%f”,&C);
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Describe the solution using a programming language Step 3. Display the average 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; Sum = A + B + C; float A,B,C; float Sum; scanf(“%f”,&A); scanf(“%f”,&B); scanf(“%f”,&C);
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Describe the solution using a programming language Step 3. Display the average 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; Average = Sum / 3.0; Sum = A + B + C; float A,B,C; float Sum; float Average; scanf(“%f”,&A); scanf(“%f”,&B); scanf(“%f”,&C);
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Describe the solution using a programming language Step 3. Display the average 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; printf(“%f”,Average); Average = Sum / 3.0; Sum = A + B + C; float A,B,C; float Sum; float Average; scanf(“%f”,&A); scanf(“%f”,&B); scanf(“%f”,&C);
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Verify that the program does state the solution correctly n Enter the program n Compile n Run n Compare the output to expected output
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir SelectionSelection n 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
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Understand the problem l Accept two integer values and decide whether or not the larger number is divisible by the or not the larger number is divisible by the smaller number smaller number n What is the problem?
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Understand the problem n What are the inputs? l Input: 2 Integer Values
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Understand the problem l Output: Yes n What are the outputs If the larger number is divisible by the second number l Output: No If the larger number is not divisible by the second number
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Understand the problem n Input output relationship Input: 4 2 Output: Yes
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Understand the problem n Input output relationship Input: 3 12 Output: Yes
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Understand the problem n Input output relationship Input: 3 7 Output: No
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Devise a solution to the problem n How is the computer to decide whether or not the larger number is divisible by the or not the larger number is divisible by the smaller number? smaller number? Step 1. Accept the input values Computer
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Devise a solution to the problem n How is the computer to decide whether or not the larger number is divisible by the or not the larger number is divisible by the smaller number? smaller number? Compute the result Step 1. Accept the input values Step 2. Compute and display the result the result
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Devise a solution to the problem n Refine Step 2. Step 2. Compute and display the result l Let “A” denote the first value l Let “B” denote the second value l Let “Larger” denote the larger value l Let “Smaller” denote the smaller value
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Devise a solution to the problem n Refine Step 2. Step 2.1. If Larger is divisible by Smaller Then print “Yes” Then print “Yes” Otherwise, Larger is not divisible Otherwise, Larger is not divisible by Smaller and print “No” by Smaller and print “No” Step 2. Compute and display the result
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir A Verify that the solution is correct 4 B 2 Step 1. Accept the input values, A, B Step 2. Compute and display the result Smaller Larger Step 2.1. If Larger is divisible by Smaller Then print “Yes” Smaller Then print “Yes” Otherwise, Larger is not Otherwise, Larger is not divisible by Smaller and divisible by Smaller and print “No” print “No”
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Verify that the solution is correct Step 1. Accept the input values, A, B Smaller Larger B A Step 2. Compute and display the result Step 2.1. If Larger is divisible by Smaller Then print “Yes” Smaller Then print “Yes” Otherwise, Larger is not Otherwise, Larger is not divisible by Smaller and divisible by Smaller and print “No” print “No”
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Verify that the solution is correct Step 1. Accept the input values, A, B Smaller Larger B A Step 2. Compute and display the result Output: Yes Step 2.1. If Larger is divisible by Smaller Then print “Yes” Smaller Then print “Yes” Otherwise, Larger is not Otherwise, Larger is not divisible by Smaller and divisible by Smaller and print “No” print “No”
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir A Verify that the solution is correct 3 B 12 Step 1. Accept the input values, A, B Step 2. Compute and display the result Smaller Larger Step 2.1. If Larger is divisible by Smaller Then print “Yes” Smaller Then print “Yes” Otherwise, Larger is not Otherwise, Larger is not divisible by Smaller and divisible by Smaller and print “No” print “No”
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Verify that the solution is correct Step 1. Accept the input values, A, B Smaller Larger B A Step 2. Compute and display the result Step 2.1. If Larger is divisible by Smaller Then print “Yes” Smaller Then print “Yes” Otherwise, Larger is not Otherwise, Larger is not divisible by Smaller and divisible by Smaller and print “No” print “No”
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Verify that the solution is correct Step 1. Accept the input values, A, B Smaller Larger B A Step 2. Compute and display the result Output: Yes Step 2.1. If Larger is divisible by Smaller Then print “Yes” Smaller Then print “Yes” Otherwise, Larger is not Otherwise, Larger is not divisible by Smaller and divisible by Smaller and print “No” print “No”
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir A Verify that the solution is correct 3 B 7 Step 1. Accept the input values, A, B Step 2. Compute and display the result Smaller Larger Step 2.1. If Larger is divisible by Smaller Then print “Yes” Smaller Then print “Yes” Otherwise, Larger is not Otherwise, Larger is not divisible by Smaller and divisible by Smaller and print “No” print “No”
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Verify that the solution is correct Step 1. Accept the input values, A, B Smaller Larger B A Step 2. Compute and display the result Step 2.1. If Larger is divisible by Smaller Then print “Yes” Smaller Then print “Yes” Otherwise, Larger is not Otherwise, Larger is not divisible by Smaller and divisible by Smaller and print “No” print “No”
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Verify that the solution is correct Step 1. Accept the input values, A, B Smaller Larger B A Step 2. Compute and display the result Output: No Step 2.1. If Larger is divisible by Smaller Then print “Yes” Smaller Then print “Yes” Otherwise, Larger is not Otherwise, Larger is not divisible by Smaller and divisible by Smaller and print “No” print “No”
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir 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” Smaller Then print “Yes” Otherwise, Larger is not Otherwise, Larger is not divisible by Smaller and divisible by Smaller and print “No” print “No” int a,b; scanf(“%d”,&a); scanf(“%d”,&b);
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir 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” Smaller Then print “Yes” Otherwise, Larger is not Otherwise, Larger is not divisible by Smaller and divisible by Smaller and print “No” print “No” if (a > b) { largest = a; largest = a; smallest = b; smallest = b; } else { largest = b; largest = b; smallest = a; smallest = a;} int a,b; int largest,smallest scanf(“%d”,&a); scanf(“%d”,&b);
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir 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” Smaller Then print “Yes” Otherwise, Larger is not Otherwise, Larger is not divisible by Smaller and divisible by Smaller and print “No” print “No” If (largest % smallest == 0) printf(“Yes”); printf(“Yes”); else printf(“No”); printf(“No”); if (a > b) { largest = a; largest = a; smallest = b; smallest = b; } else { largest = b; largest = b; smallest = a; smallest = a;} int a,b; int largest,smallest scanf(“%d”,&a); scanf(“%d”,&b);
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir IterationIteration n Facilitates the repeated execution of a segment of the program. l While you are hungry l Buy a sandwich l Eat the sandwich l While you haven’t read the last slide l Read the next slide
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Understand the problem n What is the problem? l Accept an Integer, N as input and print the value of … + N value of … + N
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Understand the problem n What are the inputs? l Input: one integer value, N
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Understand the problem n What are the outputs l Output: … + N
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Understand the problem n Input output relationship Input: 3 Output: 6
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Understand the problem n Input output relationship Input: 4 Output: 10
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Devise a solution to the problem n How is the computer to print the value of … + N Step 1. Accept the input value Step 2. Compute the sum Step 3. Display the sum
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Devise a solution to the problem n Refine Step 2 Step 2. Compute the sum Step 2.1. Sum = 0; Step 2.1. Count = 1; Step 2.3. Repeat steps N times Step Sum = Sum + Count; Step Count = Count +1;
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Verify that the solution is correct Step 3. Display the sum Step 1. Accept the input value Step 2. Compute the sum N 3 Step 2.1. Sum = 0; Step 2.1. Count = 1; Step 2.3. Repeat steps N times Step Sum = Sum + Count; Step Count = Count +1;
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Verify that the solution is correct N 3 Step 3. Display the sum Step 1. Accept the input value Step 2. Compute the sum Step 2.1. Sum = 0; Step 2.3. Repeat steps N times Step Sum = Sum + Count; Step 2.1. Count = 1; Step Count = Count +1;
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Verify that the solution is correct N 3 Sum 0 Step 3. Display the sum Step 1. Accept the input value Step 2. Compute the sum Step 2.1. Sum = 0; Step 2.3. Repeat steps N times Step Sum = Sum + Count; Step 2.1. Count = 1; Step Count = Count +1;
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Verify that the solution is correct N 3 Sum 0 Step 3. Display the sum Step 1. Accept the input value Step 2. Compute the sum Step 2.1. Sum = 0; Step 2.3. Repeat steps N times Step Sum = Sum + Count; Step 2.1. Count = 1; Step Count = Count +1; Count 1
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Verify that the solution is correct N 3 Sum 0 Step 3. Display the sum Step 1. Accept the input value Step 2. Compute the sum Step 2.1. Sum = 0; Step 2.3. Repeat steps N times Step Sum = Sum + Count; Step 2.1. Count = 1; Step Count = Count +1; Count 1
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Sum 0 1 Verify that the solution is correct N 3 Step 3. Display the sum Step 1. Accept the input value Step 2. Compute the sum Step 2.1. Sum = 0; Step 2.3. Repeat steps N times Step Sum = Sum + Count; Step 2.1. Count = 1; Step Count = Count +1; Count 1
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir 1 Sum 1 2 Verify that the solution is correct N 3 Step 3. Display the sum Step 1. Accept the input value Step 2. Compute the sum Step 2.1. Sum = 0; Step 2.3. Repeat steps N times Step Sum = Sum + Count; Step 2.1. Count = 1; Step Count = Count +1; Count
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Verify that the solution is correct N 3 Sum 1 Step 3. Display the sum Step 1. Accept the input value Step 2. Compute the sum Step 2.1. Sum = 0; Step 2.3. Repeat steps N times Step Sum = Sum + Count; Step 2.1. Count = 1; Step Count = Count +1; Count 2
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Sum 1 3 Verify that the solution is correct N 3 Step 3. Display the sum Step 1. Accept the input value Step 2. Compute the sum Step 2.1. Sum = 0; Step 2.3. Repeat steps N times Step Sum = Sum + Count; Step 2.1. Count = 1; Step Count = Count +1; Count 2
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir 2 Sum 3 3 Verify that the solution is correct N 3 Step 3. Display the sum Step 1. Accept the input value Step 2. Compute the sum Step 2.1. Sum = 0; Step 2.3. Repeat steps N times Step Sum = Sum + Count; Step 2.1. Count = 1; Step Count = Count +1; Count
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Verify that the solution is correct N 3 Sum 3 Step 3. Display the sum Step 1. Accept the input value Step 2. Compute the sum Step 2.1. Sum = 0; Step 2.3. Repeat steps N times Step Sum = Sum + Count; Step 2.1. Count = 1; Step Count = Count +1; Count 3
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Sum 3 6 Verify that the solution is correct N 3 Step 3. Display the sum Step 1. Accept the input value Step 2. Compute the sum Step 2.1. Sum = 0; Step 2.3. Repeat steps N times Step Sum = Sum + Count; Step 2.1. Count = 1; Step Count = Count +1; Count 3
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir 3 Sum 6 4 Verify that the solution is correct N 3 Step 3. Display the sum Step 1. Accept the input value Step 2. Compute the sum Step 2.1. Sum = 0; Step 2.3. Repeat steps N times Step Sum = Sum + Count; Step 2.1. Count = 1; Step Count = Count +1; Count
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Verify that the solution is correct N 3 Sum 6 Step 3. Display the sum Step 1. Accept the input value Step 2. Compute the sum Step 2.1. Sum = 0; Step 2.3. Repeat steps N times Step Sum = Sum + Count; Step 2.1. Count = 1; Step Count = Count +1; Count 4
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Sum 0 6 Verify that the solution is correct N 3 Step 3. Display the sum Step 1. Accept the input value Step 2. Compute the sum Step 2.1. Sum = 0; Step 2.3. Repeat steps N times Step Sum = Sum + Count; Step 2.1. Count = 1; Step Count = Count +1; Count 4 6
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Step Sum = Sum + Count; Describe the solution using a programming language Step 3. Display the sum Step 1. Accept the input value Step 2. Compute the sum Step 2.1. Sum = 0; Step 2.3. Repeat steps N times Step 2.1. Count = 1; Step Count = Count +1; int n; scanf(“%d”,&n);
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Describe the solution using a programming language int sum; int n; sum = 0; Step Sum = Sum + Count; Step 3. Display the sum Step 1. Accept the input value Step 2. Compute the sum Step 2.1. Sum = 0; Step 2.3. Repeat steps N times Step 2.1. Count = 1; Step Count = Count +1; scanf(“%d”,&n);
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Describe the solution using a programming language count = 1; Step Sum = Sum + Count; Step 3. Display the sum Step 1. Accept the input value Step 2. Compute the sum Step 2.1. Sum = 0; Step 2.3. Repeat steps N times Step 2.1. Count = 1; Step Count = Count +1; int count; int n; int sum; sum = 0; scanf(“%d”,&n);
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Describe the solution using a programming language While (count <= n){ } Step Sum = Sum + Count; Step 3. Display the sum Step 1. Accept the input value Step 2. Compute the sum Step 2.1. Sum = 0; Step 2.3. Repeat steps N times Step 2.1. Count = 1; Step Count = Count +1; count = 1; int sum; int count; int n; sum = 0; scanf(“%d”,&n);
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Describe the solution using a programming language sum = sum+count; sum = sum+count; Step Sum = Sum + Count; Step 3. Display the sum Step 1. Accept the input value Step 2. Compute the sum Step 2.1. Sum = 0; Step 2.3. Repeat steps N times Step 2.1. Count = 1; Step Count = Count +1; While (count <= n){ } count = 1; int sum; int count; int n; sum = 0; scanf(“%d”,&n);
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Describe the solution using a programming language Step Sum = Sum + Count; Step 3. Display the sum Step 1. Accept the input value Step 2. Compute the sum Step 2.1. Sum = 0; Step 2.3. Repeat steps N times Step 2.1. Count = 1; Step Count = Count +1; sum = sum+count; sum = sum+count; } While (count <= n){ count = 1; int sum; int count; int n; sum = 0; scanf(“%d”,&n); count = count +1;
Introduction to Problem SolvingS Bina © 1998 Liran & Ofir Describe the solution using a programming language printf(“%d”,sum); Step Sum = Sum + Count; Step 3. Display the sum Step 1. Accept the input value Step 2. Compute the sum Step 2.1. Sum = 0; Step 2.3. Repeat steps N times Step 2.1. Count = 1; Step Count = Count +1; sum = sum+count; sum = sum+count; count = count +1; count = count +1; While (count <= n){ } count = 1; int sum; int count; int n; sum = 0; scanf(“%d”,&n);