Answer to Question #163596 in C++ for sakshi

Question #163596

Write a menu driven program for the following operations on array for 10 elements:

a) display max number from the array

b) display min number from the array

c) display sec max number from the array

d) display sec min number from the array

e) sort the data

f) search the data 

g) display count of prime numbers

h) display count of perfect numbers

i) display max digit from each number 

j) display sum of digits upto one digit for each number


1
Expert's answer
2021-02-15T00:02:05-0500
#include <iostream>
#include <stdlib.h>
#include <ctime>
using namespace std;
void sortAscending (int array[], int len) {
    for (int i = 0; i < len; i++) {
        for (int j = 0; j < i; j++) {
            if (array[i] < array[j]) {
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }
}
bool checkPrimeNumber(int n) {
    bool isPrime = true;

    // 0 and 1 are not prime numbers
    if (n == 0 || n == 1) {
        isPrime = false;
    }
    else {
        for (int i = 2; i <= n / 2; ++i) {
            if (n % i == 0) {
                isPrime = false;
                break;
            }
        }
    }
    return isPrime;
}
bool isPerfect(int n) {
    int sum = 0;
    for (int i = 1; i < n; i++) {

        if (n % i == 0)
            sum += i;
    }
    if (sum == n) {
        return true;
    }
    else {
        return false;
    }
}
void maxDigit(int n) {
    int largest = 0;
    while (n) {
        int r = n % 10;
        // Find the largest digit
        largest = max(r, largest);
        n = n / 10;
    }
    cout << largest;
}
int main () {
    srand(time(NULL));
    int arr[10], originalArr[10];
    // fill the array with random numbers.
    for (int i = 0; i < 10; i++) {
        arr[i] = rand() % 100;
        originalArr[i] = arr[i];
    }
    cout << "Our array: ";
    for (int i = 0; i < 10; i++) {
        cout << arr[i] << " ";
    }
    
    cout << "\n|==|==| SELECT ONE |==|==|";
    cout << "\na) display max number from the array";
    cout << "\nb) display min number from the array";
    cout << "\nc) display sec max number from the array";
    cout << "\nd) display sec min number from the array";
    cout << "\ne) sort the data";
    cout << "\nf) search the data ";
    cout << "\ng) display count of prime numbers";
    cout << "\nh) display count of perfect numbers";
    cout << "\ni) display max digit from each number ";
    cout << "\nj) exit";
    
    char option;
    
    while (true) {
        cout << "\n\nEnter your option: "; cin >> option;
        if (option == 'a') {
            sortAscending(arr, 10);
            int max = arr[9];
            cout << "max number in array is: " << max;
        }
        else if (option == 'b') {
            sortAscending(arr, 10);
            int min = arr[0];
            cout << "max number in array is: " << min;
        }
        else if (option == 'c') {
            sortAscending(arr, 10);
            int maxSecond = arr[8];
            cout << "max number in array is: " << maxSecond;
        }
        else if (option == 'd') {
            sortAscending(arr, 10);
            int minSecond = arr[1];
            cout << "max number in array is: " << minSecond;
        }
        else if (option == 'e') {
            sortAscending(arr, 10);
            cout << "Sorted array: ";
            for (int i = 0; i < 10; i++) {
                cout << arr[i] << " ";
            }
            cout << endl;
        }
        else if (option == 'f') {
            int search, index = -1;
            cout << "Enter a number for search: ";
            cin >> search;
            for (int i = 0; i < 10; i++) {
                if (originalArr[i] == search) {
                    index = i;
                    break;
                }
            }
            if (index != -1) {
                cout << search << " found in the array at a index of " << index;
            }
            else {
                cout << "Number not found.";
            }
        }
        else if (option == 'g') {
            int primeCount = 0;
            for (int i = 0; i < 10; i++) {
                if (checkPrimeNumber(arr[i])) {
                    primeCount++;
                }
            }
            cout << "The number of prime numbers in the array: " << primeCount;
        }
        else if (option == 'h') {
            int perfectCount = 0;
            for (int i = 0; i < 10; i++) {
                if (isPerfect(arr[i])) {
                    perfectCount++;
                }
            }
            cout << "The number of perfect numbers in the array: " << perfectCount;
        }
        else if (option == 'i') {
            for (int i = 0; i < 10; i++) {
                cout << "max digit of " << originalArr[i] << " is a: ";
                maxDigit(originalArr[i]);
                cout << endl;
            }
        }
        else if (option == 'k') {
            break;
        }
        else {
            break;
        }
    }
    
}

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