Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Help coding this program. Directions are on the attachments. It needs to show the same output as the following examples below. The code below was
Help coding this program. Directions are on the attachments. It needs to show the same output as the following examples below. The code below was given and needs to be changed so it will print the same output.
#include
#include
#include
//Global variables for loop indicies
int i, j;
//Get a shape from a user, and return the shape if it is valid;
// otherwise, print an error message and terminate the program
char getShapeFromUser(void);
//Get a length from a user, and return the length if it is valid;
// otherwise, print an error message and terminate the program
int getLengthFromUser(void);
//Get a height from a user, and return the height if it is valid;
// otherwise, print an error message and terminate the program
int getHeightFromUser(void);
bool isShapeValid(char shape);
bool isLengthValid(int length);
bool isHeightValid(int height);
void drawRectangle(int length, int height);
void drawTriangle(int length);
void drawHexagon(int length);
void drawOctagon(int length);
void drawPentagon(int length);
//Do not modify the main function
int main(void){
char shape = getShapeFromUser();
int length = getLengthFromUser();
int height;
if(shape == 'r'){
height = getHeightFromUser();
printf("Below is a %d by %d rectangle of * ", length, height);
drawRectangle(length, height);
}
else if(shape == 't'){
printf("Below is a triangle with two side lengths of %d * ", length);
drawTriangle(length);
}
else if(shape == 'h'){
printf("Below is a hexagon with side lengths of %d * ", length);
drawHexagon(length);
}
else if(shape == 'o'){
printf("Below is an octagon with side lengths of %d * ", length);
drawOctagon(length);
}
else if(shape == 'p'){
printf("Below is a pentagon with 4 side lengths of %d * ", length);
drawPentagon(length);
}
return 0;
}
//Implement function prototypes below
char getShapeFromUser(void){
char shape = '\0';
printf("Enter a shape: r t h o p ");
scanf("%c", &shape);
if( !isShapeValid(shape) ){
printf("Invalid shape Goodbye! ");
exit(0);
}
return shape;
}
int getLengthFromUser(void){
int length = 0;
printf("Enter a length ");
scanf("%d", &length);
if( !isLengthValid(length) ){
printf("Length must be greater than 1 Goodbye! ");
exit(0);
}
return length;
}
int getHeightFromUser(void){
int height = 0;
printf("Enter a height ");
scanf("%d", &height);
if( !isHeightValid(height) ){
printf("Height must be greater than 1 Goodbye! ");
exit(0);
}
return height;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started