I\'m relatively new to coding and need help on this assignment. Design a class t
ID: 655430 • Letter: I
Question
I'm relatively new to coding and need help on this assignment.
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 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
#include <iostream>
#include <cstdlib>
#include "numberList.h"
using namespace std;
NumberList::NumberList(int size)
{
list = new int[size];
numElements = size;
for(int index=0; index<size; index++)
list[index]=0;
}
NumberList::~NumberList()
{
delete []list;
}
bool NumberList::isValid(int element)
{
bool status;
if(element < 0 || element >= numElements)
status=false;
else
status = true;
return status;
}
void NumberList::setElement(int element, int value)
{
if (isValid(element))
list[element]=value;
else
{
cout<<"Error: Invalid subscript ";
exit(EXIT_FAILURE);
}
}
int NumberList::getElement(int element)
{
if(isValid(element))
return list[element];
else
{
cout<<"Error: Invalid subscript ";
exit(EXIT_FAILURE);
}
}
//getHighest
double NumberList::getHighest() const
{
int count; // local to count in loop
double highest; // to hold highest
highest = list[0];
for (count =1; count < numElements; count++)
{
if (list[count] > highest)
{
highest = list[count];
}
}
return highest;
}
double NumberList::getLowest() const
{
int count;
double lowest;
lowest = list[0];
for (count = 1; count < numElements; count++)
{
if (list[count] < lowest)
{
lowest = list[count];
}
}
return lowest;
}
double NumberList::getAverage() const
{
double total = 0.0;
int count;
double average=0.0;
for (count = 1; count < numElements; count++)
{
total =+ list[count];
}
average = (total/count);
return average;
}
#include <iostream>