Thread: just for fun...

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    just for fun...

    Here's a little problem I came across. If you have the time and are interested...

    Populate a 2-D array exactly as shown below:
    Code:
    1 2 3 4 5 6 7 8 9
    2 3 4 5 6 7 8 9 1
    3 4 5 6 7 8 9 1 2
    4 5 6 7 8 9 1 2 3
    5 6 7 8 9 1 2 3 4
    6 7 8 9 1 2 3 4 5
    7 8 9 1 2 3 4 5 6
    8 9 1 2 3 4 5 6 7
    9 1 2 3 4 5 6 7 8
    Provide the simples solution possible. Only stipulation is that the values must be in an array (no classes).

    Here's my best stab at it:
    Code:
    for( int i = 0; i < 9; i++ ){
     
        for( int j = 0; j < (9-i); j++ )
            mArray[i][j] = j+i+1;
     
        for( int k = 0; k < i; k++ )
            mArray[i][(9-i)+k] = k+1;
    }
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Static initialization would be the simplest.

    Did you "come across" this problem, or was it assigned?

    gg

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    One loop. (But probably less efficient than two loops, really)
    Code:
    int x = 0;
    for( int i = 0; i < 9; i++ ){
      
       for( int j = 0; j < 9; j++) 
            mArray[i][j] = x % 9 + 1;
        x++;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by Codeplug View Post
    Static initialization would be the simplest.

    Did you "come across" this problem, or was it assigned?

    gg
    I'm exploring some options using java swt to provide the gui for an app whose nuts and bolts are in c++. as a small example, i began playing around with creating a very simplistic sudoku game. to test things out, i needed a simple a 9X9 array where every row was different from the other rows, every column diff from the other cols, and the 9 3X3 boxes all different from each othe as well. So... i went off on a tangent and played around with this. Ultimatley I'm wondering if there is a substantially simpler way to do this like cutting it down to 2 "for" loops and s single assignment? Or is it simply mathematically impossible?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  5. #5
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by matsp View Post
    One loop. (But probably less efficient than two loops, really)
    Code:
    int x = 0;
    for( int i = 0; i < 9; i++ ){
     
       for( int j = 0; j < 9; j++) 
            mArray[i][j] = x % 9 + 1;
        x++;
    }
    --
    Mats
    That gives me the following output:
    Code:
    1 1 1 1 1 1 1 1 1
    2 2 2 2 2 2 2 2 2
    3 3 3 3 3 3 3 3 3
    4 4 4 4 4 4 4 4 4
    5 5 5 5 5 5 5 5 5
    6 6 6 6 6 6 6 6 6
    7 7 7 7 7 7 7 7 7
    8 8 8 8 8 8 8 8 8
    9 9 9 9 9 9 9 9 9
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Personally, I agree with Codeplug and prefer initialisation:
    Code:
    int numbers[9][9] = {
        {1, 2, 3, 4, 5, 6, 7, 8, 9},
        {2, 3, 4, 5, 6, 7, 8, 9, 1},
        {3, 4, 5, 6, 7, 8, 9, 1, 2},
        {4, 5, 6, 7, 8, 9, 1, 2, 3},
        {5, 6, 7, 8, 9, 1, 2, 3, 4},
        {6, 7, 8, 9, 1, 2, 3, 4, 5},
        {7, 8, 9, 1, 2, 3, 4, 5, 6},
        {8, 9, 1, 2, 3, 4, 5, 6, 7},
        {9, 1, 2, 3, 4, 5, 6, 7, 8}};
    Quote Originally Posted by dudeomanodude
    That gives me the following output:
    Fix the bug, e.g.,
    Code:
    for( int i = 0; i < 9; i++ ) {
        int x = i;
        for( int j = 0; j < 9; j++) {
            mArray[i][j] = x++ % 9 + 1;
        }
    }
    Last edited by laserlight; 01-30-2009 at 11:03 AM. Reason: Since matsp chose to use post-increment, let's put it to some use.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    matsp's example was what i was looking for, thanks. i understand how static init is more efficient, but it's not something fun to play around with
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  8. #8
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    btw, it's disgusting how good you guys are.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Perhaps a little simpler:
    Code:
    for (int i = 0; i < 9; ++i) {
        for (int j = 0; j < 9; ++j) {
            array[i][j] = (i+j) % 9 + 1;
        }
    }
    (EDIT: Previous "purely gross" example excised due to incorrectness.)
    Last edited by tabstop; 01-30-2009 at 11:43 AM.

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    ^_^

    All of you are forgetting the "obvious" version. (That is probably the slowest.)

    Soma

    Code:
    for(unsigned int z(0); 81 > z; ++z)
    {
       grid[z / 9][z % 9] = ((z % 9) + (z / 9)) % 9 + 1;
    }

Popular pages Recent additions subscribe to a feed