CIS071 Lab06 - Stick Figures with Loops                                              DUE: (October 12 at the start of lab) 


 A Harder Case Study: Drawing Stick Figures (Part II)

(Introduction to loops, more on i/o, more on input arguments and functions with return values)

Lab06:  Return to Lab 03, our first lab in drawing either male or female stick figures. Make a copy of that lab for use as the Starter Program for this lab. In this lab we will also draw male or female stick figures depending upon a character code (‘M’ or ‘F’ – or ‘m’ or ‘f’) entered by the user.  This time, the figure you draw should look like one of the two figures shown below.

               $                                         %         Width of line is 1
             $   $                                     %   %       Width of line is 5
            $     $                                   %     %      Width of line is 9
             $   $                                     %   %
               $                                         %
           *********                                     *
           *       *                                    * *
           *       *                                   *   *
           *       *                                  *     *     
           *********                                 *********
               *                                         *
              * *                                       * *    
             *   *                                     *   *
            *     *                                   *     * 
 
             MALE                                     FEMALE           
            (h==5)                                    (h==5)
As was the case with Lab03, your program should begin by asking the user whether s/he wants to draw a male (enter M or m) or a female (enter F or f) figure. So far, this is all as it was for Lab03. However, at this point, we are going to graduate into something more complicated.

1) After asking which figure to draw, the program should ask the user to a) pick a single character to use in drawing the head of a figure (headChar) and b) pick another character to draw the rest of a figure (drawChar). You will need to write two separate functions, selectHeadChar and selectDrawChar to do this.

2) The program will next ask the user to enter the height, h, of each of the figure’s parts. Another function, getHeight will be used to do this. Note that the height of a figure’s head and body are the same (h); the height of the legs should be h-1 (it is an inverted triangle without a base).

3) We then compute the width of the body (wBody = 2*h-1). If you drew the arms, compute the width of the arms to be wBody+2. The maximum width of the head is wBody-2.

4) Drawing the head ... If you want a skinny head, decrease the number of blanks in the middle of each line by 2. If you want a fatter (perhaps better proportioned) head, increase the number of blanks in the middle of each line by 2. In drawing the head, note the following:
    For a fatter head --
     -- the width of the middle line (if h is odd) is 2*h-1
     -- the width of the two middle lines (if h is even) is 2*h-3 (you will not use this fact in your program)
    For a skinnier head --
     -- the width of the middle line (if h is odd) is h
     -- the width of the two middle lines (if h is even) is h-1 (again, you will not use this fact in your program) 

5) (The really hard part) We now have to rewrite our draw functions using loops. We can no longer keep these functions as they are, because they currently allow no flexibility our generality regarding the size of the figure to be drawn. We want flexibility here, and this will take some work. [Note carefully, that we will be able to rewrite these functions with very little change in the program that calls them].

Helpful Assumptions and Hints:

A. We will insist that the height, h, be an integer greater than or equal to 3.
B. Ignoring leading blanks, the width each line you print will be an odd integer such as 5, 7, 9, etc.
C. Be careful in drawing the head. It’s maximum width is two less than the width of the body.
D. Among other things, you will find it handy to have a new function, drawNchars which, given an integer n and a character ch, will display a row of n characters, ch. For example, the call
    drawNchars ($, 11);      will display 11 $ as shown on the right:     $$$$$$$$$$$
You can also use drawNchars to display a number of spaces. E. You may assume that all program inputs are valid except for the height of the body. If it is not an integer greater than or equal to 3 you will have to terminate the program OR read in Chapter 5 how to force the program to give the user another chance to enter the value of h. Compute the maximum width of the body in the main program.
F. Note that the head consists of of an isosceles triangle (height h/2) on top of an inverted isosceles triangle (height h/2). If h is odd, the head also consists of a line in the middle..

Implementation:  Use your Lab03 solution as your “starter” program. You have to perform the following modifications on your Lab03 program:
1. Write new functions char selectHeadChar(void) and char selectDrawChar(void) to select the characters to draw the head and then the rest of the figure. (In the figures shown earlier, a $ was used to draw the head of the male and a % was used to draw the head of the female. An asterisk was used for the rest of both drawings.) Make sure these functions work before going on to step 2.
2. Write a new function int getHeight ( ) that asks the user for and returns the integer height, h, of the body of the figure. Remember that the value entered here must be an integer greater than or equal to 3.
3. Redefine the interfaces (function prototypes and comments) for your functions, drawMfig( ), drawFfig( ), drawCircle( ),  drawTriangle( ), drawIntersect( ), and drawBox() to include two input arguments: the height of the figure component to be drawn and the character to be used in the drawing. You may confine your validation of the value of the height entered by the user to the function getHeight and not worry about rechecking in these functions. The functions therefore may be written to return no value. When you are ready to modify the functions to draw the figure parts, I suggest you do them one at a time, doing the box and arms first, then the circle then the intersect and the triangle.
4.   Create a capture file showing tests like those in the sample output below.
5.   Email your submission to:  mail071x@lucas.cis.temple.edu where  x = section (1, 3, 4). Use a subject of Lab06 in the email.  Attach the following files:
-  a documentation file showing a structure chart, data table, and behavior diagram.
-  a source code file
-  a capture file