开发者

Changing around arrays

I'm doing a lot of different things with manipulating arrays without vectors, I was wondering if any one could help me with shifting elements in an array and expanding an array while initializing the new space with elements. I feel like I'm very close to completing this code, but I'm hitting a block.

#include <iostream>
using namespace std;


// Function prototypes
int *reverse(int *, int);
int *expand(int *, int); 
int *shift(int *, int); 
void display(int[], int);
void display2(int[], int);
void display3(int[], int);


int main()
{
    int const SIZE = 5;
    int myArray [SIZE] = {1, 2, 3, 4, 5};
    int myArray2 [SIZE] = {1, 2, 3, 4, 5};
    int myArray3 [SIZE] = {1, 2, 3, 4, 5};

    int *arraPtr;
    int *arraPtr2;
    int *arraPtr3;

    arraPtr = reverse(myArray, SIZE);

    display(myArray, SIZE);

    arraPtr2 = expand(myArray2, SIZE);

    display2(myArray2, SIZE);

    arraPtr3 = shift(myArray3, SIZE);

    display3(myArray3, SIZE);

    delete [] arraPtr;
    delete [] arraPtr2;
    delete [] arraPtr3;


    return 0;
}



int *reverse(int *arr, int size)
{
    int *copyArray;
    int posChange;

    if( size < 0)
        return NULL;

    copyArray = new int[size];

    for (int index = 0; inde开发者_如何转开发x < --size; index++)
    {
            posChange = arr[index];
            arr[index] = arr[size];
            arr[size] = posChange;

    }
    return copyArray;

}


int *expand(int *arr, int size)
{
    int *newArray;

        newArray = new int[size * 2];
memcpy( newArray, arr, size * sizeof(int));
for (int index = size; index < (size*2); index++)
    newArray[index] = 0;
return newArray;




}

int *shift(int *arr, int size)
{
    int *newArray;
    newArray = arr;
    newArray = new int [size + 1];
    for (int index = 5; index > 0; index--)
        newArray[index] = newArray[index - 1];

return newArray;


}

void display(int arr[], int size)
{
    for (int index = 0; index < size; index++)
    {
        cout << arr[index] << " ";
    }

        cout << endl;
}

void display2(int arr[], int size)
{
    for (int index = 0; index < size; index++)
    {
        cout << arr[index] << "  ";
    }
        cout << endl;

}

void display3(int arr[], int size)
{
    for (int index = 0; index < size; index++)
    {
        cout <<arr[index] << "  ";
    }
        cout << endl;

}


There are only two compile error: int newArray; should be int* newArray; and #include <cstring> is missing (necessary for memcpy())

Also, the line display(myArray, SIZE); was probably meant to be display(arraPtr, SIZE); and likewise display2(myArray2, SIZE); -- otherwise you're only displaying the original arrays, not the results of your function calls.

However, this could benefit from the safer and more generic C++ algorithms, std::copy() and std::reverse_copy() at least:

int *reverse(int *arr, int size)
{
    int *copyArray = new int[size];
    std::reverse_copy(arr, arr+size, copyArray);
    return copyArray;
}
int *expand(int *arr, int size)
{
    int *newArray = new int[size * 2]();
    std::copy(arr, arr+size, newArray);
    return newArray;
}
int *shift(int *arr, int size)
{
    int* newArray = new int [size + 1]();
    std::copy(arr, arr+size, newArray+1);
    return newArray;
}

full program: https://ideone.com/RNFiV


This is mostly C code but I'll try to give you some tips on the methods of what you're doing more than the syntax:

In your reverse function, you never actually put anything into the new array. Instead of doing some swapping in the for loop, you can just run through the original loop backwards putting the elements into the new array.

In the expand function it looks like you're trying to do two opposite things, copy the memory from the input array to the new one and then overwrite the new array with all zeros. If you want to copy the memory manually you need to have the loop only go the original array copying its values into the new array (and not go through double the size of the original array or else you'll walk off the end of it!). If you want to use memcpy then get rid of the for loop.

I'm not sure what you want the shift function to do but it pretty much just copies the array now.


I don't know exactly what did you want to accomplish but I think it was something like this:

#include <iostream>
#include <cstring> // Needed to compile on most compilers(memcpy), dunno in yours
using namespace std;


// Function prototypes
int *reverse(int *, int);
int *expand(int *, int); 
int *shift(int *, int); 
void display(int[], int);
void display2(int[], int);


int main()
{
    int const SIZE = 5;
    int myArray [SIZE] = {1, 2, 3, 4, 5};
    int myArray2 [SIZE] = {1, 2, 3, 4, 5};
    int myArray3 [SIZE] = {1, 2, 3, 4, 5};

    int *arraPtr;
    int *arraPtr2;

    arraPtr = reverse(myArray, SIZE);

    display(arraPtr, SIZE);

    arraPtr2 = expand(myArray2, SIZE);

    display2(arraPtr2, SIZE * 2);

    delete [] arraPtr;
    delete [] arraPtr2;


    return 0;
}



int *reverse(int *arr, int size)
{
    int *copyArray;
    int posChange;

    if( size < 0)
        return NULL;

    copyArray = new int[size];

    for (int index = 0; index <= --size; index++)
    {
            posChange = arr[index];
            copyArray[index] = arr[size];
            copyArray[size] = posChange;

    }
    return copyArray;

}


int *expand(int *arr, int size)
{
    int *newArray;

    newArray = new int[size * 2];
    memcpy( newArray, arr, size * sizeof(int));
    for (int index = size; index < (size*2); index++)
        newArray[index] = 0;
    return newArray;
}

int *shift(int *arr, int size)
{
    int *newArray;
    newArray = new int [size + 1];
    memcpy( newArray, arr, size * sizeof(int));


return newArray;


}

void display(int arr[], int size)
{
    for (int index = 0; index < size; index++)
    {
        cout << endl << arr[index] << " ";
    }
}

void display2(int arr[], int size)
{
    for (int index = 0; index < size; index++)
    {
        cout << arr[index] << " ";
    }
}

As a side note, if you have problems with this kind of stuff you should take a look at any good C resource that talks about pointers and pointer arithmetics, it will come in handy when you have to do low level C++ code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜