# producing a matrix of 1's and 0's

• 02-23-2009
emj83
producing a matrix of 1's and 0's
I am trying to produce a random matrix filled only with 1's and 0's but am struggling. I woudl appreciate anyones advice!

This is my code so far:
insert
Code:

```#include <stdio.h> #include <stdlib.h> main(){         int a[10][10]={0};                 int n,p, noofelements, randomno, rowpos, colpos;         int i,j,k;         srand(k);                 printf("Enter the seed: ");         scanf("%d", &k);         printf("Enter the number of rows: ");         scanf("%d", &p);         printf("Enter the number columns: ");         scanf("%d", &n);         noofelements=n*p;         for (i=1; i<=noofelements; i++)         {                 rowpos=rand()%n;                 colpos=rand()%p;                 a[rowpos][colpos]=i;         }         for (i=0; i<p; i++)         {                 for (j=0; j<n; j++)                         printf("%d\t", a[i][j]);                 printf("\n");         } }```
Emma
• 02-23-2009
vart
if you want only zeroes and 1 why do you insert something else?

a[rowpos][colpos]=i;
• 02-23-2009
Salem
You also need to read in a value for k before doing
srand(k);
• 02-24-2009
emj83
I am new to C programming and am finding it very difficult! I realise where I have gone wrong above, but still can't get a matrix with 0's and 1's in. now it just has weird numbers in. can anyone give me a hint?

Thanks Emma

insert
Code:

``` #include <stdio.h> #include <stdlib.h> main(){         int n,p, rowpos, colpos;         int i,j;         int board[p][n];         printf("Enter the dimensions of the board , separate them with a comma:\n");         scanf("%d,%d", &p,&n);                         for (i=0; i<p; i++)         {         for (j=0; j<n; j++)         printf("%d\t", board[i][j]);         printf("\n");         } }```
• 02-24-2009
matsp
You are not setting the board[i][j] to anything, are you?

--
Mats
• 02-24-2009
laserlight