I am using Dev-C++ 4.9.9.2 and have just started to teach myself c++ (using the tutorials on this site and C++ for dummies
First, I thought you could initialize all the elements in an array to a commont value (like int block[4][4]={1}) but in the following code, it only sets block[0][0] to 1, and leaves the rest untouched. I get the output:
::::::
.# .
. .
. # .
. .
::::::
When the whole block should be '#' (if all the elements in block were set to 1)
Now my main question is how to make functions that deal with arrays. For example I want to take a 4 by 4 array and "turn" it 90 degrees. I think I kow how to do it:Code:#include <iostream> using namespace std; void display(int matrix[4][4]); main() { int block[4][4]={1}; block[2][2]=1; //to make sure it isn't the display function at fault display(block); cin.get(); } //Display function void display(int matrix[4][4]) { for(int c=0,x=4;c<=x+1;c++) {cout << ":";} cout << "\n"; for ( int y = 0; y < 4;y++ ) { cout <<"."; for ( int x = 0; x < 4; x++ ) { if (matrix[x][y]==1 || matrix[x][y]==2){cout <<'#';} else {cout <<' ';} } cout << "."; cout<<"\n"; } for(int c=0,x=4;c<=x+1;c++){cout << ":";} }
or something like that (i dont know if that will work; havn't tested it). I just dont no how to put an array in and get one out. Can I do something like I just did and then put:Code:int rotate_array(int array[4][4]) { int new_array[4][4]; for (int y;y<4;y++) { for(int x;x<4;x++) { new_array[abs(y-3)][x]=array[x][y]; } } return new_array; }
anyarray=rotate_array(anyarray)
to change anyarray to its itself rotated,
or can I make a function that will change whatever array is put into
it so I just need:
rotate_array(anyarray)
to change whatever array is put into it, or what?
I have looked through alot of posts already (and learned some stuff from them to) but i havn't figured any of this out. I also noticed that alot of questions deal with arrays, perhaps someone could make a more detailed tutorial on them?
thanks



LinkBack URL
About LinkBacks


