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.

Ben
  • 2,518
  • 4
  • 18
  • 31
  • You're dereferencing the elements of `b` without allocating memory for them. – chris Jan 14 '14 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. – Jukurrpa Jan 14 '14 at 19:14
  • Look up [dynamic memory allocation](http://stackoverflow.com/questions/2672085/c-static-array-vs-dynamic-array). That's what you'd want to use. – B. M. Knecht Jan 14 '14 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. – John Dibling Jan 14 '14 at 19:16
  • To continue, did you get a compiler error? What exactly do you need help with? – John Dibling Jan 14 '14 at 19:17
  • @user3168667 i do need to look up memory allocation for a understanding of this language thank you. – Ben Jan 14 '14 at 19:21
  • @JohnDibling I'm not sure why he gave us that prototype but it could possibly there to test as on the syntax of c++, i'm not sure. I dont get a complier error. In eclipse it runs and windows gives a close program error everytime. – Ben Jan 14 '14 at 19:23
  • First, of course, the code shouldn't even compile, since you're declaring a local array with a non-constant size. – James Kanze Jan 14 '14 at 19:46
  • @JohnDibling And it's not the way you'd declare the function in C++ either. The standard way of doing this in C++ would be `std::vector copy_and_reverse( std::vector const& original )`. – James Kanze Jan 14 '14 at 19:47
  • @JamesKanze: Yep, I mentioned that in my answer. – John Dibling Jan 14 '14 at 19:48

4 Answers4

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;
}
c-smile
  • 26,734
  • 7
  • 59
  • 86
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!
Boumbles
  • 2,473
  • 3
  • 24
  • 42
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.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
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 [].

user3195397
  • 226
  • 1
  • 2