In: Computer Science
C Program: Create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. The specifications must be followed exactly, or else the input given in the script file may not match with the expected output.
Your code must contain at least one of all of the following control types:
Important! Consider which control structures will work best for
which aspect of the assignment. For example, which would be the
best to use for a menu?
The first thing your program will do is print a menu of choices for
the user. You may choose your own version of the wording or order
of choices presented, but each choice given in the menu must match
the following:
Menu Choice | Valid User Input Choices |
Enter/Change Character | 'C' or 'c' |
Enter/Change Number | 'N' or 'n' |
Draw Line | 'L' or 'l' |
Draw Square | 'S' or 's' |
Draw Rectangle | 'R' or 'r' |
Draw Triangle (Left Justified) | 'T' or 't' |
Quit Program | 'Q' or 'q' |
A prompt is presented to the user to enter a choice from the menu.
If the user enters a choice that is not a valid input, a message
stating the choice is invalid is displayed and the menu is
displayed again.
Your executable file will be named
Lab3_<username>_<labsection>
Your program must have at least five functions (not including main()) including:
*
*
*
*
*
*
If a square is to be printed, then the following
output is expected:
******
******
******
******
******
******
In case of a rectangle, we assume its width is
N+5. It should look like the following:
***********
***********
***********
***********
***********
***********
If the user selects Triangle, then it should
print a left justified triange which looks like the
following:
*
**
***
****
*****
******
Suggested Steps to Complete the Assignment:
You are not required to complete the following steps to write your
program or even pay attention to them. However, these steps will
give you an idea on how to create C programs that are stable and
need less debugging. If you do decide to use the suggested steps,
you should test your program thoroughly to ensure each step works
correctly before moving on to the next step.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#include<stdio.h>
//method to display user a menu, read choice and update the reference parameter
void menu(char* choice){
printf("\nEnter/Change Character ('C' or 'c')\n");
printf("Enter/Change Number ('N' or 'n')\n");
printf("Draw Line ('L' or 'l')\n");
printf("Draw Square ('S' or 's')\n");
printf("Draw Rectangle ('R' or 'r')\n");
printf("Draw Triangle (Left Justified) ('T' or 't')\n");
printf("Quit Program ('Q' or 'q')\n");
printf("Your choice: ");
//reading choice, updating parameter
scanf(" %c",choice);
}
//method to read a character and return it
char inputCharacter(){
printf("Enter a single character: ");
char c;
//reading and returning a character
scanf(" %c", &c);
return c;
}
//method to read and return a number between 1 and 15
int inputNumber(){
//initializing num to -1
int num=-1;
//looping until num is valid
while(num<1 || num>15){
//asking, and reading number
printf("Enter a number between 1 and 15: ");
scanf("%d",&num);
//validating
if(num<1 || num>15){
//invalid
printf("Invalid input, try again!\n");
}
}
//returning valid number
return num;
}
//method to draw a line
void drawLine(int N, char c){
//looping for N times, printing c in each line
for(int i=0;i<N;i++){
printf("%c\n",c);
}
}
//method to draw a square
void drawSquare(int N, char c){
//looping for N times
for(int i=0;i<N;i++){
//looping for N times, printing c
for(int j=0;j<N;j++){
printf("%c",c);
}
//line break
printf("\n");
}
}
//method to draw a rectangle
void drawRectangle(int N, char c){
//looping for N times
for(int i=0;i<N;i++){
//looping for N+5 times
for(int j=0;j<N+5;j++){
//printing c
printf("%c",c);
}
//line break
printf("\n");
}
}
//method to draw a triangle
void drawTriangle(int N, char c){
//looping for N times
for(int i=1;i<=N;i++){
//looping for i times
for(int j=0;j<i;j++){
//printing c
printf("%c",c);
}
//line break
printf("\n");
}
}
//main method
int main(){
//initializing variables
char choice=' ';
char c=' ';
int N=0;
//looping until user enter 'q' or 'Q'
while(choice!='Q' && choice!='q'){
//displaying menu, getting choice
menu(&choice);
//line break
printf("\n");
//switching choice
switch(choice){
//if we put another a case below another case without break statement,
//when the first case is satisfied, it will execute the second also
//we use this technique to execute some instructions regardless of the
//case of letter. for example, in below code, the inputCharacter() method
//will be called if the value of choice is 'C' or 'c'
case 'C':
case 'c':
c=inputCharacter();
break;
//similarly enabling other choices too
case 'N':
case 'n':
N=inputNumber();
break;
case 'L':
case 'l':
drawLine(N,c);
break;
case 'S':
case 's':
drawSquare(N,c);
break;
case 'R':
case 'r':
drawRectangle(N,c);
break;
case 'T':
case 't':
drawTriangle(N,c);
break;
case 'Q':
case 'q':
//printing a goodbye message
printf("Bye!\n");
break;
default:
//invalid choice
printf("Invalid choice, try again!\n");
}
}
return 0;
}
/*OUTPUT*/
Enter/Change Character ('C' or 'c')
Enter/Change Number ('N' or 'n')
Draw Line ('L' or 'l')
Draw Square ('S' or 's')
Draw Rectangle ('R' or 'r')
Draw Triangle (Left Justified) ('T' or 't')
Quit Program ('Q' or 'q')
Your choice: c
Enter a single character: *
Enter/Change Character ('C' or 'c')
Enter/Change Number ('N' or 'n')
Draw Line ('L' or 'l')
Draw Square ('S' or 's')
Draw Rectangle ('R' or 'r')
Draw Triangle (Left Justified) ('T' or 't')
Quit Program ('Q' or 'q')
Your choice: N
Enter a number between 1 and 15: 18
Invalid input, try again!
Enter a number between 1 and 15: 6
Enter/Change Character ('C' or 'c')
Enter/Change Number ('N' or 'n')
Draw Line ('L' or 'l')
Draw Square ('S' or 's')
Draw Rectangle ('R' or 'r')
Draw Triangle (Left Justified) ('T' or 't')
Quit Program ('Q' or 'q')
Your choice: l
*
*
*
*
*
*
Enter/Change Character ('C' or 'c')
Enter/Change Number ('N' or 'n')
Draw Line ('L' or 'l')
Draw Square ('S' or 's')
Draw Rectangle ('R' or 'r')
Draw Triangle (Left Justified) ('T' or 't')
Quit Program ('Q' or 'q')
Your choice: s
******
******
******
******
******
******
Enter/Change Character ('C' or 'c')
Enter/Change Number ('N' or 'n')
Draw Line ('L' or 'l')
Draw Square ('S' or 's')
Draw Rectangle ('R' or 'r')
Draw Triangle (Left Justified) ('T' or 't')
Quit Program ('Q' or 'q')
Your choice: r
***********
***********
***********
***********
***********
***********
Enter/Change Character ('C' or 'c')
Enter/Change Number ('N' or 'n')
Draw Line ('L' or 'l')
Draw Square ('S' or 's')
Draw Rectangle ('R' or 'r')
Draw Triangle (Left Justified) ('T' or 't')
Quit Program ('Q' or 'q')
Your choice: t
*
**
***
****
*****
******
Enter/Change Character ('C' or 'c')
Enter/Change Number ('N' or 'n')
Draw Line ('L' or 'l')
Draw Square ('S' or 's')
Draw Rectangle ('R' or 'r')
Draw Triangle (Left Justified) ('T' or 't')
Quit Program ('Q' or 'q')
Your choice: c
Enter a single character: o
Enter/Change Character ('C' or 'c')
Enter/Change Number ('N' or 'n')
Draw Line ('L' or 'l')
Draw Square ('S' or 's')
Draw Rectangle ('R' or 'r')
Draw Triangle (Left Justified) ('T' or 't')
Quit Program ('Q' or 'q')
Your choice: t
o
oo
ooo
oooo
ooooo
oooooo
Enter/Change Character ('C' or 'c')
Enter/Change Number ('N' or 'n')
Draw Line ('L' or 'l')
Draw Square ('S' or 's')
Draw Rectangle ('R' or 'r')
Draw Triangle (Left Justified) ('T' or 't')
Quit Program ('Q' or 'q')
Your choice: x
Invalid choice, try again!
Enter/Change Character ('C' or 'c')
Enter/Change Number ('N' or 'n')
Draw Line ('L' or 'l')
Draw Square ('S' or 's')
Draw Rectangle ('R' or 'r')
Draw Triangle (Left Justified) ('T' or 't')
Quit Program ('Q' or 'q')
Your choice: q
Bye!