NAME: ______

Lab Activities – Lab 4

Due October 15, 2002

Print out the lab and complete Exercises 4.1 through 4.4. Staple together these sheets with your complete answers. Exercises 4.3 and 4.4 BOTH require a printout of your source code and screen shots. MAKE SURE YOU DO THEM CAREFULLY. Submit printouts of all exercises, and submit your source code (.cpp file) for Exercise 4.4 in a 9x12 manila envelope.

PROBLEM DEFINITION

We want to create a program that draws a diamond or a triangle with a size that the user selects using a specific character that will be entered at the keyboard. Here are two examples:

A triangle of size 4, using *:

*
***
*****
*******

A diamond of size 4, using *:

*
***
*****
*******
*****
***
*

The user inputs:
1) a choice to draw one of the two shapes or to quit the program,
2) a character choice which will be used for drawing the selected shape, and
3) the size of the shape
4) the desire to quit

Output:
One of these will be the output:
1) a triangle,
2) a diamond,
3) quit

Exercise 4.1 Analysis of the problem
Break down the tasks that this program is supposed to complete. Give more detail for each, to form a complete algorithm in English with some code. The main tasks are given.

1) ask for the input:

2) for both triangle and diamond

2a) display a triangle of requested size:

2b) if the choice is a diamond, add the bottom part:

3) Ask the user if he wants to draw again.

Exercise 4.2. Using Top-Down Design

Assuming you have all of the following functions that solve part of the problem, complete the diagram in the following page by filling the boxes with the names of the functions in the appropriate boxes.Show, by numbering the boxes,the flow of your program starting from the main() function if the user chooses to draw a diamond.

The diamond in the diagram is a decision point in your program. Write there the conditioin that would determine which way to go.

The arrows show which data is passed and in which direction (to or from a function).

In other words you will show how the main problem (drawing a triangle or diamond according to the user’s choices) is solved using solutions to sub-problems.

1) void instructions( );
//Describes the program and how it works

2) int menu( );
// Displays a menu of choices, validates the user’s input so only a valid
// choice is accepted, and returns either 1) draw triangle, 2) draw diamond, and 3) quit

4) int get_size( );
// Returns the size of the shape (in lines), same function for either of the shapes

5) char get_char( );
//Asks users to select a character that will be used to draw the shape

6) void draw_triangle(int size, char c);
//Draws a triangle of size size using character c

7) void draw_diamond(int size, char c);
// First calls draw_triangle, then add_bottom to draw the diamond

8) void draw_bottom(int size, char c);
// Draws an upside down triangle of size size as the bottom of the diamond

Exercise 4.3. Structured Development, Testing and Debugging

Each function in a program should be designed, coded, and tested as a separate unit from the rest of the program. To test a function, we use a driver program. To test a function that calls another function that has not been written, we use a simplified version of the function that is not written yet. These functions are referred to as stubs.

For this exercise, copy the following driver and stubs, which are provided so you can develop and test a function called draw_triangle, declared as follows:

void draw_triangle(int size, char c);
//Draws a triangle of size size using character c

Complete the development of the function draw_triangle. Save your program as “shapes_test.cpp”. Compile and run it. Turn in a printout of shapes_test.cpp, and a screen shot of its output. THIS IS NOT THE COMPLETE LAB. GO ON TO EXERCISE 4.4

#include<iostream>
using namespace std;

void draw_triangle(int size, char c);
void draw_shape(int size, char c);

int main( )
{
// Simplified version of main, notice that choice is fixed for now
int choice = 1;

draw_shape(choice);

return 0;
}

void draw_shape(int choice)
{
//A simplified version of draw_shape notice that char and size are fixed for now
cout < “Testing draw_triangle, with size 4 and char *”;
char c = '*';
int size = 4;
draw_triangle(size, c);
}

void draw_triangle(int size, char c)
{
// Completely develop the draw_triangle function here.

}

Exercise 4.4 Full Development

Using ALL the function declarations given in Exercise 4.2, and the following main function, complete the code needed in the functions to have a full working program.Save your file as “shapes.cpp”, compile and run it. Make a screen shot of a triangle and diamond of size 5 and attach to this lab. NOTE: make sure you complete Exercise 4.3 before this one.

int main ( )
{
int choice;

instructions( );
choice = menu( );
if(choice != 1 || choice != 2)
{
cout < "You requested to quit, bye \n";
return 0;
}

draw_shape(choice);

return 0;

}