I am working on designing 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 that 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
Return the lowest value stored
Return the average value of the numbers stored
I have written my header and my main.
Struggling with setting up my arrays and variables.
What to put in the header and what to put in the main.
Any and all help and suggestions are really appreciated! I'm just trying to figure this out now, been trouble shooting for a few days now. I think I'm just not seeing it.
#ifndef mainH_H
#define mainH_H
#include <string>
usingnamespace std;
class mainH // class name
{
private:
int numElements; // number of elements
public:
mainH(int); // constructor
~mainH(); // destructor
double *theArray; // pointer to the array
// Mutators
void setHighest(double);
void setLowest(double);
void setAverage(double);
void input_number(int);
// Accessors
double getHighest() const;
double getLowest() const;
double getAverage() const;
double get_element(int) const;
};
#endif
#include <iostream>
#include <array>
#include "header.h"
usingnamespace std;
double mainH::getHighest() const
{
int count;
double highest;
highest = theArray[0];
for (count =1; count < numElements; count++)
{
if (theArray[count] > highest)
{
// stores the highest number
highest = theArray[count];
}
}
return highest;
}
// Accessor function for the lowest
// use const to tell complier not to change it
double mainH::getLowest() const
{
int count;
double lowest;
lowest = theArray[0];
for (count = 1; count < numElements; count++)
{
if (theArray[count] < lowest)
{
// stores the lowest number
lowest = theArray[count];
}
}
return lowest;
}
double mainH::getAverage() const
{
int count;
double average;
double sum = 0.0;
for (count = 0; count < numElements; count++)
{
if (count < numElements)
{
// stores the sum of numbers
sum += theArray[count];
}
}
average = (sum / numElements);
return average;
}
mainH::mainH(int size)
{
theArray = newdouble[size]; // points to a dynamically allocated array of doubles
numElements = size; // number of elements in the array
for (int index = 0; index < size; index++)
theArray[index] = 0; // set everyting to zero to start
}
mainH::~mainH()
{
//destructor
delete[]theArray; //free memory
theArray = 0; //clear to prevent invalid memory address
}
double mainH::get_element(int i) const
{
return theArray[i];
}
void mainH::input_number(int i)
{
cin >> theArray[i];
}
int main()
{
int a_size = 10; // initialize
mainH myArray(a_size); // name for instance of array
int element;
int numb;
int size;
cout << "How many numbers do you want to store? "; // Get the array size.
cin >> size;
// put number into array
for (int i =0; i < size; i++)
{
cout << "Number "<< i << " : ";
void input_number(int i);
}
// Display the Array's data.
cout << "___________________________________________________________ \n\n"<<endl;
cout<<endl<<endl;
cout << "The average of those values is " << myArray.getAverage() << endl;
cout << "The highest number is " << myArray.getHighest() << endl;
cout << "The lowest number is " << myArray.getLowest() << endl;
cout << "___________________________________________________________"<<endl;
cout << endl;
cout << endl;
cout << "Which cell do you want to retrieve the value from? \n";
cout<<endl;
cout << "Cell Number: ";
cin >> element;
cout<<endl;
cout << "Cell number " << element << " contains : " << endl;
double get_element (element);
{
cout<< myArray.theArray[element];
}
cout<<endl;
cout<<endl;
cout << "___________________________________________________________"<<endl;
cout<<endl;
cout<<endl;
cout <<"Which cell do you want to modify the value? \n";
cout << "Cell Number: ";
cin >> element;
cout<<endl;
cout << "Cell number " << element << " new number will be: ";
cin >> numb;
myArray.theArray[element] = numb;
double get_element (element);
{
cout<< myArray.theArray[element];
}
cout << "___________________________________________________________"<<endl;
cout<<endl;
cout<<endl;
cout << "Which cell do you want to retrieve the value from? \n";
cout << "Cell Number: ";
cin >> element;
cout<<endl;
cout << "Cell number " << element << " contains : " ;
double get_element (element);
{
cout<< myArray.theArray[element];
cout<<endl;
}
cout << "___________________________________________________________"<<endl;
system("pause");
return 0;
}
/*
How many numbers do you want to store? 5
Number 0 : 56
Number 1 : 13
Number 2 : 99
Number 3 : 47
Number 4 : 23
___________________________________________________________The average of those
values is 23.8
The highest number is 99
The lowest number is 0
___________________________________________________________
Which cell do you want to retrieve the value from?
Cell Number: 2
Cell number 2 contains : 99
___________________________________________________________
Which cell do you want to modify the value?
Cell Number: 2
Cell number 2 new number will be: 130
130
___________________________________________________________
Which cell do you want to retrieve the value from?
Cell Number: 2
Cell number 2 contains : 130
___________________________________________________________
Press any key to cont
inue . . .
*/
Again any help or guidance is greatly appreciated!
Again relies on external knowledge of where to put the number.
3) overload the >> operator
1 2 3 4 5
friend istream & operator << (istream & is, mainH & arr)
{ // assumes num_entries tracks how many entries have been added
is >> arr.theArray[arr.num_entries++];
return is;
}
This is the classic C++ approach, but requires that you track the number of entries added from within your class.
Thank you!!
I have changed the code and header to complete that. Now it calls and changes perfectly. There is something wrong with my average and lowest values though. Any suggestions?
Not sure what your code looks like currently, so it's hard to say.
I was going to comment on this earlier, but there were so many things wrong, I didn't want to cloud the issue.
Lines 9-11: There is really no reason to declare highest, lowest, and average as members of the class. You want highest? Simply call get_highest(), etc. highest, lowest and average should be local members of the respective functions.
Note that there are trade-offs to consider here. If get_highest(), etc, are called millions of times, and the array does not change between calls, then it makes sense to compute the highest (etc) once and have a getter that only returns the stored highest value. In this case, you want to separate the logic into two functions.
If you only need to call get_average once, or the array changes frequently, then it makes sense not to store the average and compute the average each time it's needed.
It functions to access and change the values in the array but the lowest and average values it is outputting are wrong.
I have updated the code at the top