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-pointnumbers. The constructer shoul

ID: 3615065 • Letter: D

Question

Design a class that has an array of floating-pointnumbers. The constructer should accept an interger argumentand dynamically allocate the array to hold that many numbers. The destructor should free the memory held by the array. Inaddition, there should be member functions to perform the followingoperations:

* 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

// Chapter 13, ProgrammingChallenge 11
// Number Array Class
#include <iostream>
using namespace std;

class NumberArray
{
private:
   double *arrayPointer;
   int size;
  
public:
   // Constructor
   NumberArray(int);
  
   // Destructor
   ~NumberArray();
  
   // Mutator
   void setCell(int, double);
  
   // Accessor
   int getCell(int);
  
   // Average function
   double average();
};

//Constructor                            
NumberArray::NumberArray(int s)
{
   arrayPointer = new double[s];
   size = s;
}

//Destructor                             
NumberArray::~NumberArray()
{
   if (arrayPointer != NULL)
      delete [] arrayPointer;
}

// setCell memberfunction               
void NumberArray::setCell(int c, double val)
{
   if (arrayPointer != NULL && c >= 0&& c <= size)
      arrayPointer[c] = val;
   else
   {
      cout << "Invalid cellnumber. ";
      exit(EXIT_FAILURE);
   }
}

// getCell memberfunction                 
int NumberArray::getCell(int cellNum)
{
   if (cellNum < 0 || cellNum > size)
   {
      cout << "Invaild cellnumber. ";
      exit(EXIT_FAILURE);
   }

   returnarrayPointer[cellNum];
}

// average memberfunction                 
double NumberArray::average()
{
   double total = 0;

   for (int count = 0;count < size; count++)
      total += arrayPointer[count];
   return total / size;
}

int main()
{
   int howMany;   // The number of numbers
   int count;     // Loop counter
   double val;    // To hold avalue

   // Get the number ofnumbers to store.
   cout << "How many numbers do you want to store?";
   cin >> howMany;
  
   // Create a NumberArray object.
   NumberArray numbers(howMany);
  
   // Get the numbers.
   cout << "Enter the " << howMany << "numbers: ";
   for (count = 0; count < howMany; count++)
   {
      // Get a number.
      cout << " Number " <<(count+1) << ": ";
      cin >> val;
     
      // Store it in the object.
      numbers.setCell(count, val);
   }

   // Display the valuesin the object.
   cout << "Here are the values youentered: ";
   for (count = 0; count < howMany; count++)
   {
      cout << " Number " <<(count+1) << ": "
          << numbers.getCell(count)
          << endl;
   }
  
   // Display the average of the values.
   cout << "The average of those values is ";
   cout << numbers.average() << endl;
  
   cin.get();//to pause system
   cin.get();
   return 0;
}

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