Answer to Question #155912 in C++ for Abdul Rehman

Question #155912

Write a C++ menu base program in which, perform following tasks on an integer array of size 20:  Press 1 for insert data  Press 2 for display data o Press 1 for display full array o Press 2 for display array till mid o Press 3 for display array from mid o Press 4 for display array between given range  Press 3 for delete data  Press 4 for update data  Press 5 for search data o Press 1 for binary search o Press 2 for liner search  Press 1 for check presence  Press 2 for count occurrence  Press 3 for display index of each occurrence  Press 6 for sort data o Press 1 for ascending order o Press 2 for descending order  Press 7 for reverse the data  Press 8 for maximum data  Press 9 for minimum data  Press 10 for average value  Press 11 for remove all data from array  Press 12 for check array is empty or not  Press 13 for check array is full or not  Press 14 for exit


1
Expert's answer
2021-01-16T11:47:55-0500
#include <iostream>
using namespace std;
int main () {
    int Array[20] = {1, 34, 24, 56, 78, 65, 44, 90, 100, 8, 12, 24, 56, 78, 98, 34, 65, 78, 98, 120};
    cout << "Our Array: ";
    for (int i = 0; i < 20; i++) {
        cout << Array[i] << " ";
    }
    cout << "\nThis programm do the following works: \n";
    cout << "1. Insert data.\n";
    cout << "2. display data.\n";
    cout << "3. delete data.\n";
    cout << "4. Update data.\n";
    cout << "5. Search data.\n";
    cout << "6. Sort data.\n";
    cout << "7. Reverse the data.\n";
    cout << "8. Maximum data.\n";
    cout << "9. minimum data.\n";
    cout << "10. Avarage value.\n";
    cout << "11. exit.\n";
    int selector;
    
    while (true) {
        cout << "\nEnter number for selecting for order of works which program do: ";
        cin >> selector;
        if (selector == 1) {
            int element;
            cout << "\nWhich element do you want to insert: \n";
            cout << "Enter element: "; cin >> element;
            Array[20] = element;
        }
        if (selector == 2) {
            cout << "\nWe can display in a following types: \n";
            cout << "0. exit\n";
            cout << "1. display full array.\n";
            cout << "2. display array till middle.\n";
            cout << "3. display array from middle.\n";
            cout << "4. display array between given range.\n";
            int selectorDisplay;
            while(true) {
                cout << "\nEnter number for selecting for order of works of Display which program do: ";
                cin >> selectorDisplay;
                if (selectorDisplay == 0) {
                    break;
                }
                if (selectorDisplay == 1) {
                    for (int i = 0; i < 21; i++) {
                        cout << Array[i] << " ";
                    }
                }
                if (selectorDisplay == 2) {
                    for (int i = 0; i < 11; i++) {
                        cout << Array[i] << " ";
                    }
                }
                if (selectorDisplay == 3) {
                    for (int i = 12; i < 21; i++) {
                        cout << Array[i] << " ";
                    }
                }
                if (selectorDisplay == 4) {
                    cout << "Enter range: [a, b]\n";
                    int a, b;
                    cout << "a: "; cin >> a;
                    cout << "b: "; cin >> b;
                    for (int i = a; i <= b; i++) {
                        cout << Array[i] << " ";
                    }
                }
            }
        }
        if (selector == 3) {
            int deleteEntry;
            cout << "Enter a element which do you want to delete: \n";
            cin >> deleteEntry;
            bool isHave = true;
            for (int i = 0; i < 21; i++) {
                if (deleteEntry == Array[i]) {
        	        for(int j = i; j < 21; j++)
                        Array[j] = Array[j + 1];
                    i--;
                    isHave = false;
                }
            }   
            if (isHave) {
                cout << "the number which must be delete are not found.\n";
            } 
            else {
                for (int i = 0; i < 20; i++) {
                    cout << Array[i] << " ";
                }
            }
        }
        if (selector == 4) {
            cout << "Enter two numbers first is the element of array, second is the value of element of array after updating\n";
            int first, second;
            cout << "Enter first number: "; cin >> first;
            cout << "Enter second number: "; cin >> second;
            for (int i = 0; i < 20; i++) {
                if (Array[i] == first) {
                    Array[i] = second;
                    cout << Array[i] << " ";
                }
                else {
                    cout << Array[i] << " ";
                }
            }
        }
        if (selector == 5) {
            cout << "\nWe can search in a following types: \n";
            cout << "0. exit\n";
            cout << "1. binary search.\n";
            cout << "2. linear search.\n";
            cout << "3. count occurrance.\n";
            cout << "4. display index of each occurrence.\n";
            
            int selectorSearch;
            while (true) {
                cout << "\nEnter number for selecting for order of works of Search which program do: \n";
                cin >> selectorSearch;
                int searchNumber, count = 0;
                bool isHave = true;
                if (selectorSearch == 0) {
                    break;
                }
                if (selectorSearch == 1 || selectorSearch == 2) {
                    cout << "Enter a number for search: \n";
                    cin >> searchNumber;
                    for (int i = 0; i < 20; i++) {
                        if (searchNumber == Array[i]) {
                            cout << "element is found at " << i << " - index.\n";
                            isHave = false;
                        }
                    }
                }
                if (selectorSearch == 3) {
                    cout << "Enter a number for search: \n";
                    cin >> searchNumber;
                    for (int i = 0; i < 20; i++) {
                        if (searchNumber == Array[i]) {
                            count++;
                            isHave = false;
                        }
                    }
                    cout << "the occurrence of " << searchNumber << " is a " << count;
                }
                if (selectorSearch == 4) {
                    cout << "Enter a number for search: \n";
                    cin >> searchNumber;
                    cout << "The searched number index is: ";
                    for (int i = 0; i < 20; i++) {
                        if (searchNumber == Array[i]) {
                            cout << i << " ";
                            isHave = false;
                        }
                    }
                }
                if (isHave) {
                    cout << "element is not found\n";
                }
            }
        }
        if (selector == 6) {
            cout << "\nWe can sort in a following types: \n";
            cout << "0. exit\n";
            cout << "1. ascending order.\n";
            cout << "2. descending search.\n";
            int selectorSort;
            while (true) {
                cout << "\nEnter number for selecting for order of works of Sort which program do: \n";
                cin >> selectorSort;
                if (selectorSort == 0) {
                    break;
                }
                if (selectorSort == 1) {
                    cout << "ascending order: ";
                    for (int i = 0; i < 20; i++) {
                        for (int j = i + 1; j < 20; j++) {
                            if (Array[i] > Array[j]) {
                                int temp = Array[i];
                                Array[i] = Array[j];
                                Array[j] = temp;
                            }
                        }
                    }
                    for (int i = 0; i < 20; i++) {
                        cout << Array[i] << " ";
                    }
                }
                if (selectorSort == 2) {
                    cout << "descending order: ";
                    for (int i = 0; i < 20; i++) {
                        for (int j = i + 1; j < 20; j++) {
                            if (Array[i] < Array[j]) {
                                int temp = Array[i];
                                Array[i] = Array[j];
                                Array[j] = temp;
                            }
                        }
                    }
                    for (int i = 0; i < 20; i++) {
                        cout << Array[i] << " ";
                    }
                }
            }
        }
        if (selector == 7) {
            cout << "Reverse order: ";
            for (int i = 19; i >= 0; i--) {
                cout << Array[i] << " ";
            }
        }
        if (selector == 8) {
            int max = Array[0];
            for (int i = 0; i < 20; i++) {
                if (max < Array[i]) {
                    max = Array[i];
                }
            }
            cout << "Maximum data: " << max;
        }
        if (selector == 9) {
            int min = Array[0];
            for (int i = 0; i < 20; i++) {
                if (min > Array[i]) {
                    min = Array[i];
                }
            }
            cout << "Minimum data: " << min;
        }
        if (selector == 10) {
            int sum = 0;
            for (int i = 0; i < 20; i++) {
                sum += Array[i];
            }
            cout << "The Avarage value is " << (float) sum / 20.;
        }
        if (selector == 14) {
            break;
        }
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS