2011-04-13 57 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++代码时,它会派上用场。