CISC105 Spring 2006 Lab05 CISC105 Spring 2006 Lab05

Programs

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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
    *
     *
      *
       *
        *
    % 
    
    


  6. 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.
    1. Add another function that prints a line of asterisks instead of a line of spaces.

    2. Take the code that prints the diagonal line of asterisks and put it inside a function called "downRight".

    3. Add another function called "upRight" that prints a diagonal going the other way:
         *
        *
       *
      *
      
      


    4. Add a function called "bigTop" that uses two other functions that you've already written to print figures like this:
      ****
       ***
        **
         *
      
      


    5. Add a function called "littleTop", similar to the last, that prints
         *
        **
       ***
      ****
      
      


    6. 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.