Thread: value in random slot of 2d array?

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    27

    value in random slot of 2d array?

    This is a large never ending project .. but a piece at this point is to:

    currently I'm taking a random value from a text file.. There are 16 values grabbed and placed in a 2d array 4x4.

    Currently I am storing statically through the below if/else statement. What I would like to do is randomly store those 16 values in a 2d array.

    Code:
    while(!feof(fid))
    	{
    		fscanf(fid, "%s", line);
    		printf("%s\n", line);
    		r = rand()%6;
    		c = line[r];
    		printf(" %c %d\n", c,r);
    	        board[i][j]=c;
    
    //****** insert values to board ******************
    			if (i==3)
    			{
    				i=0;
    				j++;
    			}
    	
    			else
    				i++;
    		
    	}  // end while
    	
    // ************ output values of board ***************
    	for(i=0; i<4; i++)
    	{
    		 for(j=0; j<4; j++)
    		{
    			printf("%c\n",board[j][i]);
    		}
    	}
    Last edited by DJPlayer; 02-23-2010 at 08:48 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Not only can I not understand what you're trying to do, but I don't understand what it is you want help with.

    You didn't ask a question. Please do, and be as specific as possible. In your case, an example or two would be a great help.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    Quote Originally Posted by Adak View Post
    Not only can I not understand what you're trying to do, but I don't understand what it is you want help with.

    You didn't ask a question. Please do, and be as specific as possible. In your case, an example or two would be a great help.
    was worded rather poorly. edited to fix.

    I randomly grabbed 16 values from a text file. Now I want to that those 16 random letters and randomly place them in a 2d array. Currently I'm putting them in a set pattern vs using rand().

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by DJPlayer
    I randomly grabbed 16 values from a text file. Now I want to that those 16 random letters and randomly place them in a 2d array. Currently I'm putting them in a set pattern vs using rand().
    Looking at your code, it seems that your random selection involves selecting 1 of the first 6 characters in the current line. This means that someone who knows the contents of the file has about a 1/6 chance of correctly guessing the value of a given array element.

    What you can do is shuffle the 2D array: select an element at random from the array, then swap it with the first element (presumably board[0][0]). Then excluding the new first element, select an element at random from the array, then swap it with the second element (e.g., board[0][1]). Repeat for the third until the last element, each time excluding the elements in positions that have already been processed.
    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. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  2. Allocating a 2D Array Differently
    By pianorain in forum C++ Programming
    Replies: 13
    Last Post: 12-15-2005, 02:01 AM
  3. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Dynamic 2d array
    By Mithoric in forum C++ Programming
    Replies: 8
    Last Post: 12-29-2003, 09:19 AM