Post on 25-Dec-2015
Introduction to Problem Solving S1.2.1Bina © 1998 Liran & Ofir
Introduction to Problem SolvingIntroduction to Problem SolvingIntroduction to Problem SolvingIntroduction to Problem Solving
Programming in Programming in
CC
Introduction to Problem Solving S1.2.2Bina © 1998 Liran & Ofir
Problem SolvingProblem SolvingProblem SolvingProblem Solving
Describe the steps that the computerDescribe the steps that the computer should take in order to solve the problemshould take in order to solve the problem
How to get the computer solve a problem?How to get the computer solve a problem?
AlgorithmAlgorithm
Introduction to Problem Solving S1.2.3Bina © 1998 Liran & Ofir
Steps of Problem SolvingSteps of Problem SolvingSteps of Problem SolvingSteps of Problem Solving
11. Understand the problem to be solved. Understand the problem to be solved
AnalysisAnalysis
22. Devise a solution to the problem . Devise a solution to the problem
Algorithm DesignAlgorithm Design
Introduction to Problem Solving S1.2.4Bina © 1998 Liran & Ofir
Steps of Problem SolvingSteps of Problem SolvingSteps of Problem SolvingSteps of Problem Solving
33. Verify that the solution is correct. Verify that the solution is correct
Desk checkDesk check
44. Describe the solution using a programming. Describe the solution using a programming languagelanguage
ProgrammingProgramming
Introduction to Problem Solving S1.2.5Bina © 1998 Liran & Ofir
Steps of Problem SolvingSteps of Problem SolvingSteps of Problem SolvingSteps of Problem Solving
55. Verify that the program does state the. Verify that the program does state the solution correctlysolution correctly
TestingTesting
Introduction to Problem Solving S1.2.6Bina © 1998 Liran & Ofir
Understand the problemUnderstand the problemUnderstand the problemUnderstand the problem
What is the problem?What is the problem?
What are the inputs?What are the inputs?
What are the outputs?What are the outputs?
What are examples of input output What are examples of input output
relationships?relationships?
Introduction to Problem Solving S1.2.7Bina © 1998 Liran & Ofir
Understand the problemUnderstand the problemUnderstand the problemUnderstand the problem
What is the problem?What is the problem?
Compute the average of 3 numbersCompute the average of 3 numbers
Introduction to Problem Solving S1.2.8Bina © 1998 Liran & Ofir
Understand the problemUnderstand the problemUnderstand the problemUnderstand the problem
What are the inputs?What are the inputs?
Input: 3 valuesInput: 3 values
Introduction to Problem Solving S1.2.9Bina © 1998 Liran & Ofir
Understand the problemUnderstand the problemUnderstand the problemUnderstand the problem
What are the outputsWhat are the outputs
Output: The average of the input valuesOutput: The average of the input values
Introduction to Problem Solving S1.2.10Bina © 1998 Liran & Ofir
Understand the problemUnderstand the problemUnderstand the problemUnderstand the problem
Input output relationshipInput output relationship
Input:Input: 3.1 5.3 1.23.1 5.3 1.2
Output:Output: 3.23.2
Introduction to Problem Solving S1.2.11Bina © 1998 Liran & Ofir
Understand the problemUnderstand the problemUnderstand the problemUnderstand the problem
Input output relationshipInput output relationship
Input:Input: 8.1 2.2 1.38.1 2.2 1.3
Output:Output: 3.863.86
Introduction to Problem Solving S1.2.12Bina © 1998 Liran & Ofir
Devise a solution to the problemDevise a solution to the problemDevise a solution to the problemDevise a solution to the problem
How is the computer to calculate the How is the computer to calculate the average of 3 values?average of 3 values?
Step 1.Step 1. Accept the input values Accept the input values
Computer
Introduction to Problem Solving S1.2.13Bina © 1998 Liran & Ofir
Devise a solution to the problemDevise a solution to the problemDevise a solution to the problemDevise a solution to the problem
How is the computer to calculate the How is the computer to calculate the average of 3 values?average of 3 values?
Step 1.Step 1. Accept the input values Accept the input values
Step 2.Step 2. Compute the average Compute the average
ComputerCompute
theAverage
Introduction to Problem Solving S1.2.14Bina © 1998 Liran & Ofir
Devise a solution to the problemDevise a solution to the problemDevise a solution to the problemDevise a solution to the problem
How is the computer to calculate the How is the computer to calculate the average of 3 values?average of 3 values?
Computethe
Average
Step 3. Step 3. Display the averageDisplay the average
Step 1.Step 1. Accept the input values Accept the input values
Step 2.Step 2. Compute the average Compute the average
Introduction to Problem Solving S1.2.15Bina © 1998 Liran & Ofir
Devise a solution to the problemDevise a solution to the problemDevise a solution to the problemDevise a solution to the problem
Refine Step 2Refine Step 2
Step 3. Step 3. Display the averageDisplay the average
Step 1.Step 1. Accept the input values Accept the input values
Step 2.Step 2. Compute the average Compute the average
Step 2.1.Step 2.1. Add up the three valuesAdd up the three values
Step 2.2.Step 2.2. Divide the result by 3Divide the result by 3
Introduction to Problem Solving S1.2.16Bina © 1998 Liran & Ofir
Devise a solution to the problemDevise a solution to the problemDevise a solution to the problemDevise a solution to the problem
Refine Step 2.1 Refine Step 2.1 Step 2.1.Step 2.1. Add up the three valuesAdd up the three values
Let “A” denote the first valueLet “A” denote the first value Let “B” denote the second valueLet “B” denote the second value Let “C” denote the third valueLet “C” denote the third value Let “Sum” denote the sum of “A”, “B”, and “C”Let “Sum” denote the sum of “A”, “B”, and “C”
Sum = A + B + C;Sum = A + B + C;
Introduction to Problem Solving S1.2.17Bina © 1998 Liran & Ofir
Devise a solution to the problemDevise a solution to the problemDevise a solution to the problemDevise a solution to the problem
Refine Step 2.2 Refine Step 2.2
Step 2.1.Step 2.1. Divide the result by 3Divide the result by 3
Let “Average” denote the average of “A”,Let “Average” denote the average of “A”,““B” and “C”B” and “C”
Average = Sum / 3.0;Average = Sum / 3.0;
Introduction to Problem Solving S1.2.18Bina © 1998 Liran & Ofir
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
A
Step 1.Step 1. Accept the input values, A, B, C Accept the input values, A, B, C
3.9
B 4.1
C 1.3
Step 3. Step 3. Display the averageDisplay the average
Step 2.Step 2. Compute the average Compute the average
Step 2.1.Step 2.1. Sum = A + B + C;Sum = A + B + C;
Step 2.2.Step 2.2. Average = Sum /3.0;Average = Sum /3.0;
Introduction to Problem Solving S1.2.19Bina © 1998 Liran & Ofir
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
Step 3. Step 3. Display the averageDisplay the average
Step 1.Step 1. Accept the input values, A, B, C Accept the input values, A, B, C
Step 2.Step 2. Compute the average Compute the average
Step 2.1.Step 2.1. Sum = A + B + C;Sum = A + B + C;
Step 2.2.Step 2.2. Average = Sum /3.0;Average = Sum /3.0;C
B
A 3.9
4.1
1.3
Introduction to Problem Solving S1.2.20Bina © 1998 Liran & Ofir
Sum
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
Step 3. Step 3. Display the averageDisplay the average9.3
Step 1.Step 1. Accept the input values, A, B, C Accept the input values, A, B, C
Step 2.Step 2. Compute the average Compute the average
Step 2.1.Step 2.1. Sum = A + B + C;Sum = A + B + C;
Step 2.2.Step 2.2. Average = Sum /3.0;Average = Sum /3.0;C
B
A 3.9
4.1
1.3
Introduction to Problem Solving S1.2.21Bina © 1998 Liran & Ofir
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
Step 3. Step 3. Display the averageDisplay the average
Step 1.Step 1. Accept the input values, A, B, C Accept the input values, A, B, C
Step 2.Step 2. Compute the average Compute the average
Step 2.1.Step 2.1. Sum = A + B + C;Sum = A + B + C;
Step 2.2.Step 2.2. Average = Sum /3.0;Average = Sum /3.0;
Average
Sum
C
B
A 3.9
4.1
1.3
9.3
3.1
Introduction to Problem Solving S1.2.22Bina © 1998 Liran & Ofir
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
Step 3. Step 3. Display the averageDisplay the average
Step 1.Step 1. Accept the input values, A, B, C Accept the input values, A, B, C
Step 2.Step 2. Compute the average Compute the average
Step 2.1.Step 2.1. Sum = A + B + C;Sum = A + B + C;
Step 2.2.Step 2.2. Average = Sum /3.0;Average = Sum /3.0;
Average
Sum
C
B
A 3.9
4.1
1.3
9.3
3.1
Average is 3.1
Introduction to Problem Solving S1.2.23Bina © 1998 Liran & Ofir
Describe the solution using a Describe the solution using a programming languageprogramming language
Describe the solution using a Describe the solution using a programming languageprogramming language
Step 3. Step 3. Display the averageDisplay the average
Step 1.Step 1. Accept the input values, A, B, C Accept the input values, A, B, C
Step 2.Step 2. Compute the average Compute the average
Step 2.1.Step 2.1. Sum = A + B + C;Sum = A + B + C;
Step 2.2.Step 2.2. Average = Sum /3.0;Average = Sum /3.0;
float A,B,C;float A,B,C;
scanf(“%f”,&A);scanf(“%f”,&A);
scanf(“%f”,&B);scanf(“%f”,&B);
scanf(“%f”,&C);scanf(“%f”,&C);
Introduction to Problem Solving S1.2.24Bina © 1998 Liran & Ofir
Describe the solution using a Describe the solution using a programming languageprogramming language
Describe the solution using a Describe the solution using a programming languageprogramming language
Step 3. Step 3. Display the averageDisplay the average
Step 1.Step 1. Accept the input values, A, B, C Accept the input values, A, B, C
Step 2.Step 2. Compute the average Compute the average
Step 2.1.Step 2.1. Sum = A + B + C;Sum = A + B + C;
Step 2.2.Step 2.2. Average = Sum /3.0;Average = Sum /3.0;
float A,B,C;float A,B,C;
scanf(“%f”,&A);scanf(“%f”,&A);
scanf(“%f”,&B);scanf(“%f”,&B);
scanf(“%f”,&C);scanf(“%f”,&C);
Introduction to Problem Solving S1.2.25Bina © 1998 Liran & Ofir
Describe the solution using a Describe the solution using a programming languageprogramming language
Describe the solution using a Describe the solution using a programming languageprogramming language
Step 3. Step 3. Display the averageDisplay the average
Step 1.Step 1. Accept the input values, A, B, C Accept the input values, A, B, C
Step 2.Step 2. Compute the average Compute the average
Step 2.1.Step 2.1. Sum = A + B + C;Sum = A + B + C;
Step 2.2.Step 2.2. Average = Sum /3.0;Average = Sum /3.0;
Sum = A + B + C;Sum = A + B + C;
float A,B,C;float A,B,C;float Sum;float Sum;
scanf(“%f”,&A);scanf(“%f”,&A);
scanf(“%f”,&B);scanf(“%f”,&B);
scanf(“%f”,&C);scanf(“%f”,&C);
Introduction to Problem Solving S1.2.26Bina © 1998 Liran & Ofir
Describe the solution using a Describe the solution using a programming languageprogramming language
Describe the solution using a Describe the solution using a programming languageprogramming language
Step 3. Step 3. Display the averageDisplay the average
Step 1.Step 1. Accept the input values, A, B, C Accept the input values, A, B, C
Step 2.Step 2. Compute the average Compute the average
Step 2.1.Step 2.1. Sum = A + B + C;Sum = A + B + C;
Step 2.2.Step 2.2. Average = Sum /3.0;Average = Sum /3.0;
Average = Sum / 3.0;Average = Sum / 3.0;
Sum = A + B + C;Sum = A + B + C;
float A,B,C;float A,B,C;float Sum;float Sum;float Average;float Average;
scanf(“%f”,&A);scanf(“%f”,&A);
scanf(“%f”,&B);scanf(“%f”,&B);
scanf(“%f”,&C);scanf(“%f”,&C);
Introduction to Problem Solving S1.2.27Bina © 1998 Liran & Ofir
Describe the solution using a Describe the solution using a programming languageprogramming language
Describe the solution using a Describe the solution using a programming languageprogramming language
Step 3. Step 3. Display the averageDisplay the average
Step 1.Step 1. Accept the input values, A, B, C Accept the input values, A, B, C
Step 2.Step 2. Compute the average Compute the average
Step 2.1.Step 2.1. Sum = A + B + C;Sum = A + B + C;
Step 2.2.Step 2.2. Average = Sum /3.0;Average = Sum /3.0;
printf(“%f”,Average);printf(“%f”,Average);Average = Sum / 3.0;Average = Sum / 3.0;
Sum = A + B + C;Sum = A + B + C;
float A,B,C;float A,B,C;float Sum;float Sum;float Average;float Average;
scanf(“%f”,&A);scanf(“%f”,&A);
scanf(“%f”,&B);scanf(“%f”,&B);
scanf(“%f”,&C);scanf(“%f”,&C);
Introduction to Problem Solving S1.2.28Bina © 1998 Liran & Ofir
Verify that the program does state Verify that the program does state the solution correctlythe solution correctly
Verify that the program does state Verify that the program does state the solution correctlythe solution correctly
Enter the programEnter the program CompileCompile RunRun Compare the output to expected outputCompare the output to expected output
Introduction to Problem Solving S1.2.29Bina © 1998 Liran & Ofir
SelectionSelectionSelectionSelection
Facilitates the selection of one of a number Facilitates the selection of one of a number of various alternatives based on a condition.of various alternatives based on a condition.
If the door is openIf the door is open
ThenThenEnter the roomEnter the room
ElseElseKnock at the doorKnock at the door
Introduction to Problem Solving S1.2.30Bina © 1998 Liran & Ofir
Understand the problemUnderstand the problemUnderstand the problemUnderstand the problem
Accept two integer values and decide whetherAccept two integer values and decide whether or not the larger number is divisible by theor not the larger number is divisible by the smaller numbersmaller number
What is the problem?What is the problem?
Introduction to Problem Solving S1.2.31Bina © 1998 Liran & Ofir
Understand the problemUnderstand the problemUnderstand the problemUnderstand the problem
What are the inputs?What are the inputs?
Input: 2 Integer ValuesInput: 2 Integer Values
Introduction to Problem Solving S1.2.32Bina © 1998 Liran & Ofir
Understand the problemUnderstand the problemUnderstand the problemUnderstand the problem
Output: YesOutput: Yes
What are the outputsWhat are the outputs
If the larger number is divisibleIf the larger number is divisibleby the second numberby the second number
Output: NoOutput: No If the larger number is not divisibleIf the larger number is not divisibleby the second numberby the second number
Introduction to Problem Solving S1.2.33Bina © 1998 Liran & Ofir
Understand the problemUnderstand the problemUnderstand the problemUnderstand the problem
Input output relationshipInput output relationship
Input:Input: 4 24 2
Output:Output: YesYes
Introduction to Problem Solving S1.2.34Bina © 1998 Liran & Ofir
Understand the problemUnderstand the problemUnderstand the problemUnderstand the problem
Input output relationshipInput output relationship
Input:Input: 3 123 12
Output:Output: YesYes
Introduction to Problem Solving S1.2.35Bina © 1998 Liran & Ofir
Understand the problemUnderstand the problemUnderstand the problemUnderstand the problem
Input output relationshipInput output relationship
Input:Input: 3 73 7
Output:Output: NoNo
Introduction to Problem Solving S1.2.36Bina © 1998 Liran & Ofir
Devise a solution to the problemDevise a solution to the problemDevise a solution to the problemDevise a solution to the problem
How is the computer to decide whetherHow is the computer to decide whether or not the larger number is divisible by theor not the larger number is divisible by the smaller number?smaller number?
Step 1.Step 1. Accept the input values Accept the input values
Computer
Introduction to Problem Solving S1.2.37Bina © 1998 Liran & Ofir
Devise a solution to the problemDevise a solution to the problemDevise a solution to the problemDevise a solution to the problem
How is the computer to decide whetherHow is the computer to decide whether or not the larger number is divisible by theor not the larger number is divisible by the smaller number?smaller number?
Computethe
result
Step 1.Step 1. Accept the input values Accept the input values
Step 2.Step 2. Compute and display Compute and display the resultthe result
Introduction to Problem Solving S1.2.38Bina © 1998 Liran & Ofir
Devise a solution to the problemDevise a solution to the problemDevise a solution to the problemDevise a solution to the problem
Refine Step 2. Refine Step 2.
Step 2.Step 2. Compute and display the result Compute and display the result Let “A” denote the first valueLet “A” denote the first value Let “B” denote the second valueLet “B” denote the second value Let “Larger” denote the larger valueLet “Larger” denote the larger value Let “Smaller” denote the smaller valueLet “Smaller” denote the smaller value
Introduction to Problem Solving S1.2.39Bina © 1998 Liran & Ofir
Devise a solution to the problemDevise a solution to the problemDevise a solution to the problemDevise a solution to the problem
Refine Step 2. Refine Step 2.
Step 2.1.Step 2.1. If Larger is divisible by SmallerIf Larger is divisible by Smaller Then print “Yes”Then print “Yes” Otherwise, Larger is not divisibleOtherwise, Larger is not divisible by Smaller and print “No”by Smaller and print “No”
Step 2.Step 2. Compute and display the result Compute and display the result
Introduction to Problem Solving S1.2.40Bina © 1998 Liran & Ofir
A
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
4
B 2
Step 1.Step 1. Accept the input values, A, B Accept the input values, A, B
Step 2.Step 2. Compute and display the result Compute and display the result
Smaller
Larger
Step 2.1.Step 2.1. If Larger is divisible byIf Larger is divisible by Smaller Then print “Yes”Smaller Then print “Yes” Otherwise, Larger is notOtherwise, Larger is not divisible by Smaller anddivisible by Smaller and print “No”print “No”
Introduction to Problem Solving S1.2.41Bina © 1998 Liran & Ofir
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
Step 1.Step 1. Accept the input values, A, B Accept the input values, A, B
Smaller
Larger
B
A
2
4
2
4Step 2.Step 2. Compute and display the result Compute and display the result
Step 2.1.Step 2.1. If Larger is divisible byIf Larger is divisible by Smaller Then print “Yes”Smaller Then print “Yes” Otherwise, Larger is notOtherwise, Larger is not divisible by Smaller anddivisible by Smaller and print “No”print “No”
Introduction to Problem Solving S1.2.42Bina © 1998 Liran & Ofir
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
Step 1.Step 1. Accept the input values, A, B Accept the input values, A, B
Smaller
Larger
B
A
2
4
2
4Step 2.Step 2. Compute and display the result Compute and display the result
Output: Output: YesYes
Step 2.1.Step 2.1. If Larger is divisible byIf Larger is divisible by Smaller Then print “Yes”Smaller Then print “Yes” Otherwise, Larger is notOtherwise, Larger is not divisible by Smaller anddivisible by Smaller and print “No”print “No”
Introduction to Problem Solving S1.2.43Bina © 1998 Liran & Ofir
A
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
3
B 12
Step 1.Step 1. Accept the input values, A, B Accept the input values, A, B
Step 2.Step 2. Compute and display the result Compute and display the result
Smaller
Larger
Step 2.1.Step 2.1. If Larger is divisible byIf Larger is divisible by Smaller Then print “Yes”Smaller Then print “Yes” Otherwise, Larger is notOtherwise, Larger is not divisible by Smaller anddivisible by Smaller and print “No”print “No”
Introduction to Problem Solving S1.2.44Bina © 1998 Liran & Ofir
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
Step 1.Step 1. Accept the input values, A, B Accept the input values, A, B
Smaller
Larger
B
A
12
12
3
3Step 2.Step 2. Compute and display the result Compute and display the result
Step 2.1.Step 2.1. If Larger is divisible byIf Larger is divisible by Smaller Then print “Yes”Smaller Then print “Yes” Otherwise, Larger is notOtherwise, Larger is not divisible by Smaller anddivisible by Smaller and print “No”print “No”
Introduction to Problem Solving S1.2.45Bina © 1998 Liran & Ofir
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
Step 1.Step 1. Accept the input values, A, B Accept the input values, A, B
Smaller
Larger
B
A
12
12
3
3Step 2.Step 2. Compute and display the result Compute and display the result
Output: Output: YesYes
Step 2.1.Step 2.1. If Larger is divisible byIf Larger is divisible by Smaller Then print “Yes”Smaller Then print “Yes” Otherwise, Larger is notOtherwise, Larger is not divisible by Smaller anddivisible by Smaller and print “No”print “No”
Introduction to Problem Solving S1.2.46Bina © 1998 Liran & Ofir
A
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
3
B 7
Step 1.Step 1. Accept the input values, A, B Accept the input values, A, B
Step 2.Step 2. Compute and display the result Compute and display the result
Smaller
Larger
Step 2.1.Step 2.1. If Larger is divisible byIf Larger is divisible by Smaller Then print “Yes”Smaller Then print “Yes” Otherwise, Larger is notOtherwise, Larger is not divisible by Smaller anddivisible by Smaller and print “No”print “No”
Introduction to Problem Solving S1.2.47Bina © 1998 Liran & Ofir
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
Step 1.Step 1. Accept the input values, A, B Accept the input values, A, B
Smaller
Larger
B
A
7
7
3
3Step 2.Step 2. Compute and display the result Compute and display the result
Step 2.1.Step 2.1. If Larger is divisible byIf Larger is divisible by Smaller Then print “Yes”Smaller Then print “Yes” Otherwise, Larger is notOtherwise, Larger is not divisible by Smaller anddivisible by Smaller and print “No”print “No”
Introduction to Problem Solving S1.2.48Bina © 1998 Liran & Ofir
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
Step 1.Step 1. Accept the input values, A, B Accept the input values, A, B
Smaller
Larger
B
A
7
7
3
3Step 2.Step 2. Compute and display the result Compute and display the result
Output: Output: NoNo
Step 2.1.Step 2.1. If Larger is divisible byIf Larger is divisible by Smaller Then print “Yes”Smaller Then print “Yes” Otherwise, Larger is notOtherwise, Larger is not divisible by Smaller anddivisible by Smaller and print “No”print “No”
Introduction to Problem Solving S1.2.49Bina © 1998 Liran & Ofir
Describe the solution using a Describe the solution using a programming languageprogramming language
Describe the solution using a Describe the solution using a programming languageprogramming language
Step 1.Step 1. Accept the input values, A, B Accept the input values, A, B
Step 2.Step 2. Compute and display the result Compute and display the result
Step 2.1.Step 2.1. If Larger is divisible byIf Larger is divisible by Smaller Then print “Yes”Smaller Then print “Yes” Otherwise, Larger is notOtherwise, Larger is not divisible by Smaller anddivisible by Smaller and print “No”print “No”
int a,b;int a,b;
scanf(“%d”,&a);scanf(“%d”,&a);
scanf(“%d”,&b);scanf(“%d”,&b);
Introduction to Problem Solving S1.2.50Bina © 1998 Liran & Ofir
Describe the solution using a Describe the solution using a programming languageprogramming language
Describe the solution using a Describe the solution using a programming languageprogramming language
Step 1.Step 1. Accept the input values, A, B Accept the input values, A, B
Step 2.Step 2. Compute and display the result Compute and display the result
Step 2.1.Step 2.1. If Larger is divisible byIf Larger is divisible by Smaller Then print “Yes”Smaller Then print “Yes” Otherwise, Larger is notOtherwise, Larger is not divisible by Smaller anddivisible by Smaller and print “No”print “No”
if (a > b) {if (a > b) {
largest = a;largest = a; smallest = b;smallest = b; }}
else {else { largest = b;largest = b;
smallest = a;smallest = a;}}
int a,b;int a,b;int largest,smallestint largest,smallestscanf(“%d”,&a);scanf(“%d”,&a);
scanf(“%d”,&b);scanf(“%d”,&b);
Introduction to Problem Solving S1.2.51Bina © 1998 Liran & Ofir
Describe the solution using a Describe the solution using a programming languageprogramming language
Describe the solution using a Describe the solution using a programming languageprogramming language
Step 1.Step 1. Accept the input values, A, B Accept the input values, A, B
Step 2.Step 2. Compute and display the result Compute and display the result
Step 2.1.Step 2.1. If Larger is divisible byIf Larger is divisible by Smaller Then print “Yes”Smaller Then print “Yes” Otherwise, Larger is notOtherwise, Larger is not divisible by Smaller anddivisible by Smaller and print “No”print “No”
If (largest % smallest == 0)If (largest % smallest == 0) printf(“Yes”);printf(“Yes”);else else printf(“No”);printf(“No”);
if (a > b) {if (a > b) { largest = a;largest = a;
smallest = b;smallest = b; }}
else {else { largest = b;largest = b;
smallest = a;smallest = a;}}
int a,b;int a,b;int largest,smallestint largest,smallestscanf(“%d”,&a);scanf(“%d”,&a);scanf(“%d”,&b);scanf(“%d”,&b);
Introduction to Problem Solving S1.2.52Bina © 1998 Liran & Ofir
IterationIterationIterationIteration
Facilitates the repeated execution of a Facilitates the repeated execution of a segment of the program.segment of the program. While you are hungryWhile you are hungry
Buy a sandwichBuy a sandwich Eat the sandwichEat the sandwich
While you haven’t read the last slideWhile you haven’t read the last slide Read the next slideRead the next slide
Introduction to Problem Solving S1.2.53Bina © 1998 Liran & Ofir
Understand the problemUnderstand the problemUnderstand the problemUnderstand the problem
What is the problem?What is the problem?
Accept an Integer, N as input and print theAccept an Integer, N as input and print the value of 1+ 2+ … + Nvalue of 1+ 2+ … + N
Introduction to Problem Solving S1.2.54Bina © 1998 Liran & Ofir
Understand the problemUnderstand the problemUnderstand the problemUnderstand the problem
What are the inputs?What are the inputs?
Input: one integer value, NInput: one integer value, N
Introduction to Problem Solving S1.2.55Bina © 1998 Liran & Ofir
Understand the problemUnderstand the problemUnderstand the problemUnderstand the problem
What are the outputsWhat are the outputs
Output: 1 + 2 + Output: 1 + 2 + …… + N + N
Introduction to Problem Solving S1.2.56Bina © 1998 Liran & Ofir
Understand the problemUnderstand the problemUnderstand the problemUnderstand the problem
Input output relationshipInput output relationship
Input:Input: 33
Output:Output: 66
Introduction to Problem Solving S1.2.57Bina © 1998 Liran & Ofir
Understand the problemUnderstand the problemUnderstand the problemUnderstand the problem
Input output relationshipInput output relationship
Input:Input: 44
Output:Output: 1010
Introduction to Problem Solving S1.2.58Bina © 1998 Liran & Ofir
Devise a solution to the problemDevise a solution to the problemDevise a solution to the problemDevise a solution to the problem
How is the computer to print the value of How is the computer to print the value of 1+ 2+ … + N1+ 2+ … + N
Step 1.Step 1. Accept the input value Accept the input value
Step 2.Step 2. Compute the sum Compute the sum
Step 3. Step 3. Display the sumDisplay the sum
Introduction to Problem Solving S1.2.59Bina © 1998 Liran & Ofir
Devise a solution to the problemDevise a solution to the problemDevise a solution to the problemDevise a solution to the problem
Refine Step 2Refine Step 2
Step 2.Step 2. Compute the sum Compute the sumStep 2.1.Step 2.1. Sum = 0; Sum = 0;
Step 2.1.Step 2.1. Count = 1; Count = 1; Step 2.3.Step 2.3. Repeat steps 2.3.1 - 2.3.2 N timesRepeat steps 2.3.1 - 2.3.2 N times
Step 2.3.1Step 2.3.1.. Sum = Sum + Count;Sum = Sum + Count;Step 2.3.2Step 2.3.2.. Count = Count +1;Count = Count +1;
Introduction to Problem Solving S1.2.60Bina © 1998 Liran & Ofir
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
Step 3. Step 3. Display the sumDisplay the sum
Step 1.Step 1. Accept the input value Accept the input value
Step 2.Step 2. Compute the sum Compute the sum
N 3
Step 2.1.Step 2.1. Sum = 0; Sum = 0;
Step 2.1.Step 2.1. Count = 1; Count = 1; Step 2.3.Step 2.3. Repeat steps 2.3.1 - 2.3.2 N timesRepeat steps 2.3.1 - 2.3.2 N times
Step 2.3.1Step 2.3.1.. Sum = Sum + Count;Sum = Sum + Count;
Step 2.3.2Step 2.3.2.. Count = Count +1;Count = Count +1;
Introduction to Problem Solving S1.2.61Bina © 1998 Liran & Ofir
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
N 3
Step 3. Step 3. Display the sumDisplay the sum
Step 1.Step 1. Accept the input value Accept the input value
Step 2.Step 2. Compute the sum Compute the sum
Step 2.1.Step 2.1. Sum = 0; Sum = 0;
Step 2.3.Step 2.3. Repeat steps 2.3.1 - 2.3.2 N timesRepeat steps 2.3.1 - 2.3.2 N times
Step 2.3.1Step 2.3.1.. Sum = Sum + Count;Sum = Sum + Count;
Step 2.1.Step 2.1. Count = 1; Count = 1;
Step 2.3.2Step 2.3.2.. Count = Count +1;Count = Count +1;
Introduction to Problem Solving S1.2.62Bina © 1998 Liran & Ofir
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
N 3
Sum 0
Step 3. Step 3. Display the sumDisplay the sum
Step 1.Step 1. Accept the input value Accept the input value
Step 2.Step 2. Compute the sum Compute the sum
Step 2.1.Step 2.1. Sum = 0; Sum = 0;
Step 2.3.Step 2.3. Repeat steps 2.3.1 - 2.3.2 N timesRepeat steps 2.3.1 - 2.3.2 N times
Step 2.3.1Step 2.3.1.. Sum = Sum + Count;Sum = Sum + Count;
Step 2.1.Step 2.1. Count = 1; Count = 1;
Step 2.3.2Step 2.3.2.. Count = Count +1;Count = Count +1;
Introduction to Problem Solving S1.2.63Bina © 1998 Liran & Ofir
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
N 3
Sum 0
Step 3. Step 3. Display the sumDisplay the sum
Step 1.Step 1. Accept the input value Accept the input value
Step 2.Step 2. Compute the sum Compute the sum
Step 2.1.Step 2.1. Sum = 0; Sum = 0;
Step 2.3.Step 2.3. Repeat steps 2.3.1 - 2.3.2 N timesRepeat steps 2.3.1 - 2.3.2 N times
Step 2.3.1Step 2.3.1.. Sum = Sum + Count;Sum = Sum + Count;
Step 2.1.Step 2.1. Count = 1; Count = 1;
Step 2.3.2Step 2.3.2.. Count = Count +1;Count = Count +1;
Count 1
Introduction to Problem Solving S1.2.64Bina © 1998 Liran & Ofir
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
N 3
Sum 0
Step 3. Step 3. Display the sumDisplay the sum
Step 1.Step 1. Accept the input value Accept the input value
Step 2.Step 2. Compute the sum Compute the sum
Step 2.1.Step 2.1. Sum = 0; Sum = 0;
Step 2.3.Step 2.3. Repeat steps 2.3.1 - 2.3.2 N timesRepeat steps 2.3.1 - 2.3.2 N times
Step 2.3.1Step 2.3.1.. Sum = Sum + Count;Sum = Sum + Count;
Step 2.1.Step 2.1. Count = 1; Count = 1;
Step 2.3.2Step 2.3.2.. Count = Count +1;Count = Count +1;
Count 1
Introduction to Problem Solving S1.2.65Bina © 1998 Liran & Ofir
Sum 01
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
N 3
Step 3. Step 3. Display the sumDisplay the sum
Step 1.Step 1. Accept the input value Accept the input value
Step 2.Step 2. Compute the sum Compute the sum
Step 2.1.Step 2.1. Sum = 0; Sum = 0;
Step 2.3.Step 2.3. Repeat steps 2.3.1 - 2.3.2 N timesRepeat steps 2.3.1 - 2.3.2 N times
Step 2.3.1Step 2.3.1.. Sum = Sum + Count;Sum = Sum + Count;
Step 2.1.Step 2.1. Count = 1; Count = 1;
Step 2.3.2Step 2.3.2.. Count = Count +1;Count = Count +1;
Count 1
Introduction to Problem Solving S1.2.66Bina © 1998 Liran & Ofir
1
Sum 1
2
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
N 3
Step 3. Step 3. Display the sumDisplay the sum
Step 1.Step 1. Accept the input value Accept the input value
Step 2.Step 2. Compute the sum Compute the sum
Step 2.1.Step 2.1. Sum = 0; Sum = 0;
Step 2.3.Step 2.3. Repeat steps 2.3.1 - 2.3.2 N timesRepeat steps 2.3.1 - 2.3.2 N times
Step 2.3.1Step 2.3.1.. Sum = Sum + Count;Sum = Sum + Count;
Step 2.1.Step 2.1. Count = 1; Count = 1;
Step 2.3.2Step 2.3.2.. Count = Count +1;Count = Count +1;
Count
Introduction to Problem Solving S1.2.67Bina © 1998 Liran & Ofir
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
N 3
Sum 1
Step 3. Step 3. Display the sumDisplay the sum
Step 1.Step 1. Accept the input value Accept the input value
Step 2.Step 2. Compute the sum Compute the sum
Step 2.1.Step 2.1. Sum = 0; Sum = 0;
Step 2.3.Step 2.3. Repeat steps 2.3.1 - 2.3.2 N timesRepeat steps 2.3.1 - 2.3.2 N times
Step 2.3.1Step 2.3.1.. Sum = Sum + Count;Sum = Sum + Count;
Step 2.1.Step 2.1. Count = 1; Count = 1;
Step 2.3.2Step 2.3.2.. Count = Count +1;Count = Count +1;
Count 2
Introduction to Problem Solving S1.2.68Bina © 1998 Liran & Ofir
Sum 13
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
N 3
Step 3. Step 3. Display the sumDisplay the sum
Step 1.Step 1. Accept the input value Accept the input value
Step 2.Step 2. Compute the sum Compute the sum
Step 2.1.Step 2.1. Sum = 0; Sum = 0;
Step 2.3.Step 2.3. Repeat steps 2.3.1 - 2.3.2 N timesRepeat steps 2.3.1 - 2.3.2 N times
Step 2.3.1Step 2.3.1.. Sum = Sum + Count;Sum = Sum + Count;
Step 2.1.Step 2.1. Count = 1; Count = 1;
Step 2.3.2Step 2.3.2.. Count = Count +1;Count = Count +1;
Count 2
Introduction to Problem Solving S1.2.69Bina © 1998 Liran & Ofir
2
Sum 3
3
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
N 3
Step 3. Step 3. Display the sumDisplay the sum
Step 1.Step 1. Accept the input value Accept the input value
Step 2.Step 2. Compute the sum Compute the sum
Step 2.1.Step 2.1. Sum = 0; Sum = 0;
Step 2.3.Step 2.3. Repeat steps 2.3.1 - 2.3.2 N timesRepeat steps 2.3.1 - 2.3.2 N times
Step 2.3.1Step 2.3.1.. Sum = Sum + Count;Sum = Sum + Count;
Step 2.1.Step 2.1. Count = 1; Count = 1;
Step 2.3.2Step 2.3.2.. Count = Count +1;Count = Count +1;
Count
Introduction to Problem Solving S1.2.70Bina © 1998 Liran & Ofir
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
N 3
Sum 3
Step 3. Step 3. Display the sumDisplay the sum
Step 1.Step 1. Accept the input value Accept the input value
Step 2.Step 2. Compute the sum Compute the sum
Step 2.1.Step 2.1. Sum = 0; Sum = 0;
Step 2.3.Step 2.3. Repeat steps 2.3.1 - 2.3.2 N timesRepeat steps 2.3.1 - 2.3.2 N times
Step 2.3.1Step 2.3.1.. Sum = Sum + Count;Sum = Sum + Count;
Step 2.1.Step 2.1. Count = 1; Count = 1;
Step 2.3.2Step 2.3.2.. Count = Count +1;Count = Count +1;
Count 3
Introduction to Problem Solving S1.2.71Bina © 1998 Liran & Ofir
Sum 36
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
N 3
Step 3. Step 3. Display the sumDisplay the sum
Step 1.Step 1. Accept the input value Accept the input value
Step 2.Step 2. Compute the sum Compute the sum
Step 2.1.Step 2.1. Sum = 0; Sum = 0;
Step 2.3.Step 2.3. Repeat steps 2.3.1 - 2.3.2 N timesRepeat steps 2.3.1 - 2.3.2 N times
Step 2.3.1Step 2.3.1.. Sum = Sum + Count;Sum = Sum + Count;
Step 2.1.Step 2.1. Count = 1; Count = 1;
Step 2.3.2Step 2.3.2.. Count = Count +1;Count = Count +1;
Count 3
Introduction to Problem Solving S1.2.72Bina © 1998 Liran & Ofir
3
Sum 6
4
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
N 3
Step 3. Step 3. Display the sumDisplay the sum
Step 1.Step 1. Accept the input value Accept the input value
Step 2.Step 2. Compute the sum Compute the sum
Step 2.1.Step 2.1. Sum = 0; Sum = 0;
Step 2.3.Step 2.3. Repeat steps 2.3.1 - 2.3.2 N timesRepeat steps 2.3.1 - 2.3.2 N times
Step 2.3.1Step 2.3.1.. Sum = Sum + Count;Sum = Sum + Count;
Step 2.1.Step 2.1. Count = 1; Count = 1;
Step 2.3.2Step 2.3.2.. Count = Count +1;Count = Count +1;
Count
Introduction to Problem Solving S1.2.73Bina © 1998 Liran & Ofir
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
N 3
Sum 6
Step 3. Step 3. Display the sumDisplay the sum
Step 1.Step 1. Accept the input value Accept the input value
Step 2.Step 2. Compute the sum Compute the sum
Step 2.1.Step 2.1. Sum = 0; Sum = 0;
Step 2.3.Step 2.3. Repeat steps 2.3.1 - 2.3.2 N timesRepeat steps 2.3.1 - 2.3.2 N times
Step 2.3.1Step 2.3.1.. Sum = Sum + Count;Sum = Sum + Count;
Step 2.1.Step 2.1. Count = 1; Count = 1;
Step 2.3.2Step 2.3.2.. Count = Count +1;Count = Count +1;
Count 4
Introduction to Problem Solving S1.2.74Bina © 1998 Liran & Ofir
Sum 06
Verify that the solution is correctVerify that the solution is correctVerify that the solution is correctVerify that the solution is correct
N 3
Step 3. Step 3. Display the sumDisplay the sum
Step 1.Step 1. Accept the input value Accept the input value
Step 2.Step 2. Compute the sum Compute the sum
Step 2.1.Step 2.1. Sum = 0; Sum = 0;
Step 2.3.Step 2.3. Repeat steps 2.3.1 - 2.3.2 N timesRepeat steps 2.3.1 - 2.3.2 N times
Step 2.3.1Step 2.3.1.. Sum = Sum + Count;Sum = Sum + Count;
Step 2.1.Step 2.1. Count = 1; Count = 1;
Step 2.3.2Step 2.3.2.. Count = Count +1;Count = Count +1;
Count 4
6
Introduction to Problem Solving S1.2.75Bina © 1998 Liran & Ofir
Step 2.3.1Step 2.3.1.. Sum = Sum + Count;Sum = Sum + Count;
Describe the solution using a Describe the solution using a programming languageprogramming language
Describe the solution using a Describe the solution using a programming languageprogramming language
Step 3. Step 3. Display the sumDisplay the sum
Step 1.Step 1. Accept the input value Accept the input value
Step 2.Step 2. Compute the sum Compute the sum
Step 2.1.Step 2.1. Sum = 0; Sum = 0;
Step 2.3.Step 2.3. Repeat steps 2.3.1 - 2.3.2 N timesRepeat steps 2.3.1 - 2.3.2 N times
Step 2.1.Step 2.1. Count = 1; Count = 1;
Step 2.3.2Step 2.3.2.. Count = Count +1;Count = Count +1;
int n;int n;
scanf(“%d”,&n);scanf(“%d”,&n);
Introduction to Problem Solving S1.2.76Bina © 1998 Liran & Ofir
Describe the solution using a Describe the solution using a programming languageprogramming language
Describe the solution using a Describe the solution using a programming languageprogramming language
int sum;int sum;
int n;int n;
sum = 0;sum = 0;
Step 2.3.1Step 2.3.1.. Sum = Sum + Count;Sum = Sum + Count;
Step 3. Step 3. Display the sumDisplay the sum
Step 1.Step 1. Accept the input value Accept the input value
Step 2.Step 2. Compute the sum Compute the sum
Step 2.1.Step 2.1. Sum = 0; Sum = 0;
Step 2.3.Step 2.3. Repeat steps 2.3.1 - 2.3.2 N timesRepeat steps 2.3.1 - 2.3.2 N times
Step 2.1.Step 2.1. Count = 1; Count = 1;
Step 2.3.2Step 2.3.2.. Count = Count +1;Count = Count +1;
scanf(“%d”,&n);scanf(“%d”,&n);
Introduction to Problem Solving S1.2.77Bina © 1998 Liran & Ofir
Describe the solution using a Describe the solution using a programming languageprogramming language
Describe the solution using a Describe the solution using a programming languageprogramming language
count = 1;count = 1;
Step 2.3.1Step 2.3.1.. Sum = Sum + Count;Sum = Sum + Count;
Step 3. Step 3. Display the sumDisplay the sum
Step 1.Step 1. Accept the input value Accept the input value
Step 2.Step 2. Compute the sum Compute the sum
Step 2.1.Step 2.1. Sum = 0; Sum = 0;
Step 2.3.Step 2.3. Repeat steps 2.3.1 - 2.3.2 N timesRepeat steps 2.3.1 - 2.3.2 N times
Step 2.1.Step 2.1. Count = 1; Count = 1;
Step 2.3.2Step 2.3.2.. Count = Count +1;Count = Count +1;
int count;int count;
int n;int n;int sum;int sum;
sum = 0;sum = 0;
scanf(“%d”,&n);scanf(“%d”,&n);
Introduction to Problem Solving S1.2.78Bina © 1998 Liran & Ofir
Describe the solution using a Describe the solution using a programming languageprogramming language
Describe the solution using a Describe the solution using a programming languageprogramming language
While (count <= n){While (count <= n){
}}
Step 2.3.1Step 2.3.1.. Sum = Sum + Count;Sum = Sum + Count;
Step 3. Step 3. Display the sumDisplay the sum
Step 1.Step 1. Accept the input value Accept the input value
Step 2.Step 2. Compute the sum Compute the sum
Step 2.1.Step 2.1. Sum = 0; Sum = 0;
Step 2.3.Step 2.3. Repeat steps 2.3.1 - 2.3.2 N timesRepeat steps 2.3.1 - 2.3.2 N times
Step 2.1.Step 2.1. Count = 1; Count = 1;
Step 2.3.2Step 2.3.2.. Count = Count +1;Count = Count +1;
count = 1;count = 1;
int sum;int sum;int count;int count;
int n;int n;
sum = 0;sum = 0;
scanf(“%d”,&n);scanf(“%d”,&n);
Introduction to Problem Solving S1.2.79Bina © 1998 Liran & Ofir
Describe the solution using a Describe the solution using a programming languageprogramming language
Describe the solution using a Describe the solution using a programming languageprogramming language
sum = sum+count;sum = sum+count;Step 2.3.1Step 2.3.1.. Sum = Sum + Count;Sum = Sum + Count;
Step 3. Step 3. Display the sumDisplay the sum
Step 1.Step 1. Accept the input value Accept the input value
Step 2.Step 2. Compute the sum Compute the sum
Step 2.1.Step 2.1. Sum = 0; Sum = 0;
Step 2.3.Step 2.3. Repeat steps 2.3.1 - 2.3.2 N timesRepeat steps 2.3.1 - 2.3.2 N times
Step 2.1.Step 2.1. Count = 1; Count = 1;
Step 2.3.2Step 2.3.2.. Count = Count +1;Count = Count +1;
While (count <= n){While (count <= n){
}}
count = 1;count = 1;
int sum;int sum;int count;int count;
int n;int n;
sum = 0;sum = 0;
scanf(“%d”,&n);scanf(“%d”,&n);
Introduction to Problem Solving S1.2.80Bina © 1998 Liran & Ofir
Describe the solution using a Describe the solution using a programming languageprogramming language
Describe the solution using a Describe the solution using a programming languageprogramming language
Step 2.3.1Step 2.3.1.. Sum = Sum + Count;Sum = Sum + Count;
Step 3. Step 3. Display the sumDisplay the sum
Step 1.Step 1. Accept the input value Accept the input value
Step 2.Step 2. Compute the sum Compute the sum
Step 2.1.Step 2.1. Sum = 0; Sum = 0;
Step 2.3.Step 2.3. Repeat steps 2.3.1 - 2.3.2 N timesRepeat steps 2.3.1 - 2.3.2 N times
Step 2.1.Step 2.1. Count = 1; Count = 1;
Step 2.3.2Step 2.3.2.. Count = Count +1;Count = Count +1; sum = sum+count; sum = sum+count;
}}
While (count <= n){While (count <= n){
count = 1;count = 1;
int sum;int sum;int count;int count;
int n;int n;
sum = 0;sum = 0;
scanf(“%d”,&n);scanf(“%d”,&n);
count = count +1;count = count +1;
Introduction to Problem Solving S1.2.81Bina © 1998 Liran & Ofir
Describe the solution using a Describe the solution using a programming languageprogramming language
Describe the solution using a Describe the solution using a programming languageprogramming language
printf(“%d”,sum);printf(“%d”,sum);
Step 2.3.1Step 2.3.1.. Sum = Sum + Count;Sum = Sum + Count;
Step 3. Step 3. Display the sumDisplay the sum
Step 1.Step 1. Accept the input value Accept the input value
Step 2.Step 2. Compute the sum Compute the sum
Step 2.1.Step 2.1. Sum = 0; Sum = 0;
Step 2.3.Step 2.3. Repeat steps 2.3.1 - 2.3.2 N timesRepeat steps 2.3.1 - 2.3.2 N times
Step 2.1.Step 2.1. Count = 1; Count = 1;
Step 2.3.2Step 2.3.2.. Count = Count +1;Count = Count +1; sum = sum+count;sum = sum+count; count = count +1;count = count +1;
While (count <= n){While (count <= n){
}}
count = 1;count = 1;
int sum;int sum;int count;int count;
int n;int n;
sum = 0;sum = 0;
scanf(“%d”,&n);scanf(“%d”,&n);