Question
Take the following program and then add the overloaded cin (>>), cout( < = 0 && c size) { cout < < Invaild cell number.
Take the following program and then add the overloaded cin (>>), cout(<<), the equals (==), the equal (=), and the brackets ( [] ) functions.
Program:
class NumberArray { private: double *arrayPointer; int size; public: // Constructor NumberArray(int); // Destructor ~NumberArray(); // Mutator void setCell(int, double); // Accessor int getCell(int); // getAverage function double getAverage();
// getHighest function double getHighest();
// getLowest function double getLowest(); };
++++++functions++++++++
//******************************************** // Constructor * //********************************************
NumberArray::NumberArray(int s) { arrayPointer = nullptr; arrayPointer = new double[s]; size = s; }
//******************************************** // Destructor * //********************************************
NumberArray::~NumberArray() { if (arrayPointer != nullptr) { delete[] arrayPointer; arrayPointer = nullptr; } }
//******************************************** // setCell member function * //********************************************
void NumberArray::setCell(int c, double val) { if (arrayPointer != nullptr && c >= 0 && c <= size) arrayPointer[c] = val; else { cout << "Invalid cell number. "; exit(EXIT_FAILURE); } }
//******************************************** // getCell member function * //********************************************
int NumberArray::getCell(int cellNum) { if (cellNum < 0 || cellNum > size) { cout << "Invaild cell number. "; exit(EXIT_FAILURE); }
return arrayPointer[cellNum]; }
//******************************************** // getAverage member function * //********************************************
double NumberArray::getAverage() { double total = 0;
for (int count = 0; count < size; count++) { total += arrayPointer[count]; } return total / size; }
//******************************************** // getHighest member function * //********************************************
double NumberArray::getHighest() { double highest = arrayPointer[0];
for (int count = 1; count < size; count++) { if (arrayPointer[count] > highest) { highest = arrayPointer[count]; } }
return highest; }
//******************************************** // getLowest member function * //********************************************
double NumberArray::getLowest() { double lowest = arrayPointer[0];
for (int count = 1; count < size; count++) { if (arrayPointer[count] < lowest) { lowest = arrayPointer[count]; } }
return lowest; }
+++++++++main+++++++++
int main() { int howMany; // The number of numbers int count; // Loop counter double val; // To hold a value
// Get the number of numbers 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 << "\tNumber " << (count+1) << ": "; cin >> val; // Store it in the object. numbers.setCell(count, val); }
// Display the values in the object. cout << " --------------------------------- "; cout << " Here are the numbers you entered: "; for (count = 0; count < howMany; count++) { cout << "Number " << (count+1) << ": " << numbers.getCell(count) << endl; } // Display the average of the values. cout << " --------------------------------- "; cout << "The average of those numbers is: "; cout << numbers.getAverage() << endl;
// Display the highest of the values. cout << "The highest of those numbers is: "; cout << numbers.getHighest() << endl;
// Display the lowest of the values. cout << "The lowest of those numbers is: "; cout << numbers.getLowest() << endl << endl;
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started