c with comments please would be great 10 Number Array Class
c++ with comments please would be great..
10. Number Array Class 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 SAMPLE RUN : Interactive Session: Creating a Float Class Array Object with a size of 5.Using setI function with calls (0,1.4),(1,2.3),(2,3.13),(3,6.66),(4,7.711) to load array. Using getI with all legitmate index values to print array. The value of fArray1[0]= 1.4 The value of fArray1[1]= 2.3 The value of fArray1[2]= 3.13 The value of fArray1[3]= 6.66 The value of fArray1[4]= 7.711 Calling getHi returns highest value in array as: 7.711 Calling getLow returns lowest value in array as: 1.4 Calling getAvg returns the average of the array as: 4.2402
Solution
Main.cpp
#include \"NumberClass.h\"
#include <iostream>
using namespace std;
int main()
{
NumberClass * myArr;
int element;
int size;
cout << \"Enter the arrays\'s size: \";
cin >> size;
cout << endl;
myArr = new NumberClass(size);
for (int index = 0; index < size; index++)
{
cout << \"What is the number that is in \" << index << \" place of the array? \ \";
cin >> myArr->theArray[index];
}
cout << \"Pick an element in the array. \ \";
cin >> element;
cout << \"\\t Array Information \ \";
cout << \"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ \";
cout << \"\\t Number from element \" << element << \" :\" << myArr->theArray[element] << endl;
cout << \"\\t Highest : \" << myArr->getHighest() << endl;
cout << \"\\t Lowest: \" << myArr->getLowest() << endl;
cout << \"\\t Average: \" << myArr->getAverage() << endl;
cout << endl;
cout << \"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ \";
cout << endl;
delete myArr;
}
NumberClass.cpp
# include \"NumberClass.h\"
#include <cstdlib>
#include <iostream>
using namespace std;
NumberClass::NumberClass(int size)
{
theArray = new int[size];
numElements = size;
for (int index = 0; index < size; index++)
theArray[index] = 0;
}
NumberClass::~NumClass()
{
delete[]theArray;
theArray = 0;
}
double NumberClass::getHighest() const
{
int cnt;
double highest;
highest = theArray[0];
for (cnt = 1; cnt < numElements; count++)
{
if (theArray[cnt] > highest)
{
highest = theArray[cnt];
}
}
return highest;
}
double NumberClass::getLowest() const
{
int cnt;
double lowest;
lowest = theArray[0];
for (cnt = 1; cnt < numElements; cnt++)
{
if (theArray[cnt] < lowest)
{
lowest = theArray[cnt];
}
}
return lowest;
}
double NumberClass::getAverage() const
{
double tot = 0;
int cnt;
double avg;
for (cnt = 0; cnt < numElements; cnt++)
{
tot = +theArray[cnt];
}
avg = (tot / cnt);
return avg;
}
NumberClass.h
#ifndef NumberClass_H
#define NumberClass_H
class NumberClass
{
public:
int *theArray;
int numElements;
NumberClass(int);
~NumberClass();
void setHighest(double);
void setLowest(double);
void setAverage(double);
double getHighest() const;
double getLowest() const;
double getAverage() const;
};
#endif



