Algorithms Practice Topics In-Class Project: Tip Calculator

advertisement
Algorithms Practice
Topics


In-Class Project: Tip Calculator
In-Class Project: Drawing a Rectangle
1
Writing Algorithms from
Scratch


Given a problem statement, we are going to
write the corresponding generic algorithm for
the solution.
We will use the following procedure:


Determine the algorithm inputs and outputs
Develop the pseudocode
2
Tip Calculator
Problem: Write an interactive program to
calculate the dollar amount of tip on a
restaurant bill given the percentage of tip.
You should allow for changes in the total
price of the bill and the tip percentage. Error
checking should be done to be sure that the
amount of the bill is greater than 0.
3
Drawing a Rectangle
Problem: Write an interactive program that will
draw a solid rectangle of asterisks (*). You
should allow for changes in the height and
width of the rectangle. Error checking must
be done to be sure that the dimensions are
greater than zero.
4
Group Code Examples
Comments:
[General remark: it was a bit unfair
of us to make you use loops, which
we don’t get to for several weeks—
but you guys did great!]
•Use
parentheses around the test
parts of selection and repetition
structures
•This
explicit syntax gives us
leeway in formatting our code
nested loops – a very
important concept
•Note
loop varies fastest –
counterintuitive
•Innermost
Comments:
•Note
this group chose to count
down instead of up—in this case,
your choice
•In
other situations, there is natural
preference
•Note
re-initialization of inner loop
start value
•Slightly
more elegant to put at
start of repetition
Comments:
•This
was the only group that did
the required error-checking on the
input—good for them!
•I think your “<width>=0” was
supposed to be for <width2>
Comments:
best not to use I, j –type
variable names
•You are leaving realm of pseudocode
for real code with declarations like
“integer” here…
•Note alternative syntax of using “{…}”
to denote the nested part of the
repetition block
•Generally,
•Can
•Just
•Why
also use with selection blocks
be consistent!!
4 levels of nested loops?
•2nd
and 4th repetition structures will
loop infinitely
•I think you meant to use “if”, but
actually, neither is necessary
•Inote
consistency w/semantics of
“Display”—good.
•n
future assignments, you can
assume “Display” will put in a newline
at the end of each request
•If you don’t want this behavior, for
some reason, just explain at the top…
or can use special keyword, like
“Display_without_newline”
Drawing a Triangle - Advanced
Problem: Write an interactive program that will
draw a solid isosceles triangle of asterisks (*).
You should allow for changes in the height of
the triangle. Error checking must be done to
be sure that the dimension is greater than
zero.
10
Student Voluteerizer
Problem: Write a generic algorithm for helping
call on student “volunteers” in a “fair” manner
<obviously underspecified…>
11
Download