Problem Passing 2d int arrays between functions?

This is a discussion on Problem Passing 2d int arrays between functions? within the Game Programming forums, part of the General Programming Boards category; SOLVED:: Another Knotch on Elysia's belt Thanks to whoever reads this and spends their time answering. I'm building what I ...

  1. #1
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407

    Problem Passing 2d int arrays between functions?

    SOLVED:: Another Knotch on Elysia's belt

    Thanks to whoever reads this and spends their time answering.
    I'm building what I thought would be a quick sudoku generator / solver / allows the user to play it. I didn't realize how deep the logic of this particular puzzle game goes. I'll leave out most of the code and try to explain design first.

    3 Classes
    Sudoku
    Board
    Space

    Each class privately owns the class below it.
    Sudoku Possesses 10 Board classes. I declared these Boards in an Array:
    Code:
    Board GameBoard[10];
    The reasoning for this was to make many states of how the puzzle is being solved in one declaration. Allowing for the user to undo while playing the game by copying to current state from previous state. Board[9] is future (aka solved), Board[8] is Present, and any Board[7] - Board[0] is a state of the past.

    I finished the Random Generation algorythym and it works everytime. So my next step is to remove numbers while keeping it solvable. I added:
    Code:
    void Copy(Board Copyfrom, Board Copyto);
    in the Sudoku class. Before I moved any further I wanted to test this function so I copied my Solved Board over to a new blank Board and displayed the board copied to rather than the solved Board. To my surprise it showed up blank still. Somehow my data is not transfering over. My concern is there is some inaccuracy with passing an array through funcations considering Copy is taking two class arrays as input and copying 2d int array values from one to the other.

    I noticed during the randomgeneration phase, as well, that if I generated numbers into an array it gave very linear results. Example:
    Code:
    int temp[3];
    temp[0] = (rand()%9)+1; // number value for the space
    temp[1] = (rand()%9); // row of the space
    temp[2] = (rand()%9); // column of the space
    This when placed in a loop would keep generating random numbers on a linear diagnol of the board. Once finished with one diagnol it would move to another.

    If I changed nothing else in my program except:
    Code:
    int tempnumber, tempr, tempc;
    tempnumber = (rand()%9)+1; // number value for the space
    tempr = (rand()%9); // row of the space
    tempc = (rand()%9); // column of the space
    Sudenly the random numbers actually appeared random. This is starting to make me fear that there are rules to arrays that I know know about. Thank you to anyone who can shed some light on this.
    Last edited by Lesshardtofind; 09-13-2010 at 07:07 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Re: copy problem. You need to use references. Everytime you pass a variable to a function, a copy is made. Hence you are working with local copies.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    Sweet worked like a charm changed
    Code:
     
    void Copy(int copyfromindex, int copytwoindex);
    Bad structure on my part I guess. I should have forseen that. Thank you very much for the help Elysia....I noticed you didn't comment on the random number part though... any idea?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    I simply don't have time to check all of your post and I still don't. Perhaps someone else does.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Well, since I had time, I took a look at this, and there are a few things...
    First, about states... rather than keeping an array like this, I would use a structure that allows undoing. I currently have one generic piece that looks like this:
    Code:
    #ifndef UNDO_H_20100712_1247
    #define UNDO_H_20100712_1247
    
    #include <vector>
    #include <utility>
    
    namespace Stuff
    {
        template<typename T>
        class Undo
        {
        public:
            void Push(T& Where/*, const T& What*/)
            {
                m_Undo.push_back(std::make_pair(&Where, Where));
            }
    
            void Pop() 
            {
                auto & UndoData = m_Undo.back();
                *UndoData.first = UndoData.second;
                m_Undo.pop_back();
            }
    
        protected:
            std::vector<std::pair<T*, const T>> m_Undo;
        };
    }
    
    #endif
    Basically you create an instance of the class with the data type of the object you want to be able to undo. Then you push before you change. If you change your mind later, you pop, and your change is undone. It's basically a stack, so changes are pushed on top of each other and undone in reverse order.

    About the random numbers... are you calling srand in your program? Are you doing it once or more? srand should be called once at the beginning.
    If this doesn't work, then consider reading Eternally Confuzzled - Using rand()
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    Interesting I do appreciate the advice. I don't know the namespace so I'll do some research on what that is. I also ended up learning about references about two days after I posted this and realized that would also work. The game itself is halfway finished (from where I want it to end), and 95% of the functions including the random generation of the sudoku puzzle and logic are all dependant on class Board. The idea of pulling the entire code apart at this point is unappealing to say the least. I'd rather just finish it move on and definitely use your method on the next game I work on. As always I learn so much during each project that by the end of it I hate the way I programmed it lol. (guess thats good means I'm learning). I also realized the fastest method of solving/ generating the sudoku board might actually lie within linked lists(which I know nothing about other than the tutorial I read on cprogramming.com).

    On the subject of the random yes I call to srand only once at the beginning of WinMain before the while(!done) event loop. And it seemed to generate random numbers, but they were all in a diagnol.. tested it 4 times then changed the int that recieved the rand from an array to just a regular int and it worked fine. Who knows maybe the 4 times I ran it then generation was "randomly" in a diagnol line. None the less the random generation has alot more rules now to add speed to the generation of the sudoku puzzle, so it is fixed. Thanks again for your time and information Elysia.

  7. #7
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    namespace just limits the scope of objects declared within. For example, you could define "int x" as a global inside a namespace, and as long as you don't put "using namespace [name of that namespace]" you can define it as a global outside of that namespace w/o a redefinition error.

  8. #8
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    This board teaches me more than any books I've owned so far ;o). Thnx.

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    It should be noted (as Prelude's site will also allude to) that rand() is a very poor pseudo random number generator. There are many other ones available on the internet that produce far better results and the frequency of repeated patterns is much lower.

    About the states you can do what Elysia suggested and make the Undo/Redo a class or you can make a class encapsulating the state that has 2 functions Undo and Redo. Each state object would take care of the undo/redo b/c who better to know how to undo/redo an action or state than the action/state itself. Either way both designs rely on a stack to keep track of past events/actions.

  10. #10
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    I am definitely aware of that rand() being a poor random number, but after a first random space is generated in the sudoku then the numbers it can generate is restricted by the amount of numbers left possible in the next open space, and ect. I'v so far played around 50 games and generated over hundreds in a row just to see if I got a reapeat... Never even had a repeated hint pattern much less the same sudoku. When we are talking about a number that goes up to 6,670,903,752,021,072,936,960 ... I don't think I need to be heavily concerned about generating a "truly" random number.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 10:16 PM
  2. Passing 2d arrays to functions
    By owi_just in forum C Programming
    Replies: 2
    Last Post: 05-06-2005, 06:22 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21