filling double dimensional arrays

This topic is 4650 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

can anyone tell me the logic ..how to fill double dimensional array with random numbers so that it may not contain duplicates in any row or column like 123 231 312

Share on other sites
Quote:
 Original post by TEUTONcan anyone tell me the logic ..how to fill double dimensional array with random numbers so that it may not contain duplicates in any row or column like123231312
That example isn't really random; it's just a series of cyclic permutations. Perhaps you could provide more info, such as whether the array will always be square, whether the range of the entries is restricted to the dimension (as in your example), whether they should always be integers (I assume so), and whether or not they need to be truly random, or can be a pattern like in the above case.

Share on other sites
1.Yes arrays will always be sqaure
2.Range of the entries is restricted to the dimension(1 TO N), N being the dimension
3.Yes they should always be integers.

Share on other sites
It depends upon exactly what you are doing. With your example you could simply generate it as you did then randomly swap entire rows and columns. Whether that is a valid solution depends upon whether you can say in advance what values will be present and all you need is for their precise location to be randomly selected.

Share on other sites
Simple. If you never use the same number twice at all, there will be no duplication in the columns nor in the rows.

Share on other sites
Quote:
 Original post by FrunySimple. If you never use the same number twice at all, there will be no duplication in the columns nor in the rows.
I think he said the range for the 'random' numbers needed to be restricted to the dimension of the array (may have misunderstood though).

Share on other sites
I'm smelling homework here. :p

Well, you could do something like this (it's like solving (or actually, creating) sudoku):

For each of your cells, create a list, named Possible. Also, create two global lists, History and Tried (they start out empty). Fill all the Possible lists with all your numbers (1-N). Then, randomize a number and a cell. If the number/cell pair can be found in the Tried list, or the number is not in the selected cell's possible list, rerandomize. Clear the possible list for that cell and add the number(and the cell where it was stored) to the History list. Loop through the row and column you added the number to, and remove the number from their Possible list. Repeat this process, adding random numbers to random cells. You might get a successful solution on the first run, but probably not. This is why you keep track of all additions in the History list. When you get in a dead-end, remove the last item from the History list and add it to the Tried list. This is like "going back" from a nonpossible solution. Finally you should have a grid that is fully filled.

* edited..

Share on other sites
Yes you can call it an assignment or homework. But I never asked for a code from you guys.. Just asking for logic. Thanks everyone especially clb

Share on other sites
Oh and as a side note, the algorithm described here is a "trial-and-error" one and doesn't perform very fast.. if you google around, there are more sophisticated algorithms (and better described than what I did) available for creating sudokus.

1. 1
Rutin
47
2. 2
3. 3
4. 4
5. 5

• 13
• 10
• 12
• 10
• 13
• Forum Statistics

• Total Topics
632996
• Total Posts
3009776
• Who's Online (See full list)

There are no registered users currently online

×