CISC105 Spring 2006 Lab05
CISC105 Spring 2006 Lab05
Write a program for each of the following problems. Be sure to
save every separate program. All programs must be properly commented
and indented (see Assignment Standards on the class website).
Many of the functions below are very similar to one another. You
may take advantage of this to reduce the work you have to do - copy
a working function and modify it to do a new task. However, because
you are just learning the language, you should also practice writing
functions from scratch a lot.
Name each program lab05.n.c, where n is the number in the list
below. For example, the name of the file for the first will be
lab05.1.c. Put the files in your lab05 directory.
Programs
Write a function (what three things do you
need?). The function will not return a value. It will take a single
integer parameter named size and will print a square of
asterisks that size. Call the function from inside main. After the
function is working correctly, add a loop in main() so that it
continually asks the user for the size, prints the square if the
number is positive, and quits if the number is negative or zero. Be
sure your prompt explains the behavior.
Write a function called indent. It will be a void
function and takes a single integer parameter called
length. If the number is negative, the function prints
"error" and returns. Otherwise, it prints length spaces
(nothing else) and then returns.
Write a function that returns the value of its double input parameter
squared. Call it from within a loop in main so it can be easily
tested. Your final program should not have any print statements
inside the function, though you may use them there for debugging.
Write a function that takes two parameters, double
base and positive integer exponent (zero is positive). Use a loop to calculate base to the power exponent, and return the answer and print it in main() where your function is called from inside a loop.
Write a program that uses indent to create
the indents for a drawing. Your program will use
a for loop that calls indent to print the following (based on user input):
% ./a.out
Enter height: 5
*
*
*
*
*
%
Copy and expand the program from 5. All drawing functions must
take a size parameter. Don't forget to save copies of your program
each time you get something new to work.
Add another function
that prints a line of asterisks instead of a line of spaces.
Take the code that prints the diagonal line of asterisks and
put it inside a function called "downRight".
Add another function called "upRight" that prints a diagonal
going the other way:
*
*
*
*
Add a function called "bigTop" that uses two other functions
that you've already written to print figures like this:
****
***
**
*
Add a function called "littleTop", similar to the last, that
prints
*
**
***
****
Add a loop and a switch statement so a user can choose what
figure to print next. The program should only ask for size in one
place; that size is passed to the selected drawing function as a
parameter. Use a simple menu with integer choices for each drawing
and another choice to quit.
You should have a total of 6 programs named lab05.1.c to
lab05.6.c. Make a single script file (see lab00 for the
instructions) where you cat, compile, and run each one in its final form.
Submit all 6 program files and your script on WebCT, and give the paper version of the
complete script file only on paper to your TA
at the beginning of your next lab (Wednesday labs submit
Friday at the beginning of class). Note: Cat, compile, and run each
program in order! Do not cat all programs, then compile, etc.
After you finish your lab, go back and look at number 6. While
having functions like indent can be useful ways to
decompose the parts of a drawing, we can also work without them,
especially in simple cases like this. Try to rewrite all of 6
with just loops, no functions. For bigTop and littleTop, think of
writing a loop that always prints a line of length size; but at some
point in the line you stop printing spaces and start printing asterisks.
File translated from
TEX
by
TTH,
version 3.38. On 15 Mar 2006, 00:11.