Question

In: Computer Science

C Program: Create a C program that prints a menu and takes user choices as input....

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:

  • 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 triangle which looks like the following:

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

Suggested steps:

  1. Create a source file with only your main() function and the standard #include files. Compile and run (it won't do anything, but if you get compile errors, it is best to fix them immediately).
  2. Write a function, called from main(), that prints the menu. Compile and test it to ensure it works properly.
  3. Add a pass-by-reference parameter to your menu() function that will retrieve the character input within the function and make it available for use within the main function. The menu function must remain a void function and not return a value.
  4. Add code to the menu() function to prompt and retrieve user input (allow any character), and assign that input character to the pass-by-reference parameter). Compile and test your code.
  5. Enclose the print menu function and user input code within a loop that will exit the program when the Quit program choice is entered. Test the logic of your code to ensure that the loop only exits on proper input ('q' or 'Q').
  6. Create five "stub functions" for the six other (non-Quit) choices. Put a print statement such as "This is function EnterChar()" or some other informative statement in the body of the function. For functions that return a value, return a specific character 'X' or number. This will be changed when the function is filled in.
  7. Within the loop in main(), create the logic to call each function based on input from the menu choice (and handle incorrect input choices). Test this logic by observing the output of the stub function statements.
  8. Fill in the logic and code for each function. Note that the Line drawing function is probably a little easier (logically) to write than the Right Justified Printing function, so you may want to write it first. Once you have the Line drawing function complete, think about what additional character(s) you will have to print (and how many) to make a square shape. Step by step, you can write functions for other shapes as well. This is the part of the lab (and the course) where you develop your skills to create algorithms that solve specific problems. These kinds of skills are not specific to C or any other language, but how to implement these algorithms are language specific.
  9. Test your program thoroughly.

Solutions

Expert Solution

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:


Related Solutions

Write a program in C that takes as input an 8-bit binary number and prints the...
Write a program in C that takes as input an 8-bit binary number and prints the next 10 binary numbers. Define a binary number as int binNum[8]; Use binNum[0] to store the most significant (i.e., leftmost) bit and binNum[7] to store the least significant bit. Ask the user to input the first binary number with each bit separated by at least one space.
Write a program in C that takes as input a four-digit hexadecimal number and prints the...
Write a program in C that takes as input a four-digit hexadecimal number and prints the next 10 hexadecimal numbers. Define a hexadecimal number as int hexNum[4] Allow upper- or lowercase letters for input and use uppercase letters for the hexadecimal output. For example, 3C6f should be valid input and should produce output 3C6F, 3C70, 3C71, . . . .
Create the logic for a rhyming program that takes in five words from a user input...
Create the logic for a rhyming program that takes in five words from a user input and replaces the selected rhyming words with the user's input, then creates and displays the Humpty Dumpty rhyme with the five words from the user input. Hint: 1. You will use the sentinel values: start and stop 2. The program will ask the user for 5 words. 3. The program will store the 5 words 4. The program outputs the rhyme replacing the ____...
Write a complete C++ program that prompts the user for and takes as input, numbers until...
Write a complete C++ program that prompts the user for and takes as input, numbers until the user types in a negative number. the program should add all of the numbers together. Then if the result is less than 20 the program should multiply the result by 3, otherwise subtract 2 from the result. Finally, the program should printout the result.
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream>, switch and if-else statements only and do not use string storing for the conversion in words. Thank you.** **Our class is still discussing on the basics of programming. Please focus only on the basics. Thank you.** Example outputs: Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four...
Using C++ Create a program that asks the user to input a string value and then...
Using C++ Create a program that asks the user to input a string value and then outputs the string in the Pig Latin form. - If the string begins with a vowel, add the string "-way" at the end of the string. For “eye”, it will be “eye-way”. - If the string does not begin with a vowel, first add "-" at the end of the string. Then rotate the string one character at a time; that is, move the...
Write a program names EncryptDecrypt.java that has the following menu choices: Print menu and allow user...
Write a program names EncryptDecrypt.java that has the following menu choices: Print menu and allow user to choose options. The program must have a file dialogue box for text file. Output should be based on user choices. Read in a file Print the file to the console Encrypt the file and write it to the console Write out the encrypted file to a text file Clear the data in memory Read in an encrypted file Decrypt the file Write out...
OBJECTIVE C 2) // Create a small app that takes user input and performs a useful...
OBJECTIVE C 2) // Create a small app that takes user input and performs a useful calculation with that user input // - make sure that absolutely no input crashes the app. // - Use layout constraints to make sure the app looks good in portraint and landscape mode, as well as on small and large devices.
Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
IN C++ PLEASE Requirements Write a program that takes in user input of two integer numbers...
IN C++ PLEASE Requirements Write a program that takes in user input of two integer numbers for height and width and uses a nested for loop to make a rectangle out of asterixes. The creation of the rectangle (i.e. the nested for loop) should occur in a void function that takes in 2 parameters, one for height and one for width. Make sure your couts match the sample output (copy and paste from those couts so you don't make a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT