Question

E) 120 pts.] Write another function int main ) in C++(and comment the main in Q1.D) that displays the following menu using a do while loop: please choose from the following Menu: I: Populate array with 5 elements 2: Print Array 3: Reverse Array 0: Exit Based, on the user choice, you should call the appropriate function. For example, if the user: - chooses 1, then in function main you should call function populateArray (A, 5); - chooses 2, then in function main you should call function printArray (A, 5); - chooses 3, then function man should call function reverseArray(A, 5);

dont use switch

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

Code :

#include <iostream>

using namespace std;

void printMenu()
{
    cout << "\n\nplease choose from the following Menu :\n";
    cout << "1. Populate array with 5 elements.\n";
    cout << "2. Print Array.\n";
    cout << "3. Reverse Array.\n";
    cout << "0. Exit\n\n";
    cout << "Enter choice : ";

}

void Swap(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

void populateArray(int * A, int n)
{
    cout << "\nEnter the values of the array : ";
    for(int i = 0; i < n; i ++)
    {
        cin >> A[i];
    }
}

void printArray(int *A, int n)
{
    for(int i = 0; i < n; i++)
    {
        cout << A[i] << " ";
    }
    cout << "\n";
}

void reverseArray(int *A, int n)
{
    for(int i = 0; i < n/2; i++)
    {
        Swap(&A[i], &A[n-1-i]);
    }
}

int main()
{
    int choice;
    int *A = (int *)malloc(5*sizeof(int));
    do
    {
        printMenu();
        cin >> choice;
        if(choice == 1)
        {
            populateArray(A, 5);
        }
        else if(choice == 2)
        {
            printArray(A, 5);
        }
        else if(choice == 3)
        {
            reverseArray(A, 5);
        }
    }while(choice != 0);
    return 0;
}

Output :

please choose from the following Menu 1. Populate array with 5 elements. 2. Print Array. 3. Reverse Array. 0. Exit Enter choice : 1 Enter the values of the array 1 2 3 4 5 please choose from the following Menu 1. Populate array with 5 elements. 2. Print Array. 3. Reverse Array. 0. Exit Enter choice : 2 1 2 34 5 please choose from the following Menu 1. Populate array with 5 elements. 2. Print Array. 3. Reverse Array. 0. Exit Enter choice : 3 please choose from the following Menu 1. Populate array with 5 elements. 2. Print Array. 3. Reverse Array. 0. Exit Enter choice : 2 5 4 3 2 1

******************NOTE******************

If there is any problem with the code.. or If you have some doubts... please ask in the comments. :)
If everything is good, please rate positively ... as it directly affects our payscale :)

Add a comment
Know the answer?
Add Answer to:
dont use switch E) 120 pts.] Write another function int main ) in C++(and comment the...
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 program that will do the following. The main function should ask the user how...

    Write a program that will do the following. The main function should ask the user how many numbers it will read in. It will then create an array of that size. Next, it will call a function that will read in the numbers and fill the array (fillArray). Pass the array that was made in themain function as a parameter to this function. Use a loop that will read numbers from the keyboard and store them in the array. This...

  • Done in C++ using visual studio 1. Write a program with one additional function called int[]...

    Done in C++ using visual studio 1. Write a program with one additional function called int[] reverseArray(int array). This function will receive an array from main and then reverse and should return the reversed array to the main to print out. Use a single array and a single loop, you’re are neither allowed to print out just in reverse order nor allowed to use another array variable to store the original array in reverse order. 2. Write a program which...

  • You will create a program with two methods, the main() method of course and another method...

    You will create a program with two methods, the main() method of course and another method called printArray(). The main method will define an array of 5 elements. A loop will be used to prompt the user to enter 5 numeric values which will go into the 5 array elements. The main() method will then call the printArray() method passing the array by reference, and the array length by value. The printArray() method will then print the contents of the...

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • Programming Fundamental (C++) Lab C++ Lab Ra'fat Amareh Faculty of Engineering and Information Technology Assignment Write...

    Programming Fundamental (C++) Lab C++ Lab Ra'fat Amareh Faculty of Engineering and Information Technology Assignment Write a C++ program that performs the following: Prints on screen a selection menu as follow: a. Sin (x), Series b. Sum odd digit, Print Digits, Print Shape c. Array d. Exit If user presses a or A, the following sub menu should be displayed 1- Sin (x) (Program should solve the following series x - x'/3! + x®/5! – x/7...x"m!) 2- F (Program should...

  • C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the...

    C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the user for ten (10) grades, and store the data in an array.  Compute the average of all the grades.  Print the original ten grades and the average. a.       Declare an integer array with the name of “grades” of size 10 in the main function. b.      Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array.                                                                i.      The function will receive...

  • Please design, write the code and create the requisite submission files in C++ for the following...

    Please design, write the code and create the requisite submission files in C++ for the following problem: A menu-driven program that offers the user 5 options as follows: Menu: 1. Display array 2. Add up all elements in the array and display that number. 3. Add 1 to each element in the array. 4. Swap the values in row one with the values in row three. 5. Exit In main(), declare and initialize a 2-dimensional array that contains 5 x...

  • Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int...

    Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int *a2) Write a program addition.c that reads in an array (a1) of numbers, and creates a new array (a2) of numbers such that the first and last numbers of a1 are added and stored as the first number, the second and second-to-last numbers are added and stored as the second number, and so on. You need to check for even and odd length of...

  • The loop below stores the values 1 to 5 in myList: for (int i = 0;...

    The loop below stores the values 1 to 5 in myList: for (int i = 0; i < 5; i ++) { myList [ i ] = i + 1; //store values using a loop } The loop below prints the values from first to last in myList: for (int i = 0; i < myList.length; i++) { System.out.print(myList [ i ] + " "); } Use the examples on the slides to complete the lab below. Please submit the...

  • please use c ++ language and write the comment too. thanks Pointer Arithmetic Write a function...

    please use c ++ language and write the comment too. thanks Pointer Arithmetic Write a function that passes an array address to a function as a pointer. Using pointer arithmetic, the function determines the average, high, and low value of the array. Your function should have this header: void pointerArithmetic(int *array, int size, double &average, int &high, int &low) Parameter size is the number of elements in array. You may not use the subscript notation [] inside your function –...

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
ADVERTISEMENT