producing a matrix of 1's and 0's

This is a discussion on producing a matrix of 1's and 0's within the C Programming forums, part of the General Programming Boards category; I am trying to produce a random matrix filled only with 1's and 0's but am struggling. I woudl appreciate ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    8

    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

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    if you want only zeroes and 1 why do you insert something else?

    a[rowpos][colpos]=i;
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,663
    You also need to read in a value for k before doing
    srand(k);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    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");
    	}
    }
    Last edited by emj83; 02-24-2009 at 04:06 AM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are not setting the board[i][j] to anything, are you?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,910
    You have three steps to your task:
    1. Reading in the dimensions of the matrix.
    2. Creating a matrix as a two dimensional array with the given dimensions.
    3. Populating the matrix with 0's and 1's in random order.

    It looks like you have step 1 done, but it wrote it such that it comes after step 2, and consequently you do not actually create your matrix with the desired size.

    Furthermore, I must inform you that you are using a variable length array. Are you sure you are supposed to use that? Maybe you are supposed to use malloc() and free(), or maybe you are supposed to create a two dimensional array of the maximum size allowed, and then use only part of that array as necessary.

    Either way, it would be a good idea to finish steps 1 and 2 before going on to step 3. (You can also do step 3 directly by "hard coding" the array size, but for now doing things step by step is probably better.)

    Also, note that you should declare main() as returning an int, and then return 0 at the end (though that return is optional according to the 1999 edition of the C standard, which you are probably compiling against to use variable length arrays). You should also indent your code more consistently.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit Manipulation in Matrix
    By DonFord81 in forum C Programming
    Replies: 9
    Last Post: 03-06-2009, 12:33 PM
  2. Counting 1's in a string of 1's and 0's
    By Friar in forum C Programming
    Replies: 1
    Last Post: 02-21-2006, 08:50 AM
  3. Help with adjacency matrix and scanf
    By hollywoodcole in forum C Programming
    Replies: 5
    Last Post: 03-30-2005, 03:31 PM
  4. Problems putting a text file into a matrix.
    By warny_maelstrom in forum C Programming
    Replies: 15
    Last Post: 01-22-2004, 05:33 PM
  5. File to Matrix problem
    By bsimbeck in forum C Programming
    Replies: 3
    Last Post: 03-08-2003, 06:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21