Description:
The purpose of this assignment is to practice writing code that
calls functions, and contains loops and branches. You will 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.
General Comments:
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:
*
**
***
****
*****
******
#include <stdio.h>
void print_menu(char *c)//to print the menu
{
printf("Menu Choice Valid User Input Choices\n");
printf("Enter Change Character 'C' or 'c'\n");
printf("EnterChange 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("\nEnter Your Choice:\n");
scanf("%c",c);
}
void l(char ch,int n)//to print line
{
for(int i=0;i<n;i++)
printf("%c\n",ch);
}
void s(char ch,int n)//to print square
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
printf("%c",ch);
printf("\n");
}
}
void r(char ch,int n)//to print rectangle
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n+5;j++)
printf("%c",ch);
printf("\n");
}
}
void t(char ch,int n)//to print triangle
{
for(int i=0;i<n;i++)
{
for(int j=0;j<=i;j++)
printf("%c",ch);
printf("\n");
}
}
int main(void) {
// your code goes here
while(1)
{
char c;
print_menu(&c);
char ch;//these are the initial default value
int n;
//using if else block to achive this
if(c=='c' || c=='C')
{
printf("\nEnter Character:\n");
scanf("%c",&ch);
}
else if(c=='N' || c=='n')
{
printf("Enter Number:\n");
scanf("%d",&n);
}
else if(c=='L' || c=='l')
l(ch,n);
else if(c=='s' || c=='S')
s(ch,n);
else if(c=='r' || c=='R')
r(ch,n);
else if(c=='t' || c=='T')
t(ch,n);
else if(c=='q' || c=='Q')
break;
else
printf("Invalid Choice\n");
}
return 0;
}
Sample output:
Menu Choice Valid User Input Choices
Enter Change Character 'C' or 'c'
EnterChange 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'
Enter Your Choice:
Enter Number:3
Menu Choice Valid User Input Choices
Enter Change Character 'C' or 'c'
EnterChange 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'
Enter Your Choice:c
Enter Character:*
Menu Choice Valid User Input Choices
Enter Change Character 'C' or 'c'
EnterChange 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'
Enter Your Choice:l
*
*
*
Menu Choice Valid User Input Choices
Enter Change Character 'C' or 'c'
EnterChange 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'
Enter Your Choice:s
***
***
***
Menu Choice Valid User Input Choices
Enter Change Character 'C' or 'c'
EnterChange 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'
Enter Your Choice:r
********
********
********
Menu Choice Valid User Input Choices
Enter Change Character 'C' or 'c'
EnterChange 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'
Enter Your Choice:t
*
**
***
Menu Choice Valid User Input Choices
Enter Change Character 'C' or 'c'
EnterChange 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'
Enter Your Choice:a
Invalid Choice
Menu Choice Valid User Input Choices
Enter Change Character 'C' or 'c'
EnterChange 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'
Enter Your Choice:q
please refer the screen shots for more info:
Get Answers For Free
Most questions answered within 1 hours.