Trouble with Array Class

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#ifndef mainH_H		
#define mainH_H		
#include <string>
using namespace 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"
using namespace 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 = new double[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!
Last edited on
Line 13: Why is theArray an int pointer? This should be a double pointer.

Lines 23-46: These are function declarations. They do not have bodies.

Lines 37,41,45,70,89,107: You can't return a string variable in a double typed function.

Line 60, 79, 105: You can't assign a double to a string.

Line 112: You want to allocate an array of doubles, not ints.

Line 135: This line is useless. You immediately overlay the value at line 138.

Line 139,149: theArray is private. You can't reference it here. Besides, this statement makes no sense.

Line 145,164,172,178: theArray is private. You can't reference it here.

Line 149: This line makes no sense.









Last edited on
I have changed and edited the recommended instances.

I have a questions for
Line 145,164,172,178: theArray is private. You can't reference it here.

How should I call that location then to redefine?
Line 145: Several ways to handle this.
1) Add a function to your class to prompt for input.
1
2
3
4
 
void mainH::input_number (int i)
{   cin >> theArray[i];
}

Not the best approach because it relies on external knowledge (i) of where to put the number in the array.

2) Add a setter function
1
2
3
void mainH::set_number (int i, double val)
{  theArray[i] = val;
}

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.

Lines 164,172,178:
Use a classic getter fuction
1
2
3
double mainH::get_element (int i) const
{  return theArray[i];
}

If you're ambitious, you can overload the [] operator to return the n'th element.

Last edited on
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.
1
2
void compute_average ();
double get_average () const;


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.


what else do you see that's wrong?

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
Last edited on
Line 18: theArray should be private.

Line 88: The if statement is not needed. count can never be >= numElements because of the terminal condition in the for statement.

Line 94: What happens if numElements is zero? Hint: You can't divide by 0.

Line 101: No checking on the validity of size (see line 94).

Line 140, 158,175,187: Those lines do nothing. They are function prototypes. Remove the type if you want to call the function.

Line 160, 178, 188: Since theArray should be private, use your getter to retrieve the value of the specified element.

Line 173: Again, since theArray should be private, you should use a setter here to change the value of an element.

Topic archived. No new replies allowed.