<sy***************@yahoo.comwrote in message
news:11**********************@x35g2000prf.googlegr oups.com...
>I hope someone could correct this simple excerpt
since I fail to see how I can make rand() function works
in a loop manner
/////////////////////////////////
srand(time(NULL));
int r1=rand()%10+1;
for(int o=0;o<r1;++o){
int r2=rand()%10+1;
for(int p=0;p<r2;++p)
{
put_to_array[o][p]=rand()%10;
}
}
////////////////////////////////
when I call the function having this code excerpt,
a row of the 2d matrix put_to_array[][]
always has the same elements as others
i.e
1 2 4 5
1 2 4 5
1 2 4 5
You must be outputting it wrong. The output of one run of the following
program for me is:
9 9 2 6 0 8 0 6 8 0 0 0
9 1 4 7 0 1 0 0 0 0 0 0
6 5 5 0 0 0 0 0 0 0 0 0
5 9 6 0 3 9 8 0 3 1 0 0
7 0 8 0 0 0 0 0 0 0 0 0
8 7 4 4 2 1 0 0 0 0 0 0
8 6 7 4 9 8 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
int put_to_array[12][12] = {0};
std::srand(std::time(NULL));
int r1 = std::rand()%10+1;
for(int o=0;o<r1;++o){
int r2 = std::rand()%10+1;
for(int p=0;p<r2;++p)
{
put_to_array[o][p] = std::rand()%10;
}
}
for ( int i = 0; i < 12; ++i )
{
for ( int j = 0; j < 12; ++j )
std::cout << put_to_array[i][j] << " ";
std::cout << "\n";
}
std::cout << "\n";
return 0;
}