Creating a space with an int variable in a 2d array

May 13, 2014 at 1:45pm
Hey!

I'm a beginner and using c++, i was wondering how do you create a space with an int variable in an array.


int Array[9][9] = {{ ' ', 0, 1 , 2 , 3, 4 ,5,6,7},{0,1,1,1,2,3,4,5,6},
{1,1,1,2,3,4,5,6,7},{2,1,2,2,3,4,5,6,7},
{3,2,3,3,3,5,6,7,8},{4,3,4,4,5,5,6,7,8},
{5,4,5,5,6,6,7,8,9},{6,5,6,6,7,7,8,8,9},
{7,6,7,7,8,8,9,9,9}};
As you can see at Array[0][0] i want make a space there. It comes out as 32.
so i tried converting it like this static_cast<char>(32) but it still doesn't work. Any ideas or advice?
Last edited on May 13, 2014 at 1:50pm
May 13, 2014 at 1:54pm

A space is a ASCII character so naturally it would show as 32 in your array of int because that's the character code for space.

Just curious why you would want to do that?
May 13, 2014 at 1:55pm
You can't. A space and an int are two different things.

I suggest you reconsider how your routines work. (Perhaps if you tell us what you are trying to do, we can help.)
May 13, 2014 at 1:58pm
Well i have 5 2d arrays. A,B,C,D and E. The answer that i get for B and C would go into D. As in int D[B][C]. So if i make the array a char then i wouldn't get the correct values. so if i make the array an int. I can just convert the 1st value to a space
May 13, 2014 at 2:02pm
char A[8][9] = {{' ','2','3','4','5','6','7','8','9'},{'2','2','2','3','4','5','6','7','8'},
{'3','2','2','3','4','5','6','7','8'},{'4','3','3','3','4','5','6','7','8'},
{'5','4','4','4','4','5','6','7','8'},{'6','5','5','5','5','6','7','8','9'},
{'7','6','6','6','7','7','8','8','9'},{'8','7','7','7','8','8','9','9','9'}};

char B[8][10] = {{' ','0','1','2','3','4','5','6','7','8'},{'0','1','1','1','2','3','4','5','6','6'},
{'1','1','1','2','2','3','4','6','7','7'},{'2','1','2','2','3','3','4','6','7','7'},
{'3','2','2','3','3','4','5','6','8','8'},{'4','3','3','4','4','5','6','7','8','8'},
{'5','4','5','5','6','7','8','9','9'},{'6','5','5','6','7','8','8','9','9','9'}};

char C[9][9] = {{' ','0','1','2','3','4','5','6','7'},{'0','1','1','1','2','3','4','5','6'},
{'1','1','1','2','3','4','5','6','7'},{'2','1','2','2','3','4','5','6','7'},
{'3','2','3','3','3','5','6','7','8'},{'4','3','4','4','5','5','6','7','8'},
{'5','4','5','5','6','6','7','8','9'},{'6','5','6','6','7','7','8','8','9'},
{'7','6','7','7','8','8','9','9','9'}};

char D[10][10] = {{' ','1','2','3','4','5','6','7','8','9'},{'1','1','2','3','4','5','6','7','8','9'},
{'2','2','2','3','4','5','6','7','8','9'},{'3','3','3','3','4','5','6','7','8','9'},
{'4','4','4','4','4','5','6','7','8','9'},{'5','5','5','5','5','5','6','7','8','9'},
{'6','6','6','6','6','6','6','7','8','9'},{'7','7','7','7','7','7','7','7','8','9'},
{'8','8','8','8','8','8','8','8','8','9'},{'9','9','9','9','9','9','9','9','9','9'}};

I have an answer to a problem which links up at B[2][3] and C[3][4]
so that answer should go into the array D[B[2][3]][C[3][4]] but it wouldn't because it's turning everything to ascii values and the answers are coming out haywire
May 13, 2014 at 2:02pm
Thanks for the help guys:)
May 13, 2014 at 2:05pm

The answer that i get for B and C would go into D. As in int D[B][C]

You cant statically allocate an array using a variable holding a size, you would need to know its size in advance using that method. To make arrays during runtime you would need to dynamically allocate the memory using the new operator.

I still don't follow why you need a space though.
Last edited on May 13, 2014 at 2:07pm
May 13, 2014 at 2:08pm
I still don't follow why you need a space though.


please elaborate why you need this space?
May 13, 2014 at 2:13pm
It's part of an exercise unfortunately. Like asking the user questions and for each question the get a mark. Then when i'm done with section A's question. I put the marks i got for the questions in the array A and get the answer. But the table i was given has a space in it
May 13, 2014 at 2:13pm
Are you saying that i should leave the space?
May 13, 2014 at 2:21pm

No, I just didnt see the logic in having a space there.

Does the table you were given match the one posted above? - or have you modified it while experimenting?
May 13, 2014 at 2:25pm
No it matches it exactly. I also don't see the logic because we don't need to display the tables lol
May 13, 2014 at 2:25pm
But i thought maybe there could be a way. Thanks for trying though:)
May 13, 2014 at 2:26pm
Can you post the full exercise details?

I could probably help you a lot more if you posted it as it was given to you, at the moment I don't completely follow the objective.
May 13, 2014 at 2:32pm
Yes no problem. It's a pdf. Could you inbox me your email addy
May 13, 2014 at 2:35pm
Wont let me, seems to be a bug on the forum because your current and previous names cant be found.

You could always use dropbox
May 13, 2014 at 2:37pm
or send me a pm with your email and i will reply to it
Topic archived. No new replies allowed.