Conditional algorithms have selection statements in them. Their control structure is known as Selection Control Structure.
The first illustration is that of the if-then construct.
The second illustration is that of the if-then-else construct.
Examples:
1. If grade is 70 or more, output Pass.
2. If grade is 70 or more, output Pass; otherwise output Fail.
Example Problem:
Write an algorithm that determines and outputs if the average of a student allows him to pass or fail; the passing mark is 70.
Solution #1: if-then construct
Print "Enter the average of a student"
input avg
if avg >= 70 then
print "Pass"
end if
input key
Solution #2: if-then-else construct
Print "Enter the average of a student"
input avg
if avg >= 70 then
print "Pass"
else
print "Fail"
end if
input key
Now you try one:
Write an algorithm that prompts the user to enter the name and cost of two items. The algorithm should calculate and output the total cost. If the total exceeds $25.00 then offer a discount of 10%; otherwise a discount of 5%; show the new total after discount.
Sunday, February 22, 2015
Algorithms
There are four types of algorithms: Simple, Conditional, Loops and Arrays
Simple Algorithms - basic calculations
Conditional Algorithms - selection statements
Algorithms are series of instructions that rigorously defines the solution to a problem.
The attributes of an algorithm are:
Simple Algorithms - basic calculations
Conditional Algorithms - selection statements
Algorithms are series of instructions that rigorously defines the solution to a problem.
The attributes of an algorithm are:
- Must be precise
- Must be ambiguous
- Must be finite
- Must be done in a logical order
Example of given problem:
Write an algorithm that prompts the user to enter two grades, and shows the average of the two grades.
Indicators that it is a simple algorithm: basic calculation of entering data, performing calculation, and outputting results.
Solution:
Print "Enter a grade"
Input grade1
Print "Enter another grade"
Input grade2
Let total = grade1 + grade2
Let avg = total / 2
Print "The average of two grades is", avg
Input key
Sunday, February 8, 2015
Problem Solving Phase
Click on the link below for a short video about the five steps in the problem solving phase along with an important message.
Intro to Programming by Ms Young
Intro to Programming by Ms Young
Thursday, February 5, 2015
Problem Solving Phase
The key to solving a problem is understanding what is being asked to do, logic how
to do it, and get it done.
The Five (5) Steps in Problem Solving Phase
- Define the problem
- Find a solution
- Evaluate alternate solutions
- Choose the most efficient solution and write an algorithm
- Test algorithm for correctness
To define the problem, programmers use a defining diagram, commonly referred to as an IPO table.
Defining Diagram - a three column table that separates a given problem into Input, Processing, and Output.
The IPO table does not solve the problem; it simply outline the procedural steps. Calculations are not shown in the table.
Example of IPO Table
Problem: Calculate and output the total cost of three items.
Input
|
Processing
|
Output
|
Cost1
Cost2
Cost3
|
1.
Read Cost1, Cost2, Cost3
2.
Calculate Total
3.
Print Total
|
Total
|
Input
We have assigned a name (variable name) to each of the cost values that will be entered.
we will learn more about variables at a later point.
Processing
Each processing step is numbered.
The processing steps are what steps the computer will have to do to solve the problem.
The first processing step is always to (read) the variables that are inputted.
The final processing step is always to display (or print) was is to be outputted.
Output
Anything that needs to be displayed or the result of what is being asked to solve
You try one...
Prepare the defining diagram for the following problem: Calculate and output the average of two quiz grades. (hint...there are four processing steps)
Subscribe to:
Posts (Atom)