2 Replies - 196 Views - Last Post: 05 December 2012 - 06:46 AM Rate Topic: -----

#1 shadok  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 08-October 12

Finding Average of Variables Within Array

Posted 04 December 2012 - 07:10 PM

This should be extremely easy but I can't figure out my problem. I'm trying to create a member function within a class that will give me the average of the numbers I have stored within an array. Here's my code:

#include <iostream>
#include <string>
using namespace std;

class doubleArray {
private:
	double * _theArray;
	int _size;
public:
	doubleArray()
	{
		_size = 0;
		_theArray = new double [_size];

		_theArray[0] = NULL;
	}
	doubleArray(int size)
	{
		_size = size;
		_theArray = new double[_size];

		for (int i = 0; i < _size; i++)
		{
			_theArray[i] = 0;
		}
	}
	
	void setElement(double input, int i)
	{
		_theArray[i] = input;
	}
	
	double getElement(int element)
	{
		return _theArray[element];
	}
	double getAvg()
	{
		double sum = 0,
			   avg = 0;

		for (int i = 0; i < _size; i++)
		{
			sum = sum + _theArray[i];
		}
		
		avg = (sum / _size);
		return avg;
	}
	~doubleArray()
	{
		delete [] _theArray;
	}

};

void displayArr(doubleArray, int size);

int main()
{
	int arrSz;
	double input,
		avg;
	
	cout << "How many numbers do you want to store? ";
	cin >> arrSz;

	doubleArray arr(arrSz);

	cout << endl << "Enter the " << arrSz << " numbers:" << endl;

	for (int i = 0; i < arrSz; i++)
	{
		cout << "Number " << i << ": ";
		cin >> input;

		arr.setElement(input, i);
	}

	displayArr(arr, arrSz);

	avg = arr.getAvg();

	cout << endl << "The average of those values is " << avg << endl;
	
	system ("pause");
	return 0;
}

void displayArr(doubleArray a, int size)
{
	cout << endl << "Here are the values you entered:" << endl;

	for (int i = 0; i < size; i++)
	{
		cout << "Number " << i << " : " << a.getElement(i) << endl;
	}
}



Now here he the specific member function on its own:

double getAvg()
	{
		double sum = 0,
			   avg = 0;

		for (int i = 0; i < _size; i++)
		{
			sum = sum + _theArray[i];
		}
		
		avg = (sum / _size);
		return avg;
	}



And here is the function's call on its own:

avg = arr.getAvg();



I've gone through the debugger, and the issue is that the variable _theArray[i] is not giving me the correct values. Now that I've figured out that this is the issue, I can't manage to figure out why it's doing this.

Here is the output this code is giving:

Quote

How many numbers do you want to store? 3

Enter the 3 numbers:
Number 0: 1
Number 1: 2
Number 2: 3

Here are the values you entered:
Number 0 : 1
Number 1 : 2
Number 2 : 3

The average of those values is -2.65698e+303


As you can see, the values for the array are set, but seem to be incorrect when it comes to using them to find the average. It would be greatly appreciated if someone could help point out my error. Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Finding Average of Variables Within Array

#2 shadok  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 08-October 12

Re: Finding Average of Variables Within Array

Posted 04 December 2012 - 07:33 PM

Messing around some more, I've realized that putting my destructor into comments fixes this. Should I just take that out, or do I simply need to change it in some way? I'm relatively certain I need one, so I'm not quite sure what to do here.
Was This Post Helpful? 0
  • +
  • -

#3 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: Finding Average of Variables Within Array

Posted 05 December 2012 - 06:46 AM

First, explain what this is doing in your default constructor?
doubleArray() {
    _size = 0;
    _theArray = new double [_size];
    _theArray[0] = NULL;
}

Now on to your problem, void displayArr(doubleArray, int size) is giving you this problem. Note that, here you are passing the doubleArray by value, which means it gets copied and then used inside this function. But, you haven't specified a proper copy-constructor/assignment operator for doubleArray. Guess what? You have pointers stored inside doubleArray which were allocated by using new.
Remember that copy of doubleArray inside displayArr(doubleArray, int size)? When this function is done, the destructor gets called for this duplicated doubleArray and the internal allocated space is deleted.
But wait... don't you have those pointers still inside the original object? ;)

So, as a quick fix, pass the argument by reference:
void displayArr(doubleArray &a, int size);

But you should define a copy constructor and assignment operator as well, which deals with copying a doubleArray by allocating space and then deep-copying the values(not pointers).

Don't forget the rule of three, if you define one of these, you probably have to define the other two as well:
  • destructor
  • copy constructor
  • copy assignment operator

This post has been edited by Anarion: 05 December 2012 - 07:19 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1