0

I want to create a multidimensional array with just two values : 0 or 1.

I use the srand/rand functions but array contains only 0. Here is the code :

#define NB_LINE 4
#define NB_COLUMN 11
int tab[NB_LINE][NB_COLUMN] ; // global variable
 void generate() {
 srand((unsigned int)time(NULL));
 int n, i, j;
 for(n = 0; n < NB_LINE*NB_COLUMN; ++n){
 do
 {
 i = rand() % NB_LINE;
 j = rand() % NB_COLUMN;
 }
 while (tab[i][j] != 0);
 tab[i][j] = 1 ;
 }
}

I don't know how to solve this problem ?

thanks !

Edit : Thanks for your answers. Do you think it's possible with rand() to have juste one "1" per column and others spots contain only 0 ?

asked May 9, 2011 at 22:21
5
  • Just to clarify: you want each spot in your array to randomly have a 1 or a 0? Commented May 9, 2011 at 22:26
  • You should tag the language your're using. I'm going to guess c++, but you're more likely to get an answer if you tag the language Commented May 9, 2011 at 22:30
  • I want to generate an array where each spot contains 0 or 1. Commented May 9, 2011 at 22:34
  • "column" is spelled with a "u". This is not mere pedanticism; poor spelling makes code harder to read, can lead to bugs, and has made it into APIs with quite unfortunate results (e.g., the notorious HTTP "referer" field). Commented May 10, 2011 at 0:21
  • "but array contains only 0" -- no, your code fills the array with 1's -- you search, 44 times, for a 0 and then change it to a 1. You could fill it with half 0's an half 1's by looping until n < NB_LINE*NB_COLOMN/2, but that would be the slow way to do it. Commented May 10, 2011 at 0:34

3 Answers 3

4

Your loop doesn't do what you think it does. Try this:

int tab[NB_LINE][NB_COLUMN] ; // global variable
void generate() {
 srand((unsigned int)time(NULL));
 int i, j;
 for (i=0; i < NB_LINE; ++i) {
 for (j=0; j < NB_COLUMN; j++) {
 tab[i][j] = rand() % 2;
 }
 } 
}

At the end of that, you will have an array where each spot randomly has a 1 or a 0.

answered May 9, 2011 at 22:32
Sign up to request clarification or add additional context in comments.

1 Comment

Please remove the unused variable n and fix the bad indentation (copied from the OP).
2

I want to create a multidimensional array with just two values : 0 or 1

You should take the % with 2 on a random number generated. With that hint given, try to make the program more simpler and easy to understand. Why don't you just fill each element of the array sequentially ?

answered May 9, 2011 at 22:32

1 Comment

Thanks for the hint, I fully understand now, my code was too complicated.
0

Edited question

Do you think it's possible with rand() to have just one "1" per column and others spots contain only 0?

Yes, of course.

Instead of looping over all lines and all columns setting each individual array element to a random value of 0 or 1 do initialize all array elements to 0 (if they aren't already); loop over all columns and choose 1 random line and set the corresponding array element to 1.

#include <stdlib.h>
#include <string.h> // memset
#include <time.h>
void generate(int *arr, size_t lines, size_t columns) {
 int col, row;
 memset(tab, 0, lines * columns * sizeof *arr);
 for (col = 0; col < columns; col++) {
 row = rand() % lines;
 tab[row * columns + col] = 1;
 }
}
int main(void) {
 int tab[NB_LINE][NB_COLUMN];
 srand(time(0)); /* no casts */
 generate(&tab[0][0], NB_LINE, NB_COLUMN);
 /* ... */
}

Notice I've removed the srand() from the generate() function. srand() should be called no more than once per program invocation (to guarantee maximum randomness). The best way to accomplish that is to call srand from main().
Also, instead of i and j, I named the variables row and col.
And, I've made tab a local variable rather than global. Avoid globals whenever possible: it's a favor you do yourself. With no globals the information is passed to the function through arguments.

answered May 13, 2011 at 8:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.