Need help using mutator function to set variables in a dynamically allocated array pointer.

Hi, I've been stuck on this problem for days. I am using a mutator member function to set variables to a dynamically allocated pointer array. When the display function is called, nothing is displayed, so I think the problem is with my setCell() member function. When debugging, the compiler gives an error on my setCell() mutator function saying that arrayPtr[index] is some garbage value. I have been dreading this problem all week and would greatly appreciate some help here...

Here's my NumberArray.cpp
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
#include "NumberArray.h"
#include <iostream>
#include <iomanip>

using namespace std;

NumberArray::NumberArray(int size)
{
	arrayPtr = new double[size];

	for (int i = 0; i < size; i++)
	{
		arrayPtr[i] = 0;
	}

}

NumberArray::~NumberArray()
{
	if (arrayPtr != nullptr)
	{
		delete[] arrayPtr;
	}
}

void NumberArray::setCell(int index, double value)
{
	arrayPtr[index] = value;
}

double NumberArray::getCell(int index) const
{
	return arrayPtr[index];
}

double NumberArray::getAverage() const
{
	double count = 0,
	       average;

	for (int i = 0; i < size; i++)
	{
		count = count + arrayPtr[i];
	}

	average = count / static_cast<double>(size);

	return average;
}

double NumberArray::getHighest() const
{
	double highestVal = 0;

	for (int i = 0; i < size; i++)
	{
		if (arrayPtr[i] > highestVal)
		{
			highestVal = arrayPtr[i];
		}
	}

	return highestVal;
}

void NumberArray::display() const
{
	for (int i = 0; i < size; i++)
	{
		cout << left << setw(5) << fixed << setprecision(2) << arrayPtr[i] << " ";
	}
}

void NumberArray::sort()
{
	bool swap;
	double temp;

	do
	{
		swap = false;
		for (int i = 0; i < (size - 1); i++)
		{
			if (arrayPtr[i] > arrayPtr[i + 1])
			{
				temp = arrayPtr[i];
				arrayPtr[i] = arrayPtr[i + 1];
				arrayPtr[i + 1] = temp;
				swap = true;
			}
		}
	} 
	while (swap);
}


And here is my NumberArrayDriver.cpp main program

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
#include "NumberArray.h"
#include <iostream>
#include <string>

using namespace std;

int getNumberOfValues();
int getIndex(int);
void display(NumberArray * arrayPtr, int size);
void getInput(NumberArray * arrayPtr, int size);
double newValue();

int main()
{
	string line(60, '-');
	int size = getNumberOfValues();

	NumberArray * arrayPtr = new NumberArray[size];

	getInput(arrayPtr, size);
	display(arrayPtr, size);

	arrayPtr[size].setCell(getIndex(size), newValue());

	delete[] arrayPtr;
	system("Pause");
	return 0;
}

int getNumberOfValues()
{
	int size;

	cout << "How many numbers do you want to store? ";
	cin >> size;

	while (size < 2 || size > 10)
	{
		cout << "!!!Error, size of index must be between 2 - 10 ";
		cin >> size;
	}

	return size;
}

int getIndex(int size)
{
	int index;

	cout << "Which number do you want to change? Enter an index ";
	cin >> index;

	while (index < 1 || index > size)
	{
		cout << "!!!Error, index must be in the range of 1 - " << size << " ";
		cin >> index;
	}

	return index;
}

void display(NumberArray * arrayPtr, int size)
{
	cout << "Here are the numbers in this array:\n\n";

	arrayPtr[size].display();
	arrayPtr[size].sort();

	cout << "After sort:\n\n";

	arrayPtr[size].display();

	cout << "The average of those numbers is: " << arrayPtr[size].getAverage() << endl;
	cout << "The highest of those numbers is: " << arrayPtr[size].getHighest() << endl;
}

void getInput(NumberArray * arrayPtr, int size)
{
	double number;

	cout << "Enter the " << size << " numbers:\n\n";

	for (int i = 0; i < size; i++)
	{
		cout << "Number " << i + 1 << " : ";
		cin >> number;
		arrayPtr[i].setCell(i, number);                   // this is the line I am sure is incorrect
	}
}

double newValue()
{
	double value;

	cout << "What is the new number? ";
	cin >> value;

	return value;
}
Last edited on
If size is five, for example, you've got this:

NumberArray * arrayPtr = new NumberArray[5];

This is an attempt to create 5 NumberArray objects using the default constructor, and from the code we can see it won't compile because there is no default constructor.

So where's the default constructor, and did you mean to make an array of five NumberArray objects, or are you trying to create a single NumberArray object of size 5?


arrayPtr[size].setCell(getIndex(size), newValue());
This is a nonsense. The last element in the array is element size-1 . There is no element size.
Last edited on
The assignment stated that the constructor needed a default argument of 5, which is what i used in the NumberArray.h header file in the default constructor. I am trying to make a single NumberArray object of a size the user would input. I suck at dynamically allocated pointers and thank you for the help. How should I better rework the code?
You need to create ONE NumberArray object. This code creates a single NumberArray object, of capacity size.

NumberArray the_numberArray_object(size);

Then set the five values in it:

1
2
3
4
5
6
7
8
for (int i = 0 ; i < size ; ++i)
{
  double someValue;
  cout << "What value for element " << i << "?" <<'\n';
  cin >> someValue;

  the_numberArray_object.setCell(i, someValue);
}



The thing is the object has to be a dynamically allocated pointer array. Your snippet creates a non pointer object that is not dynamically allocated. Am I missing something? Just to reclarify, I am trying to dynamically allocate a single pointer array object of a size. Also after changing my code to what you have, nothing is displayed when the display function is called, like there is a problem setting with the setCell assignment.
Last edited on
Just to reclarify, I am trying to dynamically allocate a single pointer array object of a size.

Array of what? What you say makes no sense. "single pointer array object" makes no sense.

The thing is the object has to be a dynamically allocated pointer array.
That makes no sense. An array is many objects. One object isn't an array. The NumberArray object does contain a dynamically allocated array of double. It does not contain an array of pointers. I see nowhere here that an array of pointers makes any sense.


NumberArray * arrayPtr = new NumberArray[size];
This is not an array of pointers. This creates an array of size NumberArray objects, and arrayPtr is a single pointer to the first one of them. This also makes little sense, given that I suspect you're meant to create a single NumberArray object.

What does the assignment actually say?
Last edited on
Here is what has been written to me...

Constructor should accept one integer as input argument (parameter). The input argument should be set to 5 by default. The constructor should then dynamically allocate the array to hold that many numbers using arrayPtr. Set all values of the dynamically allocated array to be 0.

Driver program should ask user to enter the total number of double values to enter, and the value of each number. The driver program should then sort, and display the average and highest of the numbers entered by the user.
Constructor should accept one integer as input argument (parameter). The input argument should be set to 5 by default.


Header:
1
2
3
4
5
6
7
8
class NumberArray
{
  double* arrayPtr; // This is the pointer that will point to the first element of the dynamically allocated array
  int m_size; // The object needs to know how big the array is

  public:
  NumberArray(int size = 5); // default size of 5
  ...



Source:
1
2
3
4
5
6
7
8
9
NumberArray::NumberArray(int size) // size will be five if this function is called without providing a size
{
        m_size = size;
	arrayPtr = new double[m_size];
	for (int i = 0; i < m_size; i++)
	{
		arrayPtr[i] = 0;
	}
}




Driver program should ask user to enter the total number of double values to enter, and the value of each number.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int numberToEnter;
cout << "How many?" << '\n';
cin >> numberToEnter;

NumberArray the_numberArray_object(numberToEnter);


for (int i = 0 ; i < numberToEnter ; ++i)
{
  double someValue;
  cout << "What value for element " << i << "?" <<'\n';
  cin >> someValue;

  the_numberArray_object.setCell(i, someValue);
}


The driver program should then sort, and display the average and highest of the numbers entered by the user.

I'll leave that for you.

There is nothing in those instructions involving an array of pointers. ONE pointer, that points to the first element of the dynamically allocated array, inside the NumberArray object.
Last edited on
It worked!! Thank You so much!!! I suck at programming and realize I sometimes have trouble explaining my situation.
That is unfortunate. If you're going to program, you need to get better at precisely understanding and precisely explaining. A program is a precise explanation to the machine of what you want it to do.
Topic archived. No new replies allowed.