Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
/* A function that reads a sequence of integers from input (with the length of sequence, followed b
For example, if the length is 3, and the numbers are
123 345 99 then the array returned will be of size 3, and stores values 123, 345, and 99
@param length: upon return, stores the length/size of the array
@return the pointer pointing to the array
*/

int * ReadNumberSequence (int & size)
{
int * array = NULL; // initialize the pointer to NULL
do {
cout <<"Enter the length of the number sequence:";
cin >> size;
} while (size<=0);

// Todo: Write a statement to allocate memory for the array.
// Note: we only know the value of size at run time, so we need to DYNAMICALLY
// allocate memory for this array

// Todo: write a loop to read size # of int from input, and save them to the //array

array = new int [size];

for (int i = 0; i < size; i++)
{
cin >> array[i];
}

return array;
}

int main()
{
// i'm not sure what to do here
// Todo: delcare necesssary variables

//Todo: call the ReadNumberSequence function to read a sequence of numbers


//Todo: write a loop to


display the elements in the array returned ...
//Todo: free the array returned by ReadNumberSequence.

}

What I have tried:

I have completed the dynamic array fuction called int * ReadNumberSequence, but I'm stuck and don't know what to write in the main function.
Posted
Updated 5-Oct-18 22:04pm

Do yourself a favour and take out the now-redundant TODO comments: if you have done it, you don't need a reminder to do it again!

But ... we do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think! And what you have left is the really easy stuff, most of which you have already done before: you know how to "delcare necesssary variables" - you have to decide what variables you need is all.
You know how to call a function - that was probably covered very recently in your course.
You know how to write a loop.
You know how to print values.
You have probably been told very recently how to delete dynamically created data, but if you can't remember, look in the documentation: operator delete[] - C++ Reference[^]

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
In the main you should only do some stuff to call your function like:
C++
int size = 0;
int poinster = ReadNumberSequence(size);
and the rest of your homework task.

Really simple. Use printf for output.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900