#include <stdio.h> #include <stdlib.h> #include <stdbool.h> //Global variables f
ID: 3803295 • Letter: #
Question
#include <stdio.h> #include <stdlib.h> #include <stdbool.h>//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 = ''; 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; }
#include <stdio.h> #include <stdlib.h> #include <stdbool.h>
//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 = ''; 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; }
...oo AT&T; 1:57 PM 91% K Project 3 proj 3.pdf Examples Your program should work correctly on the examples below, its source code should be compiled with the command aforementioned to produce an executable called proj3-out and it should draw various shapes comectly when the inputs are valid and output the messages shown in the examples below when it detects an invalid input. All input and output should be formatted as shown when nun via the line on our Unix (note: new each example not part of your program'svo.it in this dacument to show the difference between examples Each example is run working Some examples invalid inputs, and some examples include Unix commands that provide alternative ways to input data into your program. /proj3 out Enter a shape: rth op Enter a length Enter a height Below is a 3 by 4 rectangle of Page 3 Vproj3.out Enter a shape: rt h op Enter a length Below is a triangle with two side lengths of 6 /proj out Enter a shape: rth op Enter a length Below is a with side lengths of 10 To Do Notifications Messages Calendar
Explanation / Answer
Function codes:
// Code for rectangle function
void drawRectangle(int length, int height)
{
for(i = 0; i < length; i++){
for(j = 0; j < height; j++){
printf("*");
}
printf(" ");
}
//code for Triangle function
void drawTriangle(int length)
{
for(int i=1; i<=lenght; ++i, int k=0)
{
for(int j=1; j<=rows-i; +j)
{
printf(" ");
}
while(k != 2*i-1)
{
printf("* ");
++k;
}
printf(" ");
}
//code for Hexagon
void drawHexagon(int length)
{
Char *str=”****************”
for(int i=0;i<length;i++){
printf("%*.*s ",length+i, 2*i+1, str);
}
for(int j= length; j >= 1; j--) {
for(int s = 0; s <= length-j; s++) {
printf(" ");
}
int star = 0;
while(star != (2*j- 1)) {
printf("*");
star++;
}
printf(" ");
}
//code for octagon function
void drawOctagon(int length)
{
Char *str=”****************”
for(int i=0;i<length;i++){
printf("%*.*s ",length+i, 2*i+1, str);
}
for(int m = 0; m < length; m++){
for(int n = 0; n < length; n++){
printf("*");
}
printf(" ");
}
for(int j= length; j >= 1; j--) {
for(int s = 0; s <= length-j; s++) {
printf(" ");
}
int star = 0;
while(star != (2*j- 1)) {
printf("*");
star++;
}
}
Function codes:
// Code for rectangle function
void drawRectangle(int length, int height)
{
for(i = 0; i < length; i++){
for(j = 0; j < height; j++){
printf("*");
}
printf(" ");
}
//code for Triangle function
void drawTriangle(int length)
{
for(int i=1; i<=lenght; ++i, int k=0)
{
for(int j=1; j<=rows-i; +j)
{
printf(" ");
}
while(k != 2*i-1)
{
printf("* ");
++k;
}
printf(" ");
}
//code for Hexagon
void drawHexagon(int length)
{
Char *str=”****************”
for(int i=0;i<length;i++){
printf("%*.*s ",length+i, 2*i+1, str);
}
for(int j= length; j >= 1; j--) {
for(int s = 0; s <= length-j; s++) {
printf(" ");
}
int star = 0;
while(star != (2*j- 1)) {
printf("*");
star++;
}
printf(" ");
}
//code for octagon function
void drawOctagon(int length)
{
Char *str=”****************”
for(int i=0;i<length;i++){
printf("%*.*s ",length+i, 2*i+1, str);
}
for(int m = 0; m < length; m++){
for(int n = 0; n < length; n++){
printf("*");
}
printf(" ");
}
for(int j= length; j >= 1; j--) {
for(int s = 0; s <= length-j; s++) {
printf(" ");
}
int star = 0;
while(star != (2*j- 1)) {
printf("*");
star++;
}
}
Function codes:
// Code for rectangle function
void drawRectangle(int length, int height)
{
for(i = 0; i < length; i++){
for(j = 0; j < height; j++){
printf("*");
}
printf(" ");
}
//code for Triangle function
void drawTriangle(int length)
{
for(int i=1; i<=lenght; ++i, int k=0)
{
for(int j=1; j<=rows-i; +j)
{
printf(" ");
}
while(k != 2*i-1)
{
printf("* ");
++k;
}
printf(" ");
}
//code for Hexagon
void drawHexagon(int length)
{
Char *str=”****************”
for(int i=0;i<length;i++){
printf("%*.*s ",length+i, 2*i+1, str);
}
for(int j= length; j >= 1; j--) {
for(int s = 0; s <= length-j; s++) {
printf(" ");
}
int star = 0;
while(star != (2*j- 1)) {
printf("*");
star++;
}
printf(" ");
}
//code for octagon function
void drawOctagon(int length)
{
Char *str=”****************”
for(int i=0;i<length;i++){
printf("%*.*s ",length+i, 2*i+1, str);
}
for(int m = 0; m < length; m++){
for(int n = 0; n < length; n++){
printf("*");
}
printf(" ");
}
for(int j= length; j >= 1; j--) {
for(int s = 0; s <= length-j; s++) {
printf(" ");
}
int star = 0;
while(star != (2*j- 1)) {
printf("*");
star++;
}
Function codes:
// Code for rectangle function
void drawRectangle(int length, int height)
{
for(i = 0; i < length; i++){
for(j = 0; j < height; j++){
printf("*");
}
printf(" ");
}
//code for Triangle function
void drawTriangle(int length)
{
for(int i=1; i<=lenght; ++i, int k=0)
{
for(int j=1; j<=rows-i; +j)
{
printf(" ");
}
while(k != 2*i-1)
{
printf("* ");
++k;
}
printf(" ");
}
//code for Hexagon
void drawHexagon(int length)
{
Char *str=”****************”
for(int i=0;i<length;i++){
printf("%*.*s ",length+i, 2*i+1, str);
}
for(int j= length; j >= 1; j--) {
for(int s = 0; s <= length-j; s++) {
printf(" ");
}
int star = 0;
while(star != (2*j- 1)) {
printf("*");
star++;
}
printf(" ");
}
//code for octagon function
void drawOctagon(int length)
{
Char *str=”****************”
for(int i=0;i<length;i++){
printf("%*.*s ",length+i, 2*i+1, str);
}
for(int m = 0; m < length; m++){
for(int n = 0; n < length; n++){
printf("*");
}
printf(" ");
}
for(int j= length; j >= 1; j--) {
for(int s = 0; s <= length-j; s++) {
printf(" ");
}
int star = 0;
while(star != (2*j- 1)) {
printf("*");
star++;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.