473,491 Members | 2,205 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

8 New Member
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 3901
Ganon11
3,652 Recognized Expert Specialist
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
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
angel120
8 New Member
Well, I tried it this way;

Expand|Select|Wrap|Line Numbers
  1. int b[8][8]={0};
  2.     int j;
  3.     for (i=0; i<8; i++)
  4.     {
  5.         j=q[i];
  6.     }
  7.  
  8.     for (i=0; i<8; i++)
  9.     {
  10.         for (j=0; j<8; j++)
  11.         {
  12.             cout << b[j][i];
  13.         }
  14.         cout << endl;
  15.     }
  16.  
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
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
angel120
8 New Member
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

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

Similar topics

2
2040
by: Gus Tabares | last post by:
Hello all, I'm trying to subclass array.array but having problems with a default parameter in the init constructor. Example: import array class TestClass(array.array): def __init__(self,...
2
19403
by: CES | last post by:
All, Sorry for the 101 question but I can't figure out how to return all of the contents of an array to another function. I been able to figure out how to return individual elements of the array...
2
15111
by: Tobias Olbort | last post by:
Hello, i've a outer function, which takes a params-array as a parameter. I want to pass this array to inner function with a params-array (e. g. string.format). When i've passed an integer to...
3
4151
by: Steve | last post by:
How can I copy 5 bytes from the middle of one array to another in Managed C++? The following code segment causes the compilation errors below: unsigned char cResult __gc = new unsigned char __gc;...
3
6561
by: unicorn7777777 | last post by:
Hi, I need a deep copy constructor for a class which contains an array for another class: class Chessboard { public: ChessSquare chessSquare; // copy constructor needed to copy all...
4
2592
by: Sandman | last post by:
Hi, So I have 2 arrays: one contains userids. It may look like: user_id =12, user_id =30, user_id =43 The other is a multi-dimensional array with fields like: user_info = Array (
0
936
by: kishor14 | last post by:
I need an example of how to marshal a structure that has an array of another structures as a member. An example as mentioned below public struct strcutdata { ...
1
2966
by: agendum97 | last post by:
MSDN says splice has the following arguments: arrayObj.splice(start, deleteCount, ]]]) Thus I can insert items into an array using splice. But how do I insert an entire array? For example:...
9
1911
by: manojrana | last post by:
Can we store an array within another array in javascript var locat = new Array(); var element = new Array(); locat = element; or Can we use like this locat = "manoj";
1
2708
by: aspire | last post by:
All, I am trying to allocate memory to a pointer to an array in another function, but i am not getting a successful compilation. I am getting error on a line shown below in code. ------------...
0
7115
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7154
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7190
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6858
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7360
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4881
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1392
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.