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.

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:

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

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.

  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

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!


Related Solutions

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?...
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...
Create a C++ program that will prompt the user to input an positive integer number and...
Create a C++ program that will prompt the user to input an positive integer number and output the corresponding number to words. Check all possible invalid input data. (Please use only switch or if-else statements. Thank you.)
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT