CSC 150 Chapter 2: Representing algorithms
primary information resource:
An Invitation to Computer Science (Java), Third Edition,
G. Michael Schneider and Judith L. Gersting,
Course Technology, 2007.
[ previous | schedule | next ]
Two that come to mind are: a natural language (e.g. English) or a programming language (e.g. Java)
Critique English as means for representing an algorithm.
(ADV: well known notation; DIS: ambiguous, too loosely structured)
Critique Java as a means for representing an algorithm.
(ADV: structured and nonambiguous; DIS: too highly structured, not
abstract enough)
middle ground approach normally used: pseudocode .
structured subset of English with programming language symbols used
to efficiently write calculations and expressions
establish consistent notation for the three kinds of operations (once
again, what are they?)
1. sequential operations
2. conditional operations
3. iterative operations
The pseudocode language used in the textbook and lab manual is pretty simple.
Sequential operations (computation/assignment, input, output) have the formats:
Additional notes about While loops:
Keep in mind that the actual syntax does not have to conform exactly
to these formats. It is important, however, that the meaning is unambiguous
and clear. For example, the operation "Add 1 to count" means the
same as "Set the value of count to count+1" (and is probably clearer, to
boot).
Given the pseudocode language, let's create some algorithms.
Get length and width of piece of carpet in feet, and cost per square yard. Compute and output total cost including 6% sales tax.
Input the beginning and ending electric meter reading for a month in kilowatt hours (KWH). Compute charge based on rate of 6 cents per KWH for the first 1000 KWH used and 8 cents per KWH for excess over 1000 KWH. If less than 500 KWH was used, output a message congratulating customer on conserving energy.
Alice is thinking of a number between 1 and 10. Two brothers, Bob and Charlie, each guess what it is. Whoever is closer gets a kiss from Alice. In case of a tie, both have to kiss their sister Kate. Write an algorithm to get Bob and Charlie's guesses, determine the outcome and output an appropriate message. Alice's number is fixed at the beginning of the program (e.g., start your program with: Set Target to 7)
Alice is thinking of a number between 1 and 10. Bob has three chances to guess it correctly. If he does, Alice kisses him and the game is over. If he does not, he has to kiss his sister Kate. Write an algorithm to get Bob's guess(es), determine the outcome for each guess and output appropriate messages. Alice's number is fixed at the beginning of the program.
Alice is thinking of a number between 1 and 10. Two brothers, Bob and Charlie, take turns guessing what it is until one guesses correctly. First correct guesser gets a kiss and then the game is over. Write an algorithm to get Bob and Charlie's guesses, determine the outcome for each guess and output appropriate messages. Alice's number is fixed at the beginning of the program.
Homework for next class: Given a text string, determine whether or
not it is a palindrome (spelled the same either forward or backward).
The year 2002 is itself a palindrome, the last one until 2112. Assume
that all spaces and special characters have already been removed from the
string. Write an algorithm to get a string, conduct the test, and
output the result.