2013-03-12 127 views
-1

我尝试将随机函数用于不同的事情。 现在它的工作原理,但我得到一个问题是我想在我的代码中随机生成F,但他们需要成为后面的数组。此外,我需要使用它,如果我需要做1 1我认为我的代码将太长,混乱。 你知道我该怎么做?C++ Random&Array

#include <iostream> 
#include <ctime> 
#include <cstdlib> 
using namespace std; 
int main(){ 
    srand(time(0)); 
    //random numbers 1 to 10 for Time: 
    int t = rand() % 10 + 1 ; 
    cout << "There is "<< t << " Time; 
    int* F1 = new int [t]; 
    int* F2 = new int [t]; 
    int* F3 = new int [t]; 
    int* F4 = new int [t]; 
    int* F5 = new int [t]; 
    cout << "Time per F: 0 Not available, 1 available; 
    //For F1 
    for(int i = 0; i < t; i++){ 
     //random numbers 0 or 1: 
     F1[i] = rand() % 2 ; 
    } 
    cout << "The Time for F1 is "; 
    for(int a = 0; a < t; a++){ 
     cout << " "<< F1[a] <<" "; 
    } 
    //For F2 
    for(int j = 0; j < t; j++){ 
     //random numbers 0 or 1: 
     F2[j] = rand() % 2 ; 
    } 
    cout << "The Time slot for F2 is "; 
    for(int b = 0; b < t; b++){ 
     cout << " "<< F2[b] << " "; 
    } 
    return 0; 
} 

谢谢

编辑:通过该解决方案,你给我的帮助,以找到解决方案 我做诠释F [EM] [T]

+2

'cout <<“Time per F:0不可用,1可用;'此行语法错误编辑:此代码中有超过1个语法错误。 – mostruash 2013-03-12 04:18:20

+0

WTB接近报价。 – WhozCraig 2013-03-12 04:27:45

+0

欢迎来到StackOverflow。请不要提出新的问题作为编辑。如果答案帮助你解决了所述的问题,[请将其标记为已接受](http://stackoverflow.com/faq/#howtoask)并提出一个新的(特定的)问题。如果没有答案有帮助,那么你需要改变你的问题(参见[编写完美问题](http://tinyurl.com/so-hints)以获得提示)。 – Johnsyweb 2013-03-12 04:57:17

回答

0

由于您使用C++,并有机会获得标准库,这可以写成更清洁更安全的std::vector

#include <iostream> 
#include <ctime> 
#include <cstdlib> 
#include <iostream> 
#include <vector> 

using namespace std; 
int main() 
{ 
    srand(time(0)); 

    //Random number from 3 to 7 
    int numberOfVectors = (rand() % 7) + 3; 

    //Random number from 1 to 10. 
    int size = (rand() % 10) + 1; 

    std::cout << "There are " << numberOfVectors << " vectors." << std::endl; 
    std::cout << "Each vector has " << size << " elements." << std::endl; 

    std::vector< std::vector<int> > vectorOfVectorOfInt; 

    std::cout << "Value per element: 0 = Not available, 1 = available" << std::endl; 

    for(int vec = 0; vec < numberOfVectors; vec++) 
    { 
     //Create a new vector with 'size' elements. 
     std::vector<int> newVector(size); 

     for(int i = 0; i < size; i++) 
     { 
      //Generate a random value between 0 and 50 
      newVector[i] = (rand() % 50); 
     } 

     //Add the vector to our vector-of-vectors. 
     vectorOfVectorOfInt.push_back(newVector); 

     std::cout << "The values for Vector #" << (vec+1) << " is:"; 
     for(int b = 0; b < size; b++) 
     { 
      int value = vectorOfVectorOfInt[vec][b]; 
      std::cout << "\t" << value; 
     } 
     std::cout << std::endl; 
    } 

    return 0; 
} 

您可以运行它,看看结果这里:http://ideone.com/RWQhjO

0

使用数组的数组:

#include <ctime> 
#include <cstdlib> 
using namespace std; 
int main(){ 
    srand(time(0)); 
    //random numbers 1 to 10 for Time: 
    int t = rand() % 10 + 1 ; 
    cout << "Time per F: 0 Not available, 1 available"; 
    const int f_num = 5; 
    cout << "There is "<< t << "Time"; 
    int* F = new int*[f_num]; 
    for(int i = 0; i < f_num; i++){ 
     F[i] = new int[t]; 
     cout << "The Time for F"<<i<<" is :"; 
     for(int j = 0; j < t; j++){ 
      //random numbers 0 or 1: 
      F[i][j] = rand() % 2 ; 
      cout << " "<< F1[i][j] <<" "; 
     } 
    } 
} 

顺便说一句,你的代码是C-等。在C++中,我们通常使用std::vector而不是普通数组,而使用http://en.cppreference.com/w/cpp/numeric/random而不是rand()。另外,请勿使用using namespace std。这可能会导致名称冲突。

+0

当我这样做时,我有这个错误 't'不能出现在一个常量表达式 对于这一行:int ** F = new int [f_num] [t]; – usertfwr 2013-03-12 04:23:01

+0

很奇怪。让我想一想:S – 2013-03-12 04:25:26

+0

'int ** F = new int [f_num] [t];'?你不能使用标准容器吗? – Johnsyweb 2013-03-12 04:26:00

0

我尝试将随机函数用于不同的事情。

大概是随机的东西。

现在,它的工作原理,但我得到一个问题是我想在我的代码中随机生成F,但他们需要成为数组之后。

您可以生成此值,就像您为t生成值一样。

const int number_of_fs = rand() % 10 + 1; 
std::cout << "Working with " << number_of_fs << " Fs" << std::endl; 

此外,我需要使用它,如果我需要在1去做1我觉得我的代码过长而凌乱。你知道我能做到吗?

您的代码已经太长,乱糟糟,并有内存泄漏。您应该使用标准Containers library中的容器,而不是指向从未获得delete d的指针。

这里是你的代码的外观是你使用std::vectorstd::map A S例如:

#include <cstdlib> 
#include <ctime> 
#include <iostream> 
#include <iterator> 
#include <map> 
#include <vector> 

int main(){ 
    srand(time(0)); 
    //random numbers 1 to 10 for Time: 
    const int t = rand() % 10 + 1; 
    std::cout << "There is "<< t << " Time" << std::endl; 
    const int number_of_fs = rand() % 10 + 1; 
    std::cout << "Working with " << number_of_fs << " Fs" << std::endl; 

    std::map<int, std::vector<int> > f; 

    std::cout << "Time per F: 0 Not available, 1 available" << std::endl; 

    for (int f_slot = 1; f_slot <= number_of_fs; ++f_slot) { 
     for(int i = 0; i < t; i++){ 
      //random numbers 0 or 1: 
      f[f_slot].push_back(rand() % 2); 
     } 
     std::cout << "The Time for F" << f_slot << " is " << std::endl; 
     std::copy(f[f_slot].begin(), f[f_slot].end(), 
      std::ostream_iterator<int>(std::cout, " ")); 
     std::cout << std::endl; 
    } 
} 

See it run

当然,这些容器的内容的填充和印刷可能应该分解成单独的功能,但我不想重新编写代码完全