I have to write a code to build an array like this:

1,6 1,0 2,3
3,4 5,6 4,5
7,6 1,2 9,7

Where the numbers are coordinates which have to be "separated" when taken in consideration
Like:

cell[2][2]---> x=5 y=6

I decided to make a char array, so that with atoi() I then convert it to numbers, but I cannot declare the array:

char iaMatrix[iSize][iSize][3]={{'1,2','1,0'}
				{'1,0','6,4'}};

It gives me error:
missing } for lots of times.

What should I do? Thank you.

Recommended Answers

All 7 Replies

Store the data in a structure

struct mys
{
unsigned int x;
unsigned int y;
};

struct mys the_s[10] = {{1, 2}, {3, 4}, {5, 6}, ...};

Store the data in a structure

struct mys
{
unsigned int x;
unsigned int y;
};

struct mys the_s[10] = {{1, 2}, {3, 4}, {5, 6}, ...};

But does that allow me to make it Bydimensional? I cannot see it... I mean:
I cannot print it as I want.

But does that allow me to make it Bydimensional? I cannot see it... I mean:
I cannot print it as I want.

You can make the array as many dimensions as you like but remember that each element contains a structure that has both an int x and a int y. For printing try..Note for the demonstration I'll use a two dimensional example.

std::cout << "x->" << the_s[1][1]->x << " y->" << the_s[1][1]->y << std::endl;

I'm sorry, I've not explained myself:

I need to print a table like this:
1,2 2,3 4,7 4,3
1,8 9,2 5,1 8,5
9,3 3,0 2,1 3,5

(random numbers)

And to make that, I need those number (1,2) to be char[]

But How can I make it? I mean the struct Idea is fine

struct cChar{
   char cCoordinates[3];
};
int main(){
	cChar iaMatrix[iSize][iSize]={{'1,2','2,3'}};
}

And it works, I mean it compiles. But I cannot print it as a table!
How could I do it? Maybe without a struct, cause the homework doesn't ask it...

What I need to do is: I get a table row column
[1][3]
And then I have to read the values as separeted coordinates to go to the cell they say if it exists.
So I thought of char, but maybe there's a better solution?

To print the table use a nested for statement...one for statement for each dimension.

I've found it myself, I simply put ' instead of "
for anybody who may need it:

const int iSize=8;
const int iSizeChar=3;

int main(){
	char iaMatrix[iSize][iSize][4]={{"1,2","2,3"}};
	cout << iaMatrix[0][0] << endl;
	system("pause");
	return 0;
}

works fine.
Just remember that the size of the string (char[]) must be one more of what you need for the terminator character '\0'.
Night.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.