By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,480 Members | 1,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,480 IT Pros & Developers. It's quick & easy.

8 RANDOM queens.

P: 8
Since this is my first post, allow me to introduce myself. My name is Angel, and I'm a 3rd year student at Queens College. I currently am taking CS211, which is the second level of C++, CS212 which is the intro to Java, CS240 which is Assembler, and Math 120 which is Discrete Math. I'm a fairly quick learner, and haven't had much trouble learning all of the commands and keywords, etc. However, this semester is a bit intense, possibly because of the caliber of problems we're tackling and whatnot. Anyway;

So in my C++ class we're taking on the 8 queens problem. However, in my lab class, the TA wants us to do a certain variant on the problem where we write a program that will place 8 queens RANDOMLY on the board using a 2D array.

I understand that we're not allowed to post whole source codes (though unsure why), so I'm just going to post the for-loop I have thus far;
Expand|Select|Wrap|Line Numbers
  1. for (col=0; col<8; col++)
  2. {
  3.     for (row=0; row<8; row++)
  4.     {
  5.         b[row][col] = rand()%8;
  6.     }
  7. }
However, this function simply places a random number between 0 and 7 onto the 8x8 array.

The TA wants us to place 8 queens (1 queen per column) and symbolize the queen with a 1, and an empty space with a 0.

I'm thinking that the random function should place a 1 on a random row in each column, and I tried doing so using temp values, but instead, I just get 64 0's.
I'm not exactly sure how I should approach that method of thinking, and any help is greatly appreciated.


PS - Sorry for the long post!
Sep 23 '07 #1
Share this Question
Share on Google+
2 Replies

Expert 2.5K+
P: 3,652
Loop through each row, generate a random number, and use that for the column. Then set your row, column combination to 1. Is this what you've tried?
Sep 23 '07 #2

P: 8
WOW, the way you put it was so much clearer. Thanks! (It worked). However, if I put it in a while loop to make it generate a 2nd random (with prompts, etc), the program them generates a second board where the first column has straight 0's, and the rest have more than 1 "queen" per column (which is incorrect). Although, this isn't part of the assignment, I was simply taking it further for my own sake.

To answer your question,
The way how I originally did it was the following;
Cycle through each column,
Find a random number between 0 and 7, then set that to rrow (a temp value),
Set [col][rrow] = 1, then start all over.

This always gave me straight 0's, and I had no idea why. I honestly can't remember exactly what my coding was, since I was working on it late last night, and just wound up getting frustrated.

However, thanks a million for your help!
Sep 23 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.