Problem Definition: Inputs
Problem Definition: Inputs
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:
*
***
*****
*******
*
***
*****
*******
*****
***
*
Output:
One of these will be the output:
1) a triangle,
2) a diamond,
3) quit
To write this program, we will assume that we have the following functions:
2) int menu( )
// This function will return a choice to the main ; 1) draw triangle, 2) draw
diamond, and 3) quit
Now that we have defined the functions, it is time to design the algorithm for the
program. However, we can write the main part of the program.
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;
}
Algorithm Design
The following diagram (structure chart) summarizes the algorithm design for this
program. The direction of each arrow is the same as the direction in which the data
flow.
Suppose we want to test the draw triangle function. This function is called within the
draw_shape function. There are two arguments to this function, the size and the
character c. To test the program we can use the driver shown below:
#include<iostream>
using namespace std;
return 0;
}
Complete the function draw_triangle in the above program, then use the driver and
stubs given above to draw a triangle. Repeat a similar thing for the draw_diamond
function.
Answer=
#include <iostream>
#include <cstdlib>
using namespace std;
void instruction()
{
cout<<"\n ****************** Instructions ****************** ";
cout<<"\n Select the shape you want.";
cout<<"\n Enter the size of the shape.";
cout<<"\n Select choice 3 to stop.";
}// End of function
int menu()
{
int ch;
cout<<"\n\n ****************** Menu ****************** ";
cout<<"\n 1. Draw Triangle. ";
cout<<"\n 2. Draw Diamond. ";
cout<<"\n 3. Quit. ";
}// End
// Function to display bottom part of the diamond based on the size and symbol(c) passed as
parameter
void draw_bottom(int size, char c)
{
// For number of times the symbol to display
int times = (size * 2) - 3;
Sample Output:
Invalid choice!