Answer to Question #274058 in C++ for Amit

Question #274058

Write a program to create a class template having a an array and perform the

following menu based operation on object with integer array , floating point

array


l. Search an user entered element


ll. Sum of all the elements.


lll. Find 2 nd largest element and 2 nd minimum element


1
Expert's answer
2021-12-11T01:57:43-0500
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;


template <typename T>
class Array {
public:
    Array(int size);
    ~Array();
    bool Search(T x);
    T Sum();
    T SecondLargest();
    T SecondSmallest();
    int Size() { return size; }
    void SetValue(int i, T x);
    T GetValue(int i);
    void InitRandom(T lo, T hi);
    void Print();


private:
    int size;
    T* data;
};


template<typename T>
Array<typename T>::Array(int size) : size(size) 
{
    data = new T[size];
}


template <typename T>
Array<typename T>::~Array()
{
    delete [] data;
}


template <typename T>
bool Array<typename T>::Search(T x)
{
    for (int i=0; i<size; i++) {
        if (data[i] == x) {
            return true;
        }
    }
    return false;
}


template <typename T>
T Array<typename T>::Sum() 
{
    T sum = 0;
    for (int i=0; i<size; i++) {
        sum += data[i];
    }
    return sum;
}


template <typename T>
T Array<typename T>::SecondLargest() {
    if (size < 2) {
        return 0;
    }
    T large, large2;
    if (data[0] > data[1]) {
        large = data[0];
        large2 = data[1];
    }
    else {
        large = data[1];
        large2 = data[0];
    }
    for (int i=2; i<size; i++) {
        if (data[i] >= large) {
            large2 = large;
            large = data[i];
        }
        else if (data[i] > large2) {
            large2 = data[i];
        }
    }
    return large2;
}


template <typename T>
T Array<typename T>::SecondSmallest() 
{
    if (size < 2) {
        return 0;
    }
    T small, small2;
    if (data[0] > data[1]) {
        small = data[1];
        small2 = data[0];
    }
    else {
        small = data[0];
        small2 = data[1];
    }
    for (int i=2; i<size; i++) {
        if (data[i] <= small) {
            small2 = small;
            small = data[i];
        }
        else if (data[i] < small2) {
            small2 = data[i];
        }
    }
    return small2;


}


template <typename T>
void Array<typename T>::SetValue(int i, T x)
{
    if (i>=0 && i<size) {
        data[i] = x;
    }


}


template <typename T>
T Array<typename T>::GetValue(int i)
{
    if (i>=0 && i<size) {
        return data[i];
    }
    return 0;
}


template <typename T>
void Array<typename T>::InitRandom(T lo, T hi) 
{
    for (int i=0; i<size; i++) {
        data[i] = rand() / static_cast<double>(RAND_MAX) *(hi-lo) + lo;
    }
}


template <typename T>
void Array<typename T>::Print() {
    for (int i=0; i<size; i++) {
        cout << data[i];
        if ((i+1) % 10 == 0) {
            cout << endl;
        }
        else  {
            cout << " ";
        }
    }
    if (size % 10 != 0) {
        cout << endl;
    }
}


int Menu() {
    string menu[4] = {"1. Search an user entered element",
                      "2. Sum of all the elements.",
                      "3. Find 2 nd largest element and 2 nd minimum element.",
                      "4. Exit. "};
    for (int i=0; i<4; i++) {
        cout << menu[i] << endl;
    }
    cout << "Enter your choice: ";
    int ans;
    cin >> ans;
    return ans;
}


int main() {
    int choice;
    bool done;


    Array<int> arr_i(10);
    int i;
    arr_i.InitRandom(1, 20);
    
    done = false;
    while (!done) {
        cout << "Integer array:" << endl;
        arr_i.Print();
        cout << endl;


        choice = Menu();
        switch (choice) {
        case 1:
            cout << "Enter value to search: ";
            cin >> i;
            if (arr_i.Search(i) ) {
                cout << "The value was found" << endl;
            }
            else {
                cout << "The value was not found" << endl;
            }
            break;


        case 2:
            cout << "The sum of elements is "<< arr_i.Sum() << endl;
            break;


        case 3:
            cout << "The 2nd lagest elemet is " << arr_i.SecondLargest() << endl;
            cout << "The 2ng smallest element is " << arr_i.SecondSmallest() << endl;
            break;


        default:
            done = true;
        }
        cout << endl;
    }


    Array<double> arr_d(10);
    double x;
    arr_d.InitRandom(0, 10);
    
    done = false;
    while (!done) {
        cout << "Float point array:" << endl;
        arr_d.Print();
        cout << endl;


        choice = Menu();
        switch (choice) {
        case 1:
            cout << "Enter value to search: ";
            cin >> x;
            if (arr_d.Search(x) ) {
                cout << "The value was found" << endl;
            }
            else {
                cout << "The value was not found" << endl;
            }
            break;


        case 2:
            cout << "The sum of elements is "<< arr_d.Sum() << endl;
            break;


        case 3:
            cout << "The 2nd lagest elemet is " << arr_d.SecondLargest() << endl;
            cout << "The 2ng smallest element is " << arr_d.SecondSmallest() << endl;
            break;


        default:
            done = true;
        }
        cout << endl;
    }
}

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