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.
#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;
}
Comments
Leave a comment