so i have been told multiple times that "using MD arrays are bad". so i decided to finally start using 1d arrays instead, so i was wondering if i did it correctly:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include "stdafx.h"
#include <iostream>
usingnamespace std;
int main (){
int a[3 * 3] = {1,2,3,4,5,6,7,8,9};
for (int r = 0; r < 3; r++){
for (int c = 0; c < 3; c++){
cout << a [(r*3) + c] << " ";
}
cout << endl;
}
system ("Pause");
return 0;
}
by the way, please ignore the "system("pause");" i know its pretty bad to use, but i dont really care as this code will only be used for practice and not for an actual program ;)
well, yes it is easy to mix up, but i wouldnt have it like this in an actual code. i would probably do something like having an int or a const int that was named width and one that was called height.
this way i would easily be able to se wich was wich and would also be able to quickly change the Height/width :)
Whomever told you "using MD arrays are bad" is hardly an authority, and he is wrong.
Multidimensional arrays exist so that you do not need to write a[r * HEIGHT + c] stuff. The computer provides you with a convenient abstraction so that you can use it effectively to manage your ideas.
Unless you are seeking to understand how multidimensional arrays actually work underneath (and pretend you are the compiler) -- or if you are trying to do something difficult (such as pass an arbitrary 2D array to a function) -- there is no need to do that kind of thing.
Yes, they have their place, but C++ (and boost::multi_array) provides great alternatives for us. I suspect I would have said to use something else in the same places Disch has.
well, first off, yes you are the one who told me they were bad, disch ;)
and secondly the 3x3 stuff was just an example. i am using arrays in my 2d tile engine, and therefore i would have alot bigger arrays than a 3x3 array, it was just not needed to write like a bigger array to test if it worked.
and also, douas, I do want to learn more about how MD arrays work (i really am fascinated about programming, pc's, and compilers and all that stuff, so i love to learn how stuff works.)
and yes i wanted to be able to pass the arrays to a function, wich was basicly what made disch say they were bad in the first place, and he linked me to one of his posts on arrays (thanks alot for that, it was a interesting post)
Okay yeah if the array is going to be dynamically sized and passed to and from functions, then I would definitely either use some kind of established container or encapsulate it in a class.