Advertisement

using 2D arrays

Started by May 26, 2000 06:52 AM
10 comments, last by Crazie Joey 24 years, 7 months ago
right. i have my array set up. it''s 4x4. now, is there a way i can put a continuous stream of data in? with 1D arrays, you can do it like this: Array[] = { 1, 2, 3, 4 }; but can you do it with 2D arrays, or do i need 16 "=" signs and ugly code? Crazie Joey
I don''t beleive you can do this:


int array[][] = {

3,4,

5,6};


But you could use a for or do loop or something to move in the data, if it need only be incremented like your post suggested...I sincerely doubt that though. I think what you want to do though is to actually declare the size of the array. The weirdest part about 2d arrays is that normal people think in X-Y. C/C++ thinks in Y-X, e.g.-->

int array[y][x];

Weird stuff. (Did u know that already. Probably.)


I hope my br HTML tags showed up properly.....
"Now watch as I run away in a womanly fashion." - Batman
Advertisement
You can do this:

array[4][4] = {{0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}}; 
"I have realised that maths can explain everything. How it can is unimportant, I want to know why." -Me
chris: thank you very much. this was the information i was looking for (filling out matrix tables and keeping code pretty at the same time - not an easy thing!)

CJ
You could, infact, do it like this:

int array[][4] = {
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0
};

I believe that is one of the correct ways to do it. No, you don''t need to tell C the first index on initialisation - only the second.

-Mezz
i''ve tried all the suggestions but none of them work. this is a sample of the code.

#include

typedef float Matrix4x4[4][4];

void RotX4x4(float AngleX, Matrix4x4 *Dest) {
*Dest = {{ 1, 0, 0, 0 };
{ 0, cosf(AngleX), - (sinf(AngleX)), 0 };
{ 0, sinf(AngleX), cosf(AngleX), 0 };
{ 0, 0, 0, 1 };};
}

this spits out 6 error messeges(over 60 in the full code)

Compiling...
matrix.cpp
C:\Windows\Desktop\eng\matrix.cpp(6) : error C2059: syntax error : ''{''
C:\Windows\Desktop\eng\matrix.cpp(6) : error C2143: syntax error : missing '';'' before ''{''
C:\Windows\Desktop\eng\matrix.cpp(6) : error C2143: syntax error : missing '';'' before ''}''
C:\Windows\Desktop\eng\matrix.cpp(7) : error C2143: syntax error : missing '';'' before ''}''
C:\Windows\Desktop\eng\matrix.cpp(8) : error C2143: syntax error : missing '';'' before ''}''
C:\Windows\Desktop\eng\matrix.cpp(9) : error C2143: syntax error : missing '';'' before ''}''
Error executing cl.exe.

matrix.obj - 6 error(s), 0 warning(s)


if anyone can help i would appreciate it.
Advertisement
Did you actually read my post? Your not supposed to put a semi-colon in between the braces your supposed to put commas. Re-read my post.
"I have realised that maths can explain everything. How it can is unimportant, I want to know why." -Me
i did, and it generated even more errors:

C:\Windows\Desktop\eng\matrix.cpp(6) : error C2143: syntax error : missing '';'' before ''{''
C:\Windows\Desktop\end\matrix.cpp(6) : error C2143: syntax error : missing '';'' before ''}''
C:\Windows\Desktop\eng\matrix.cpp(6) : error C2143: syntax error : missing '';'' before '',''

x 4!!!

you people dont understand, the array has already been created, i just need to shove in some information!

I''ll give you another example.

Matrix4x4 MyArray;

RotX4x4(2.0f, &MyArray);

and in the function the data is added.

CJ
Code like
int Array[][4] = { {1,2,3,4}, {5,6,7,8} }; 

only works when initialising a declaration. You can''t use it to assign to an already declared variable.

Erik
bugger. i thought you would be able to because you can with 1D arrays. oh well

CJ

This topic is closed to new replies.

Advertisement