Thread: Access Array

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    85

    Access Array

    Hi every1

    i Have an array with 2 sets of elements
    But how can i allow the use to choose which set of eletment to display on screen

    Code:
    ////create array 2 maps (32 X 32) (1024 steps)////
    
    int matrix[MATRIX_SIZE][MATRIX_SIZE];
    int matrix_data[2][MATRIX_SIZE][MATRIX_SIZE]; ={ {{0}}, 
    
    { {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
        {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
        {0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0},
        {0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},
        {0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0},
        {0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0},
        {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
        {0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0},
        {0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0},
        {0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},
        {0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0},
        {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
        {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}
      };}
    
    return matrix_data[0];
    int trace_matrix[MATRIX_SIZE][MATRIX_SIZE];
    in the main I have done these:

    Code:
    
    
    int main()
    {
        int maze;
        cout <<"Ender Maze wanted, 0,1,2,3:\n";
        cout <<"    0- Random Maze\n";
        cout <<"    1- Set Maze (1)\n";
        cout <<"    2- Exit\n";
        cin >> maze;
        cout << "Maze Selected is " << i;
        return 0;
        //int n;
        //for (n=0; n<4; n++);
        //result += matrix_data[n];
        //printf("%d\n", matrix_data[n]);
        //std::cout<< matrix_data <<std::enl;
        //cout << result
       
    //////////////////////////////////////////////////////////////////////
        
    
    {
        //////////I have set it to the array zero for random generat maze///
    int x; 
    srand(time(0));
    //for (x = 0; x < 300; x++) matrix_data[0][rand()%MATRIX_SIZE][rand()%MATRIX_SIZE] = 1;
        ////////////// ensure food do not over lap//////////////////
        
    for (x = 0; x < 300; x++) {
        int x = rand()%MATRIX_SIZE;
        int y = rand()%MATRIX_SIZE; //our x- y- coords
        matrix_data[x][y]++; // add 1 to out rand chosen spot
        if (matrix_data[x][y] > 1) 
            matrix_data[x][y] = 1;
            x--;
          printf("%d\n", 1);    
            
        }
    
        
    }
    Therefore the first map is random generated and second map positioned...


    How can i make a loop for the user to choose which map??


    ------THX---------A-------------LOT----

  2. #2
    Why am I a programmer? shoutatchickens's Avatar
    Join Date
    Mar 2008
    Posts
    45
    >> How can i make a loop for the user to choose which map??

    You could create the code to display each maze in seperate functions and determine which to print based on user input. I'll show you a simple example.

    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    void printFirstMaze() {
        cout << "First maze!" << endl;
    }
    
    void printSecondMaze() {
        cout << "Second maze!" << endl;
    }
    
    int main()
    {
        bool isDone = false;
        int  choice = 0;
    
        while(!isDone) {
            cout << "Enter choice: ";
            cin  >> choice;
    
            cout << endl;
    
            switch(choice) {
                case 1:
                    printFirstMaze();
                    break;
    
                case 2:
                    printSecondMaze();
                    break;
    
                case 3:
                    isDone = true;
                    break;
    
                default:
                    cout << "Invalid option";
            }
    
            cout << endl;
        }
    
        return 0;
    }
    Does that answer your question?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Access array from class
    By parad0x13 in forum C++ Programming
    Replies: 6
    Last Post: 03-18-2009, 08:12 AM
  3. How to access a dynamic array inside a std::set ?
    By IndioDoido in forum C++ Programming
    Replies: 16
    Last Post: 11-04-2007, 05:27 PM
  4. access violation in int array
    By George2 in forum C Programming
    Replies: 2
    Last Post: 08-02-2007, 11:28 PM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM