Thread: producing a matrix of 1's and 0's

  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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if you want only zeroes and 1 why do you insert something else?

    a[rowpos][colpos]=i;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  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 05: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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 01: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, 09:50 AM
  3. Help with adjacency matrix and scanf
    By hollywoodcole in forum C Programming
    Replies: 5
    Last Post: 03-30-2005, 04: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, 06:33 PM
  5. File to Matrix problem
    By bsimbeck in forum C Programming
    Replies: 3
    Last Post: 03-08-2003, 07:12 PM