# Array Question...

• 11-25-2002
tetraflare
Array Question...
im teaching myself c ++ due to my teachers lack of teaching skill, he might get fired, but it wont happen for a while... so instead of allowing him to trash my future, i wanna learn it on my own. ok... with multi-dimensional arrays, i am trying to make a box.

this is how i do it but it requires about 30 for loops....

Quote:

int main ()
{

int arraybox [20][20]; // defines array

for ( int i = 0; i < 20 ; i ++ )
cout<<arraybox[ i ] [0]<<endl; //draws box out ONE row at a
// time.

for ( int j = 0; ij < 20 ; j ++ )
cout<<arraybox[j ] [1]<<endl; //draws box out ONE row at a
// time.

i am just wondering if this can be done in like 2 for loops instead of 20 for each row and column.
• 11-25-2002
lostminds
Code:

```for ( int i = 0; i < 20 ; i ++ )   for (int j=0; j < 20; j++)     cout<<arraybox[i][j]<<endl;```
Is that what you are looking for? ( I don't really understand what you are asking though :( , think my brain is fried)
• 11-25-2002
myjunk517
Yes this operation can be done in a nested for loop.
Two for loops combined. Like this

int arraybox[20][20]

for (int i = 0; i < 20; i++)
for(int j = 0; j < 20; j++)
cout<<arraybox[ i ] [j]<<endl;
• 11-25-2002
tetraflare
thats what i tried... but it doesnt make a box, just the sections of it :(
• 11-25-2002
lostminds
post all code perhaps?:)
• 11-25-2002
tetraflare
u sure you want that? ok :)

Quote:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <windows.h>
using namespace std;
unsigned long stepInt = 0;
const int size = 40;
int arraybox[size][size];
int r = 1, c = 1;
void print(void);
void move(void);
int main()
{
int yo=1;

for (int i = 0; i < size + 1; i++) {
arraybox[i][0] =177;
arraybox[i][size-1] = 177;
arraybox[0][i] = 177;
arraybox[size-1][i] = 177;
}
print();
cout << "\nProccessing\n";
for (int y = 0; y < 50; y++) {

Sleep(10);

cout << '.';
}
move();
cout << endl << "Steps: " << stepInt << endl;
cin >> yo;

return 0;

}

void print()
{

for (int l = 0; l < size; l++) {
cout << endl;
for(int g = 0; g < size; g++)
cout<<static_cast<char>(arraybox[ l ] [g]);

}
}

void move()
{
int num, end = 0;
srand(time(0));
while (end != 1) {

num = 1 + rand() % 4;

if ( num == 1 && arraybox[r-1][c] != 177) {
arraybox[r][c] = 27;
r = r - 1;
stepInt += 1;
}
else if ( num == 2 && arraybox[r+1][c] != 177) {
arraybox[r][c] = 26;
r = r + 1;
stepInt += 1;
}
else if ( num == 3 && arraybox[r][c-1] != 177) {
arraybox[r][c] = 24;
c = c - 1;
stepInt += 1;
}
else if ( num == 4 && arraybox[r][c+1] != 177) {
arraybox[r][c] = 25;
c = c + 1;
stepInt += 1;
}

if (r == size-2 && c == size-2) {
end = 1;
cout << endl << endl << endl << endl;
arraybox[r][c] = 4;
print();
}

}
}

this will generate a box, but if you havent noticied i used 20 for loops in the print() function.... how can i reduce that ... to say 2? or something way smaller.
• 11-25-2002
lostminds
don't think there is
you'll just have to live with that for loop :eek: