440,180 Members | 1,012 Online
Need help? Post your question and get tips & solutions from a community of 440,180 IT Pros & Developers. It's quick & easy.

# Printing a 1D array as a 2D array (another 8 queens variant)

 P: 8 So my second hw is to solve the 8 queens problem using a 1D array. The professor gave us a code that outputs the following (labeled as q[8]): 0 2 5 7 6 3 1 4 The spaces 0-7 refer to the column #, and the number in each space refers to the row #. So, I have to write a function that would put a 1 on space (0,0), (1,2), (2,5), etc. How should I approach this? I was wondering if it was possible to take the data from the 1D array and transform it into a 2D array, but it's not really coming out properly. My first attempt included turning q[8] into a 2D array (q[][]), but the compiler (in Visual Studios .NET 2003) said that pointers were needed... I never learned about pointers in CS111, but I think we were supposed to... Sep 23 '07 #1
5 Replies

 Expert 2.5K+ P: 3,652 This should be simple. You still know that this is a chessboard, which is an 8x8 board, right? Then there should be no need to (explicitly) work with pointers. Just work through the 1D array. At any spot i, you have a value in the array at q[i] that equals j. So you have i, j, and (presumably created beforehand) an 8x8 chessboard. at can you do with those values and variables? Sep 23 '07 #2

 Expert Mod 5K+ P: 9,197 I never learned about pointers in CS111, but I think we were supposed to... This is absolutely essential to your progressing in C++. Unless you understand address-based programming, otherwise known as indirection, learning C++ will always be just beyond your grasp. I suggest you revisit pointers and write some small programs using them. I would like to solve your array dilemma but I can't do it without using addresses and pointers. I will give you a hint, though: There are only one-dimensional arrays in C and C++. The number of elements is declared between brackets ( array[5]). Any other brackets describe the elements and not the number of elements. Sep 23 '07 #3

 P: 8 Well, I tried it this way; Expand|Select|Wrap|Line Numbers int b[8][8]={0};     int j;     for (i=0; i<8; i++)     {         j=q[i];     }       for (i=0; i<8; i++)     {         for (j=0; j<8; j++)         {             cout << b[j][i];         }         cout << endl;     }   And the array just printed out as 64 0's again. However, the original array (q[8] = {0, 2, 5, 7, 6, 3, 1, 4}) still retains it's data. What am I doing wrong? How would this be done using pointers (just to expand my knowledge)? Sep 23 '07 #4

 Expert Mod 5K+ P: 9,197 Well, I tried it this way; Code: ( text ) int b[8][8]={0}; int j; for (i=0; i<8; i++) { j=q[i]; } for (i=0; i<8; i++) { for (j=0; j<8; j++) { cout << b[j][i]; } cout << endl; } And the array just printed out as 64 0's again. However, the original array (q[8] = {0, 2, 5, 7, 6, 3, 1, 4}) still retains it's data. What am I doing wrong? How would this be done using pointers (just to expand my knowledge)? The array I see is b and b has 8 elements where each element is an array of 8 int all intialized to 0. So you see 64 zeros. What's the problem? What is this q array? Did you read Ganon11 Post#2? Sep 24 '07 #5

 P: 8 The array I see is b and b has 8 elements where each element is an array of 8 int all intialized to 0. So you see 64 zeros. What's the problem? What is this q array? Did you read Ganon11 Post#2? Actually, I misread it. I thought he meant for me to declare a second 8x8 array, then somehow set the values from the original 1D array into the 2D array. Now I realize that he meant for me to keep the 1D array. Thanks for your help. Sep 25 '07 #6