Function that gets an array from pointers using Dynamic Memory Allocation

The instructions from my textbook are:
Write a function int* read_data(int& size) that reads data from cin until the user terminates input by entering Q. The function should set the size reference parameter to the number of numeric inputs. Return a pointer to an array on the heap. That array should have exactly size elements. Of course, you won’t know at the outset how many elements the user will enter. Start with an array of 10 elements, and double the size whenever the array fills up. At the end, allocate an array of the correct size and copy all the inputs into it. Be sure to delete any intermediate arrays.

My professor's instructions are:
1. Your program should prompt the user for the following sequence of numbers
1.3 4 5.2 16.3 9.99 7.21 4.5 7.43 11.21 12.5
to fill out an array (10).
Note: Please print the input stream
2. Then, prompt the user for another 5 numbers as following
1.5 4.5 9.5 16.5 7.5 11.5
Note: Please print all 15 members of your final array.

3. You should use the Dynamic Memory Allocation (DMA) technique to implement this exercise

I am having a lot of trouble understanding how pointers work, that is why I deviated a bit from the textbooks instructions and focused on my professors instruction. I have the function down, the only problem I am having is that when I copy the input from the first pointer into the second pointer, they all come out as memory addresses instead of the actual values.

Here is my function:

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
double* read_data(double& size)
{
	double* data_array = new double[10];
	double* bigger_array = new double[15];	

	cout << "Please enter 10 values for your array\n";
	for (int i = 0; i < 10; i++)
	{
		cin >> data_array[i];
		size++;
	}
	cout << "The first values of the array you entered are:\n";
	for (int i = 0; i < size; i++)
	{
		if (i < size - 1)
		{
			cout << data_array[i] << " | ";
		}
		else
		{
			cout << data_array[i] << endl;
		}
	}
	cout << "Would you like to enter more values for your array? (Y/N): ";
	string answer;
	cin >> answer;
	if (answer == "Y" || answer == "y")
	{
		cout << "Please enter 5 more values for your array:\n";
		delete[] data_array;
		data_array = bigger_array;

		for (int i = 10; i < 15; i++)
		{
			bigger_array[i] = data_array[i];
			cin >> bigger_array[i];
			size++;
		}
		cout << "The new values of the array you entered are:\n";
		for (int i = 10; i < 15; i++)
		{
			if (i < size - 1)
			{
				cout << bigger_array[i] << " | ";
			}
			else
			{
				cout << bigger_array[i] << endl;
			}
		}
		return bigger_array;
	}
	else
	{
		return data_array;
	}
}


& this is my main function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
	double size = 0;
	double* data = read_data(size);
	cout << "\nThe size of your array is: " << size << endl;
	cout << "The final values of your array are:\n";
	for (int i = 0; i < size; i++)
	{
		if (i < size - 1)
		{
			cout << data[i] << " | ";
		}
		else
		{
			cout << data[i] << endl;
		}
	}
	system("pause");
	return 0;
}


I can't figure out what's wrong with my code. I feel that it is because my understanding of pointers or DMA might be wrong.
Of course, you won’t know at the outset how many elements the user will enter. Start with an array of 10 elements, and double the size whenever the array fills up. At the end, allocate an array of the correct size and copy all the inputs into it. Be sure to delete any intermediate arrays.
You don't appear to be following instructions.

Your code won't cope with 20 inputs, nor does it seem to check for Q.
Last edited on
I chose to follow my professors instruction, I have another version of the function the way that the textbook says to do 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
int* read_data(int& size)
{
	string input = "";
	size = 0;
	int capacity = 1; 
	int* data = new int[1];
	cout << "Please enter values for your array, Q to quit :\n";
	cin >> input;
	while (input != "Q" && input != "q")
	{
		data[size] = atoi(input.c_str());
		size++;
		if (size == capacity)
		{
			capacity *= 2;
			int* bigger_array = new int[size];
			for (size_t i = 0; i < size; i++)
			{
				bigger_array[i] = data[i];
			}
			delete[] data;
			data = new int[capacity];			
			for (size_t i = 0; i < size; i++)
			{
				data[i] = bigger_array[i];
			}
			delete[] bigger_array;
		}
		cin >> input;
	}
	int* bigger_array = new int[size];
	for (size_t i = 0; i < size; i++)
	{
		bigger_array[i] = data[i];
	}	
	delete[] data;
	data = bigger_array;
	return data; 
}


But this version uses a string, so that I can use the atoi function. This is not how my professor wants us to write the code, he wants us to use decimal values, so I have to use either a float or a double.
First post: double* read_data(double& size)
1
2
  delete[] data_array;
  data_array = bigger_array;
All previous input is lost. You need to copy the values from data_array into bigger_array first.

Forget the textbook and follow your professor only, he/she makes it easier.
Why do you complicate it even more?
It doesn't say anything about outputting '|', not asking if the user wants to input more.
1
2
3
4
5
6
7
  cout << "Would you like to enter more values for your array? (Y/N): ";
  string answer;
  cin >> answer;
  if (answer == "Y" || answer == "y")
  {
     cout << "Please enter 5 more values for your array:\n";
//... 

could be just cout << "Please enter 5 more values for your array:\n";

The whole thing in pseudo code

Allocate array for 10 numbers called org_array.
Let the user enter 10 numbers and store them in org_array.
Print the numbers in org_array.

Create a new array called new_array with size 15
Copy org_array to new_array.
Delete org_array.
Let the user input 5 more numbers into new_array.
Finally print new_array and return it to main.

In main delete the array.


Thank you for your help, I tried following your pseudo code as best as I could

Here is what I got for my function

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
double* read_data(int& size)
{
	double* data = new double[10];		
	cout << "Please enter 10 values for your array\n";
	for (int i = 0; i < 10; i++)
	{
		cin >> data[i];
		size++;
	}		
	cout << "The values you entered are:\n";
	for (int i = 0; i < 10; i++)
	{
		if (i < size - 1)
		{
			cout << data[i] << " | ";
		}
		else
		{
			cout << data[i] << endl;
		}
	}	
	double* bigger_array = new double[15];
	for (int i = 0; i < size; i++)
	{
		bigger_array[i] = data[i];
	}
	delete[] data;
	cout << "Please enter 5 more values for your array:\n";
	for (int i = 0; i < 5; i++)
	{
		cin >> bigger_array[i];		
		size++;
	}
	cout << "The other values you entered are:\n";
	for (int i = 10; i < size; i++)
	{
		if (i < size - 1)
		{
			cout << bigger_array[i] << " | ";
		}
		else
		{
			cout << bigger_array[i] << endl;
		}
	}	
	return bigger_array;	
}


This is my main function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
	int size = 0;
	double* data = read_data(size);
	cout << "\nThe size of your array is: " << size;	
	cout << "\nThe final values of your array are:\n";
	for (int i = 0; i < size; i++)
	{
		if (i < size - 1)
		{
			cout << data[i] << " | ";
		}
		else
		{
			cout << data[i] << endl;
		}
	}	
	delete[] data;

	system("pause");
	return 0;
}


However, I am still having the same problem, the other 5 inputs are only showing the memory address instead of the actual values. I am not seeing what's wrong with my code. Any suggestions?
Lets first assume that input follows this pattern:
1
2
3
4
5
size = 0;
double input {};
while ( std::cin >> input ) {
  fubar[size++] = input;
}

The question is, is the fubar large enough?

How were we supposed to make the fubar?
1
2
3
4
5
6
7
8
9
10
11
size_t capacity = 10;
double* fubar = new [capacity];
// other code
while ( std::cin >> input ) {
  if ( capacity <= size ) {
    // expand fubar

    // assert: size < capacity
    // assert: capacity == 2 * old capacity
  }
  fubar[size++] = input;


When all input has been received, you are supposed to create and fill:
1
2
3
4
double* result = new double [size];
// copy data from fubar to result
// deallocate fubar
return result;
1
2
3
4
5
for (int i = 0; i < 5; i++)
{
  cin >> bigger_array[i];		
  size++;
}

bigger_array[0] - bigger_array[9] contain the first input so you override them.
Thank you for all your help! Really appreciate it
Topic archived. No new replies allowed.