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.