Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a class that has an array of floating-point numbers. The constructor shou

ID: 3841685 • Letter: D

Question

Design a class that has an array of floating-point numbers. The constructor should accept an integer argument and dynamically allocate the array to hold that many numbers. The private data members of the class should include the integer argument in a variable to hold the size of the array and a pointer to float type to hold the address of the first element in the array . The destructor should free the memory held by the array . In addition, there should be member functions to perform the following operations : • Store a number in any element of the array • Retrieve a number from any element of the array • Return the highest value stored in the array • Return the lowest value stored in the array • Return the average of all the numbers stored in the array Demonstrate the Class in a Program

Explanation / Answer

class Demo {
    private:
        float *list;
        int size;
    public:
        Demo(int n){
            list = new float[n];
            size = n;
        }
        ~Demo(){
           delete [] list;
        }
        void store(int n, int pos){   // store n at pos location in the array
            if (pos < size && pos >= 0)
               list[pos] = n;
            else
               cout << "Position out of bound of the list" << endl;
        }
        float retrieve(int pos){   // retrieve value at pos location in the array
            if (pos < size && pos >= 0)
               return list[pos];
            else
               cout << "Position out of bound of the list" << endl;
        }
        float highest(){   // returns the highest value in the array
            float max = 0;
            for (int i = 0; i<size; i++){
                if (list[i] > max)
                   max = list[i];
            }
            return max;
        }
        float lowest(){   // returns the lowest value in the array
            float min = list[0];
            for (int i = 0; i<size; i++){
                if (list[i] < min)
                   min = list[i];
            }
            return min;
        }
        float average(){   // returns the average value of the array
            float sum = 0;
            for (int i = 0; i<size; i++){
                 sum = sum + list[i];
            }
            return sum/size;
        }
};


void main() {

    Demo dm(5);
   
    dm.store(4,0);
    dm.store(3,1);
    dm.store(7,2);
    dm.store(10,3);
    dm.store(11,4);

    cout << "Value at index 3:" << dm.retrieve(3) << endl;
    cout << "Highest:" << dm.highest() << endl;
    cout << "lowest:" << dm.lowest() << endl;
    cout << "Avearge:" << dm.average() << endl
   
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote