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.
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 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 triangle which looks like the
following:
*
**
***
****
*****
******
Suggested steps:
CODE IN C:
#include <stdio.h>
void line(char ch,int n){
int i;
for(i=1;i<=n;i++){
printf("\n%c",ch);
}
printf("\n");
}
void triangle(char ch,int n){
int i,j;
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
printf("%c",ch);
}
printf("\n");
}
}
void square(char ch,int n){
int i,j;
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
printf("%c",ch);
}
printf("\n");
}
}
void rectangle(char ch,int n){
int i,j;
for(i=1;i<=n;i++){
for(j=1;j<=n+5;j++){
printf("%c",ch);
}
printf("\n");
}
}
int main()
{
int n=6;
char ch = '*';
char choice;
do{
printf("\nEnterChange Character \'C\' or \'c\'");
printf("\nEnter Change Number \'N\' or \'n\'");
printf("\nDraw Line \'L\' or \'l\'");
printf("\nDraw Square \'S\' or \'s\'");
printf("\nDraw Rectangle \'R\' or \'r\'");
printf("\nDraw Triangle (Left Justified) \'T\' or \'t\'");
printf("\nQuit Program \'Q\' or \'q\'");
printf("\n\nEnter your choice:");
scanf("%c",&choice);
if(choice=='c'||choice=='C'){
printf("Enter the character:");
scanf("%c",&ch);
}
else if(choice=='N'||choice=='n'){
printf("Enter the number:");
scanf("%d",&n);
}
else if(choice=='L'||choice=='l'){
line(ch,n);
}
else if(choice=='S'||choice=='s'){
square(ch,n);
}
else if(choice=='r'||choice=='R'){
rectangle(ch,n);
}
else if(choice=='t'||choice=='T'){
triangle(ch,n);
}
else if(choice=='q'||choice=='Q'){
printf("Thank you....");
break;
}
else{
printf("Invalid input...");
}
scanf("%c",&choice);
}while(choice!='q' || choice!='Q');
return 0;
}
OUTPUT: