Answer to Question #213941 in C++ for Maryam

Question #213941

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 

destructor should free the memory held by the array. In addition, there should be member 

functions to perform the following operations:

• Store a number at any index of the array

• Retrieve a number from any index 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.


1
Expert's answer
2021-07-06T12:17:19-0400
#include <iostream>


using namespace std;
class Arrays{
    private:
        float * arr;
    public:
        Arrays(int n){
            arr=new float[n];
        }
        ~Arrays(){
            delete [] arr;
        }
        
        float storeNumber(float num, int index){
            arr[index]=num;
        }
        float retriveNumber(int index){
            return arr[index];
        }
        float highestNum(int n){
            int max=arr[0];
            for(int i=0;i<n;i++){
                if (arr[i]>max)
                    max=arr[i];
            }
            return max;
        }
        float lowestNum(int n){
            int min=arr[0];
            for(int i=0;i<n;i++){
                if (arr[i]<min)
                    min=arr[i];
            }
            return min;
        }
        float averageNum(int n){
            float sum=0;
            for(int i=0;i<n;i++){
                sum=sum+arr[i];
            }
            return (sum/n);
        }
};
int main()
{
    Arrays r(5);
    r.storeNumber(44,0);
    r.storeNumber(42,1);
    r.storeNumber(67,2);
    r.storeNumber(33,3);
    r.storeNumber(78,4);
    cout<<"\nArray number at index 2:"<<r.retriveNumber(2);
    cout<<"\nHighest:"<<r.highestNum(5);
    cout<<"\nLowest:"<<r.lowestNum(5);
    cout<<"\nAverage:"<<r.averageNum(5);
    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
New on Blog
APPROVED BY CLIENTS