1

I'm studying for an exam in c++ and i have a question on the past papers

"Write a function in C++ that takes as input an array of doubles and the length of the array, and returns an array twice the length. The first half of the returned array should contain a copy of the contents of the original array. The second half of the returned array should contain the contents of the original array in reverse order."

"The function should have the following prototype: double *copy_and_reverse(double *a, int length);"

since im obviously new to c++ i got stuck in my solution, my code so far is:

double *copy_and_reverse(double *a, int length){
 double *b[length*2];
 for(int i=0;i<length;i++){
 *b[i]=a[i];
 }
 for(int i=length;i<length*2;i++){
 int w=length-1;
 *b[i]=a[w];
 w--;
 }
 return *b;
}
int main()
{
double nums[2]={1.23,5.364};
double *pnums=nums;
*pnums=*copy_and_reverse(pnums, 2);

I think i got the core of the method correct but i'm just stuck in the syntax of using pointers, any help is appreciated and if possible a reasoning behind it so i can learn for the exam.

asked Jan 14, 2014 at 19:10
10
  • You're dereferencing the elements of b without allocating memory for them. Commented Jan 14, 2014 at 19:11
  • You are unnecessarily dereferencing b everytime you use it, as well as pnums when you're assigning it. Look up tutorials about the usage of pointers, and primarily what dereferencing is used for. Also, your b array will be destroyed when your function returns, you should allocate it dynamically. Commented Jan 14, 2014 at 19:14
  • Look up dynamic memory allocation. That's what you'd want to use. Commented Jan 14, 2014 at 19:16
  • To begin with your professor is an idiot. The prototype copy_and_reverse(double *a, int length); doesn't take an array, it takes a pointer to the first element within the array. Commented Jan 14, 2014 at 19:16
  • To continue, did you get a compiler error? What exactly do you need help with? Commented Jan 14, 2014 at 19:17

4 Answers 4

1

You've got a few problems with this code.

First

double *b[length*2];

Here you're declaring an array of pointers to doubles. The array is of size length * 2, however, none of the pointers in this array are valid yet. This is probably not what you intended to do.

You want an array of doubles, of size length * 2. You can't return an array in C++ but you can return a pointer to some memory that contains an array of doubles.

Let's start by allocating enough memory for all those doubles

double *b= new double[length * 2];

In your first for loop you can treat result as an array

for(int i=0;i<length;i++){
 b[i]=a[i];
}

Here you're copying the values from a for each index i to be at the same index. I'll let you figure out how to fill in the reverse part for the second half of the array. You're on the right track, however you might want to think about doing it all in one loop ;)

Your return statement just needs to return your variable b, as it's already a double *.

return b;

An important thing to remember is that you're allocating memory in this function with new. You are responsible for deleting this when you're done with it. Also, when you allocate using new and [] you have to delete using [] as well.

delete [] b;

you can call your function just by de-referencing the first item in your array.

int main() {
 double nums[2]={1.23,5.364};
 double *pnums = copy_and_reverse(&pnums[0], 2);//don't forget to clean up pnums afterwards!
answered Jan 14, 2014 at 19:25
Sign up to request clarification or add additional context in comments.

1 Comment

very concise answer, much appreciated with regards to an exam.
1

There are quite many errors in your code. The major one is that you need to allocate new array of doubles. And return that array. I'd suggest compare this with your version line by line:

double *copy_and_reverse(double *a, int length){
 double *result = new double[length*2];
 for(int i=0;i<length;i++) {
 result[i]=a[i];
 }
 int r = length*2;
 for(int i=0; i < length;i++){
 result[--r]=a[i];
 }
 return result;
}

And your main() shall look like:

int main()
{
 double nums[2]={1.23,5.364};
 double *pnums = copy_and_reverse(nums, 2);
...
 delete[] pnums;
}
answered Jan 14, 2014 at 19:19

Comments

0

Ok, there are at least two problems with this:

double *b[length*2];

The first problem is that you are declaring a local array (of pointers), which you will then try to return:

return *b;

(You're returning the wrong thing here, too, but that's another story) You can't return a pointer to a locally-allocated thing because as soon as the function returns, the locally-allocated thing will be destroyed. Instead, given that you must return a pointer to the first element of an array, you have to dynamically allocate that thing using new.

Second, you can't declare an array like this using a length which s only known at runtime. But this problem will be obviated when you use new to dynamically allocate the array.

I would normally say that you shouldn't be doing any of this at all, and just use a std::vector -- but clearly a requirement of this assignment is to use a dynamically allocated C-style array. (Which I take great issue with your professor on.)

I would also say that the prototype:

double *copy_and_reverse(double *a, int length);

doesn't declare a function which takes an array, as your professor incorrectly asserts, but a function which takes a pointer to a double. That that pointer is the first element in an array doesn't magically make a an array. In short: an array and a pointer are not the same thing.

These last two observations are just for your benefit.

answered Jan 14, 2014 at 19:26

Comments

0

I assume this is not your homework and I am trying to help you out. Look at the comment of code.

 double *copy_and_reverse(double *a, int length)
 {
 double * b = new double[length*2]; //create a new array using new[]
 for(int i=0;i<length;i++){
 b[i]=a[i]; //addressing element with []
 }
 int w=length-1; //I assume this is what you want
 for(int i=length;i<length*2;i++){
 b[i]=a[w];
 w--;
 }
 return b;
 }
 int main()
 { 
 double nums[2]={1.23,5.364};
 double *pnums = copy_and_reverse(nums, 2);
 delete[] pnums;
 }

Also noted the memory is allocated in the function, so in the main, you want to delete it by using [].

answered Jan 14, 2014 at 19:27

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.