Question

Description: The purpose of this assignment is to practice writing code that calls functions, and contains...

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:

  • nested for() loops
  • a while() or a do-while() loop
  • a switch() statement
  • an if-else statement
  • functions (see below)


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:

  • A function that prints the menu of choices for the user, prompts the user to enter a choice, and retrieves that choice. The return value of this function must be void. The function will have one pass-by-reference parameter of type char. On the function's return, the parameter will contain the user's menu choice.
  • A function that prompts the user to enter a single character. The return value of the function be a char and will return the character value entered by the user. This return value will be stored in a local variable, C, in main(). The initial default value of this character will be ' ' (blank or space character).
  • A function that prompts the user to enter a numerical value between 1 and 15 (inclusive). If the user enters a value outside this range, the user is prompted to re-enter a value until a proper value is entered. The return value of the function be an int and will return the value entered by the user. This return value will be stored in a local variable, N, in main(). The initial default value of this character will be 0.
  • A function for each geometric shape. Each function will take the previously entered integer value N and character value C as input parameters (You will need to ensure that these values are valid before entering these functions). The return values of these functions will be void. The functions will print the respective geometric shape of N lines containing the input character C. N is considered the height of the shape. For a line, it is just printing the character in C, N number of times, so that it creates a vertically standing line of length N. For N = 6 and C = '*', the draw line function should output:

    *
    *
    *
    *
    *
    *


    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:

    *
    **
    ***
    ****
    *****
    ******

Homework Answers

Answer #1

#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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
#include<iostream> #include<iomanip> using namespace std; int main() { //variables int choice; float radius,base,height,area; const double PI=3.14159;...
#include<iostream> #include<iomanip> using namespace std; int main() { //variables int choice; float radius,base,height,area; const double PI=3.14159; //repeat until user wants to quits while(true) { //menu cout<<endl<<endl<<"Geometry Calculator"<<endl<<endl; cout<<"1. Calculate the area of a circle"<<endl; cout<<"2. Calculate the area of a triangle"<<endl; cout<<"3. Quit"<<endl<<endl; //prompt for choice cout<<"Enter your choice(1-3): "; cin>>choice; cout<<endl; //if choice is circle if(choice==1) { cout<<"What is the radius of the circle? "; cin>>radius; //calculating area area=PI*radius*radius; cout<<endl<<"The area of the circle is "<<fixed<<setprecision(3)<<area<<endl; } //if choice...
Write the functions which are called in the main function given. YOU MAY ASSUME THAT THE...
Write the functions which are called in the main function given. YOU MAY ASSUME THAT THE USER ENTERS A VALID INTEGER. printDouble should accept one value and should print the value of that number times 2. printEndMessage should not accept any values and should print a message indicating that the program has finished. def main(): num1 = int(input("Please enter your number ")) printDouble(num1) printEndMessage() main()
Python code only! (Do not include breaks or continue functions) Write a program that asks the...
Python code only! (Do not include breaks or continue functions) Write a program that asks the user to enter the amount that they budgeted for the month. A loop should then prompt the user to enter their expenses, one at a time and to enter 0 to quit. When the loop finishes, the program should display the the amount of budget left. (a negative number indicates the user is over budget and a positive number indicates the user is under...
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
Create a Python main program which calls two functions enterNum and calcResult and accomplishes the following:...
Create a Python main program which calls two functions enterNum and calcResult and accomplishes the following: 1. The main program calls the function enterNum 3 times. The first time enterNum is called, the main program stores the returned output in the variable xx. The second time, the returned output is stored in the variable yy and the third time in zz. 2. enterNum asks the user to enter a floating point number. This function has no input arguments and returns...
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are...
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are pointers or constant pointers. Directions: Using the following parallel array and array of vectors: // may be declared outside the main function const int NUM_STUDENTS = 3; // may only be declared within the main function string students[NUM_STUDENTS] = {"Tom","Jane","Jo"}; vector <int> grades[NUM_STUDENTS] {{78,98,88,99,77},{62,99,94,85,93}, {73,82,88,85,78}}; Be sure to compile using g++ -std=c++11 helpWithGradesPtr.cpp Write a C++ program to run a menu-driven program with the...
In C programming language please. -Construct a program that will make use of user defined functions,...
In C programming language please. -Construct a program that will make use of user defined functions, the do/while loop, the for loop and selection. -Using a do/while loop, your program will ask/prompt the user to enter in a positive value representing the number of values they wish to have processed by the program or a value to quit/exit. -If the user enters in a 0 or negative number the program should exit with a message to the user indicating they...
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to...
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to N. The function calculateSum has one parameter N of type integer and returns an integer which represents the sum from -1 to N, inclusive. Write another function calculateAverage that calculates an average. This function will have two parameters: the sum and the number of items. It returns the average (of type float). The main function should be responsible for all inputs and outputs. Your...
In this assignment you will write a program that compares the relative strengths of two earthquakes,...
In this assignment you will write a program that compares the relative strengths of two earthquakes, given their magnitudes using the moment magnitude scale. Earthquakes The amount of energy released during an earthquake -- corresponding to the amount of shaking -- is measured using the "moment magnitude scale". We can compare the relative strength of two earthquakes given the magnitudes m1 and m2 using this formula: f=10^1.5(m1−m2) If m1>m2, the resulting value f tells us how many times stronger m1...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT