2014-10-26 64 views
0

所以我试图随机化1,2,3,4,5,6,7,8,9,10的数组。然后要求用户在数组中选择一个位置,并修改它。之后,它应该显示用户输入的数字全部的10个值。最后,它需要获得随机化的原始数组并将其逆转。随机化/修改数组

到目前为止,我有这个

#include <iostream> 

using namespace std; 
int array [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 
int rarray [10]; 

int main() { 
    cout << "Random Array = "; 
    for (int i = 0; i < 10; i++) { 
     int index = rand() % 10; 

     int temp = array[i]; 
     array[i] = array[index]; 
     array[index] = temp; 
    } 

    for (int i = 1; i <= 10; i++) { 
     cout << array[i] << " "; //somehow, this display needs to be entered into another array 
    } 

    system("PAUSE"); 
} 

但正如评论所说,我卡上,如何做到这一点。

+3

C++中的数组使用基于零的索引,而不是一个基于索引。 ['std :: shuffle'](http://en.cppreference.com/w/cpp/algorithm/random_shuffle)和['std :: reverse'](http://en.cppreference.com/w/cpp /算法/反向)是你的朋友。 – 2014-10-26 23:54:54

+0

请将问题的范围缩小到您遇到问题的特定部分。 – 2014-10-27 00:10:09

回答

2

您可以使用C++标准库中的std::shuffle,std::copystd::reverse来完成此操作。

#include <iostream> 
#include <algorithm> 

using namespace std; 

int main() 
{ 
    int position; 

    // Get the position to modify and make sure it's within our bounds. 
    do 
    { 
     cout << "Select a position: "; 
    } 
    while (!(cin >> position) || position < 0 || position > 9); 

    int array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 
    int rarray[10]; 

    // Shuffle the array. We use std::begin and std::end to get the bounds of 
    // of the array instead of guessing it's size. 
    std::random_shuffle(std::begin(array), std::end(array)); 

    // Copy it to the new array 
    std::copy(std::begin(array), std::end(array), rarray); 

    // Modify the new array and display it. Add your own code here to get the 
    // value it is modified with. 
    rarray[position] = 100; 

    for (auto value : rarray) 
     cout << value << " "; 
    cout << endl; 

    // Reverse the original array and display it 
    std::reverse(std::begin(array), std::end(array)); 

    for (auto value : array) 
     cout << value << " "; 
    cout << endl; 

    system("PAUSE"); 
} 

或者如果您不允许使用C++标准库,则需要手动处理所有内容。这是一个乏味的任务,但是为什么C++标准库应尽可能地被利用的一个很好的例子。这也更容易出错,更难以维护,而且看起来丑陋。

int main() 
{ 
    int position; 

    do 
    { 
     cout << "Select a position: "; 
    } while (!(cin >> position) || position < 0 || position > 9); 

    int array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 

    for (int i = 0; i < 10; i++) 
    { 
     int index = rand() % 10; 

     int temp = array[i]; 
     array[i] = array[index]; 
     array[index] = temp; 
    } 

    // Copy the array 
    int rarray[10]; 
    for (int i = 0; i < 10; i++) 
    { 
     rarray[i] = array[i]; 
    } 

    // Modify the new array and display it 
    rarray[position] = 100; 
    for (int i = 0; i < 10; i++) 
    { 
     cout << rarray[i] << " "; 
    } 
    cout << endl; 

    // Reverse the old array and display it 
    for (int i = 0; i < 10/2; i++) 
    { 
     int tmp = array[i]; 
     array[i] = array[9 - i]; 
     array[9 - i] = tmp; 
    } 
    for (int i = 0; i < 10; i++) 
    { 
     cout << array[i] << " "; 
    } 
    cout << endl; 

    system("PAUSE"); 
} 

两种实现接近原来的要求,但你可能需要在其上展开一点完全符合您的要求。他们应该让你很好地移动。

+0

如果我想打乱生产代码,我会推荐一个比rand()更好的伪随机数生成器。如果STL坚持使用rand(),我也推荐使用STL,或者从STL借用代码。 – Steve 2014-10-27 02:18:56

+0

这正是我所寻找的,谢谢。 PS,第二个代码块缺少'#include '和'using namespace std;':P – Cosmetify 2014-10-27 17:27:55