Thread: Array Question...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    51

    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....


    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.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    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)
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    2
    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;

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    51
    thats what i tried... but it doesnt make a box, just the sections of it

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    post all code perhaps?
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    51
    u sure you want that? ok



    #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.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    don't think there is
    you'll just have to live with that for loop
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM