Question

Write a menu driven program to demonstrate the use of array and switch. It must be...

Write a menu driven program to demonstrate the use of array and switch.

It must be written in C language .

Here is the menu -

  • Press 1 to add a number to the array.
  • Press 2 to display the numbers entered in the array so far
  • Press 3 to find the average of the numbers entered.
  • Press 4 to exit.

Option 1 - The user should be able to enter 20 numbers only. If all twenty numbers are entered by the user and user selects choice 1, then a message should be displayed saying that the array is full.

Option 2 -The numbers saved in the array that are entered by the user should be displayed. (For example - If user enters one number and then selects menu option 2, then that one number needs to be displayed only. No zeros should be displayed for the rest of the numbers in the array)

Option 3 - Need to display average of all the numbers entered by the user. (hint- use a counter variable to keep track of how many numbers are entered by the user.)

Option 4 - Displays "Thank you" message.
Make sure to turn in the source code with comments and program output screen shots in a word document. No design tool required. Make sure to run various test cases so that I can be convinced that your program is working perfectly for all the menu options.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code in C below. I've commented it for you for better understanding. Refer to the output below the code and indentation below the output for better formatting of the code.

#include<stdio.h>

void main(){

    //initialising variables

    int arr[20];

    int count=0;

    printf("Press 1 to add a number to the array.\n");

    printf("Press 2 to display the numbers entered in the array so far\n");

    printf("Press 3 to find the average of the numbers entered.\n");

    printf("Press 4 to exit.\n");

    int choice;

    printf("Enter your choice: ");

    scanf("%d",&choice);

    //assuming valid choices are between 1 to 4

    while(choice>=1 && choice<=4){

        switch (choice)

        {

            //entering into array

            case 1:

                if(count<20){

                    //input a number and add it to array

                    printf("Enter a number: ");

                    int num;

                    scanf("%d",&num);  

                    arr[count++]=num;          

                    printf("Number added to array\n");

                }

                else{   //array full

                    printf("Array is full\n");

                }

                break;

            case 2:

            //printing the array using for loop

                printf("Array: ");

                for(int i=0;i<count;i++)

                    printf("%d ",arr[i]);

                printf("\n");

                break;

            case 3:

            //calculating average

                if(count==0){   //empty array

                    printf("Empty Array\n");

                    break;

                }

                printf("Average: ");

                float sum=0;

                for(int i=0;i<count;i++)

                    sum+=arr[i];

                printf("%f \n",sum/count);  //average=sum/count

                break;

            case 4:

            //exit

                printf("Thank you!!");

                return;

        }

        //entering choice in while loop

        printf("Enter your choice: ");

        scanf("%d",&choice);

    }

}

Output

For Indentation Purpose

Add a comment
Know the answer?
Add Answer to:
Write a menu driven program to demonstrate the use of array and switch. It must be...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a contacts database program that presents the user with a menu that allows the user...

    Write a contacts database program that presents the user with a menu that allows the user to select between the following options: (In Java) Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...

  • Write a C program Design a program that uses an array to store 10 randomly generated...

    Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...

  • Create a menu-driven program (using the switch) that finds and displays areas of 3 different objects....

    Create a menu-driven program (using the switch) that finds and displays areas of 3 different objects. The menu should have the following 4 choices: 1 -- rectangle 2 -- circle 3 -- triangle 4 -- quit If the user selects choice 1, the program should find the area of a rectangle. rectangle area = length * width If the user selects choice 2, the program should find the area of a circle. circle area = PI * radius * radius...

  • Write a program that displays a menu on the screen. The menu will give the user...

    Write a program that displays a menu on the screen. The menu will give the user four options - enter two integers, enter two decimal numbers (#.#), enter one integer and one decimal number, or quit. The inputs should be labelled a, b, c, or d, and you should enter in the letter to choose the appropriate option. Once the user selects the option, two numbers will be read in from the user. The numbers will be added together and...

  • Develop a flowchart and then write a menu-driven C++ program that uses several FUNCTIONS to solve...

    Develop a flowchart and then write a menu-driven C++ program that uses several FUNCTIONS to solve the following program. -Use Microsoft Visual C++ .NET 2010 Professional compiler using default compiler settings. -Use Microsoft Visio 2013 for developing your flowchart. -Adherence to the ANSI C++  required -Do not use <stdio.h> and <conio.h>. -Do not use any #define in your program. -No goto statements allowed. Upon execution of the program, the program displays a menu as shown below and the user is prompted to make a selection from the menu....

  • extra credit 1 Write a program that will display the following menu and prompt the user...

    extra credit 1 Write a program that will display the following menu and prompt the user for a selection: A) Add two numbers B) Subtract two numbers C) Multiply two numbers D) Divide two numbers X) Exit program The program should: display a hello message before presenting the menu accept both lower-case and upper-case selections for each of the menu choices display an error message if an invalid selection was entered (e.g. user enters E) prompt user for two numbers...

  • In Small Basic Write a program that asks the user how many numbers s/he wishes to...

    In Small Basic Write a program that asks the user how many numbers s/he wishes to enter into an array and then enters a loop and proceeds to get those numbers from the user and enter them into the array. Once the array has been filled with the numbers, your program then asks the user to make a choice indicating what s/he wishes to do with the numbers in the array. The choices are: TextWindow.WriteLine("Enter 1 to compute and display...

  • Menu-driven programs will display the menu options to the user and then prompt them for a...

    Menu-driven programs will display the menu options to the user and then prompt them for a menu choice. This program will display the following menu in the following format: Calculator Options: Calculate the area of a circle Calculate the area of a rectangle Calculate the area of a triangle Calculate the area of a trapezoid Calculate the area of a sphere Exit Enter your choice (1-6) Once the user enters a choice for the menu, the program should use a...

  • Write a program that keeps track of a speakers’ bureau. The program should use a structure...

    Write a program that keeps track of a speakers’ bureau. The program should use a structure to store the following data about a speaker: (this is in C++) Name Telephone Number Speaking Topic Fee Required The program should use a vector of structures. It should let the user enter data into the vector, change the contents of any element, and display all the data stored in the vector. The program should have a menu-driven user interface. Input Validation: When the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT