Thread: multi-demensional array confusion

  1. #1
    irregularly symmetrical n3v's Avatar
    Join Date
    Mar 2006
    Location
    Finland
    Posts
    67

    multi-demensional array confusion

    multi-demensional arrays are getting me kinda confused. I have a question. Firstly, have a look at this loop:

    Code:
        int board[7][7] = {0}; 
       for(int n = 0;n < 8;n++) {
              for(int i = 0;i < 8;i++) {
               cout << "[" << board[n][i] << "]";
               }
        cout << endl;
    }
    all i want it to do is project the correct matrix, which should be a big box full of zeros, because i'm doing nothing more to it yet. However, the very last number on the 7th line, and all of the 8th line are overloads, and bring random digits from surrounding memory. any hints why?
    the weird thing is, if i add a line of code before the loop like:
    Code:
    board[7][6] = 0;
    when the program runs, i'll still get overflows on the last line, except for 7,6! I've done all kinds of debugging, and it looks like the loop is producing the correct numbers, none over 7, but it just goes crazy on the last line when i have it display the matrix! I know i could patch this problem up by adding unnessecary lines of code, but i'm sure there's a more reasonable explination.

    also, is there a way to make changes to a whole row at once, like if you want row 4 to entirely have the same number, can you do it with a command, and not with a loop?

    thanks.
    Last edited by n3v; 03-17-2006 at 02:21 AM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Don't forget you're declaring your arrays with a size of 7. This means the indexes go from 0 to 6. By looping to < 8, then you're accessing an index (7) out of the arrays bounds. While this may not seem like a problem because it's just passing junk to your program, accessing memory that you didn't allocate can cause segmentation faults and make your program crash.

    ...and to your second question, not really. I suppose you could manipulate memset(), but that may not be a good idea, and it would only work with characters. Also, if I had to guess, memset() does a loop anyway.

    Code:
    #include <iostream>
    #include <cstdio>
    
    using namespace std;
    
    const int SIZE1 = 7,
              SIZE2 = 7;
    
    int main() {
        char arry[SIZE1][SIZE2] = { NULL };
        
        // Changing the 4th row to 'A'
        memset(arry + 3, 'A', sizeof(*arry));
        
        for(int n = 0;n < 7;n++) {
          for(int i = 0;i < 7;i++)
             cout << "[" << arry[n][i] << "]";
          cout << endl;
        }
        cin.get();
        return 0;
    }
    Last edited by SlyMaelstrom; 03-17-2006 at 02:45 AM.
    Sent from my iPadŽ

  3. #3
    irregularly symmetrical n3v's Avatar
    Join Date
    Mar 2006
    Location
    Finland
    Posts
    67
    i just made a very silly mistake. for shame.
    i somehow had it fixed in my head that arrays counted zero up to the number i have. isn't it that way in visual basic? anyway, thanks a lot, sorry for wasting your time.

    by the way, when you declare a function that er.. you know, uses an array, er.. can't think of the proper word.. anyway, like this:

    Code:
    int arrayfunction (int x[5][5]);
    do you do it like that?

    also, any tips on returning a matrix? is it even possible?
    Last edited by n3v; 03-17-2006 at 02:34 AM.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    When you pass an array to a function you only have to define the sizes for each dimension after the first
    Code:
    int arryFunc(int [][SIZE2]);
    and you can't return an array, but you can return a structure that contains an array. Otherwise when it's passed as an arguement, it's passed as a reference and can be changed in the function anyway.
    Sent from my iPadŽ

  5. #5
    irregularly symmetrical n3v's Avatar
    Join Date
    Mar 2006
    Location
    Finland
    Posts
    67
    structure that contains an array? i think this is the time when you need to explain where pointers actually come in handy. I've read about pointers, but i just need an example to show me when they'd actually do good for something. is this one of those cases? if not, what would that structure be? a string?

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    This is a structure.
    Code:
    // This is a structure that contains an array.
    struct foo {
        int arry[5][5];
    };
    
    // This is a function that returns a structure.
    foo Func1(int num1) {
       foo temp;
       for(int n = 0;n < 5;n++)
          for(int i = 0;i < 5;i++)
             temp.arry[i][n] = (i + n) * num1;
       return temp;
    }
    
    int main() {
       // This prints out the returned structure.
       for(int n = 0;n < 5;n++) {
          for(int i = 0;i < 5;i++)
             cout << setw(3) << Func1(4).arry[i][n];
          cout << endl;
       }
    
       cout << endl;
    
       for(int n = 0;n < 5;n++) {
          for(int i = 0;i < 5;i++)
             cout << setw(3) << Func1(6).arry[i][n];
          cout << endl;
       }
       return 0;
    }
    Last edited by SlyMaelstrom; 03-17-2006 at 03:03 AM.
    Sent from my iPadŽ

  7. #7
    irregularly symmetrical n3v's Avatar
    Join Date
    Mar 2006
    Location
    Finland
    Posts
    67
    ooh, a structure. stupid me again. i'm kind of a n00b in c++, so i don't really have any experience with structures. thanks for pointing that out though. what does setw(3) do?

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    It's a function defined in <iomanip> it sets the buffer width of the right operand (the next thing in the cout statement) to the size in the arguement and by default aligns to the right.

    For instance:
    Code:
    cout << setw(7) << "World!";
    can be seen as [ ][W][o][r][l][d][!], where as
    Code:
    cout << setw(11) << "World!";
    can be seen as [ ][ ][ ][ ][ ][W][o][r][l][d][!]
    Last edited by SlyMaelstrom; 03-17-2006 at 03:24 AM.
    Sent from my iPadŽ

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    also, any tips on returning a matrix?
    For what purpose? What would you like to do?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  2. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  3. multi dimensional array definition
    By gazsux in forum C Programming
    Replies: 2
    Last Post: 02-19-2003, 07:35 AM
  4. ? passing multi dimensional array to function
    By rc7j in forum C++ Programming
    Replies: 1
    Last Post: 02-10-2002, 02:19 AM
  5. multi array to function
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-09-2001, 03:01 AM