mercredi 17 juin 2015

why does rand() in this code always produce the same matrix

I would like to create a matrix which has all it's elements as either 1 or 0. This is what I've tried, it produces a matrix with 1s and 0s at random locations just as I want. However, it produces the same matrix everytime.

#include <stdio.h>
#include <stdlib.h>

int a[10][10];
int i = 0,j= 0,n=0;

int main(void)
{
 populate(5);
 print(5);

 return 0;
}

int populate(int n)
{
 for(i=0;i<n;i++)
 {
  for(j=0;j<n;j++)
  {
   a[i][j] = rand()%2;
  }
 }
}

int print(int n)
{
 for(i=0;i<n;i++)
 {
  for(j=0;j<n;j++)
  {
   printf("%d ",a[i][j]);
  }
  printf("\n");
 }
}




Aucun commentaire:

Enregistrer un commentaire