2011-04-13 83 views
1

我在處理沒有矢量的數組時做了很多不同的操作,我想知道是否有人可以幫助我在數組中移動元素並在用元素初始化新空間時擴展數組。我覺得我很接近完成這個代碼,但是我打了一個塊。更改陣列

#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; index < --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; 

} 
+0

它看起來像你的newArray變量裏面的shift應該是一個int指針,但考慮到你實際上並沒有在其他代碼中使用shift,它可能並不重要。你可能應該更新它。 – 2011-04-13 15:49:22

+0

究竟是什麼問題?你有任何錯誤?什麼不按預期工作?它以哪種方式不按預期工作? – sth 2011-04-13 15:49:22

回答

1

只有兩個編譯錯誤:int newArray;應該int* newArray;#include <cstring>缺少(必要memcpy()

此外,線display(myArray, SIZE);大概意思是display(arraPtr, SIZE);,同樣display2(myArray2, SIZE); - 否則你只是顯示原始數組,而不是函數調用的結果。

然而,這可從更安全,更通用的C受益++算法,std::copy()std::reverse_copy()至少:

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; 
} 

完整的程序:https://ideone.com/RNFiV

+0

在閱讀你之前,我正在編輯我的解決方案,但是我會發布它,因爲我不使用通用算法,所以我認爲從學習的角度來看,我的效果更好,當然你的實際代碼更好。 – AntonioMO 2011-04-13 16:03:19

+0

@machielo當然,考慮到問題陳述的模糊性,任何答案都可能是OP正在尋找的。 – Cubbi 2011-04-13 16:17:16

1

這主要是C代碼,但是我會盡力給你的,你在做什麼,不是語法的詳細方法的一些提示:

在反向功能,你從來沒有真正把任何東西進入新陣列。而不是在for循環中進行一些交換,你可以通過原始循環向後運行,將元素放入新數組中。

在展開函數中,它看起來像是在嘗試做兩件相反的事情,將輸入數組中的內存複製到新數組中,然後用全零覆蓋新數組。如果你想手動複製內存,你只需要循環將原始數組的值複製到新數組中(而不是通過原來數組的兩倍大小,否則你會走到最後! )。如果你想使用memcpy然後擺脫for循環。

我不確定你想要移位功能做什麼,但它現在幾乎只是複製數組。

+0

以及我發佈後的問題後,我改變了一些東西... – Shimar 2011-04-13 15:59:13

+0

@Shimar看起來像一些東西可能已經修復,但還有更多的工作要做! – DShook 2011-04-13 16:43:01

0

我不知道到底是什麼?你想完成但我認爲它是這樣的:

#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] << " "; 
    } 
} 

作爲一個方面說明,如果你有問題這種類型的東西你應該看看任何有關指針和指針算術的優秀C語言資源,當你必須執行低級C++代碼時,它會派上用場。