Two Dimensional Array Passing

This is a discussion on Two Dimensional Array Passing within the C++ Programming forums, part of the General Programming Boards category; I have a program Code: #include <iostream> using namespace std; void array_ini(); int main() { int array1[x_max][y_max]; /*x_min and y_min ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    85

    Two Dimensional Array Passing College Homework

    I have a program
    Code:
    #include <iostream>
    
    using namespace std;
    
    void array_ini();
    
    int main()
    {
    
    int array1[x_max][y_max];
    /*x_min and y_min are passed from cmd line...they work and define a ggod array as I can work with in the main function*/
    
    
    return 0;
    }
    
    void array_ini()
    {
    
    
    }
    What do I put in the function declaration at the top and the function call and function definition itself to pass my 2D array?


    Pleas help quick as this is needed for a college project
    Last edited by swanley007; 09-20-2005 at 07:49 PM. Reason: No One Looked

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    85
    sorry the array_ini function will initialize the array...Iknow how to do

  3. #3
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    I think it would be impossible to do such a thing.

    Since you would need to know at least the second amount of elements for array_ini in the prototype directives, which would be impossible if you're getting the values from main()'s parameters, so you could use global values to assign them to. You could make the array[][100] in the parameter, but y_max would have to be 100, which is unlikely to guess.

    I would use a class, and create the array in that, operate through that, and resize the array to the x_max, y_max values.

    Code:
    #include <iostream>
    
    class array
    {
    private:
      int *someints;
    
    public:
      array() { someints = new int[10 * 10]; }
    
      void resize(int, int);
    };
    
    void array::resize(int x, int y)
    {
      int* tempints = new int[x * y];
    
      delete someints;
    
      someints = tempints;
    }
    
    int function(array& object);
    
    int main()
    {
      array object;
    
      object.resize(x_max, y_max);
    
      function(object);
    
      std::cin.get();
    }
    
    int function(array& object)
    {
    
    }
    It works, but you would have to then use calculations to access elements of the array (quite easy ones), which I could post if you use this and dont know.

    By your last post you could have even figured it out, so heh.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    85
    IS there a way to do it without the class?????

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    85
    I am so confused?? I just want to be able to pass a 2d array that I declare in one function to another to be initialized to all zero's, and then be able to work with that in the original function.

  6. #6
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by swanley007
    I am so confused?? I just want to be able to pass a 2d array that I declare in one function to another to be initialized to all zero's, and then be able to work with that in the original function.
    As long as you know exactly how many elements you want to have in the array (which means you would HAVE to know how many elements when programming it, not based on parameters from main()). e.g.

    Code:
    #include <iostream>
    
    void function(int array[10][10]);
    
    int main()
    {
      int array[10][10];
    
      function(array);
    
      std::cout << array[1][1];
      std::cin.get();
    }
    
    void function(int array[10][10])
    {
      array[1][1] = 50;
    }
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    85
    Yup I got that to work, but as soon as I declare the array using a variable with an int in it...it stops working....If I could just find a way aroud that?

  8. #8
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by swanley007
    Yup I got that to work, but as soon as I declare the array using a variable with an int in it...it stops working....If I could just find a way aroud that?
    With an int in it? you mean like using x_max for the element instead of 10? or actually assigning an int (like 50) to say array[1][1], like I did, which works?

    Code examples, and more thoughtful explanation, would be nice.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    85
    like using x_max and y_max when declaring ex.
    Code:
    int max_x, max_y;
    max_x = 10;
    max_y = 10;
    int array[max_x][max_y];

  10. #10
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Pay attention in class.

    The elements (10, and 10) of the array in function(int array[10][10]) have to be the same as the ones you are passing. If you go off and change the elements in the array to pass in main() of course its going to give you errors. The only way to do it the way you want is to declare the variables before the prototype directive.

    Code:
    #include <iostream>
    
    int max_x, max_y;
    max_x = 10;
    max_y = 10;
    
    void function(int array[max_x][max_y]);
    
    int main()
    {
      int array[max_x][max_y];
    
      function(array);
    
      std::cin.get();
    }
    
    void function(int array[max_x][max_y])
    {
      for(int i = 0; i < max_x; ++i)
        for(int j = 0; j < max_y; ++j)
          array[i][j] = 0;
    }
    If you want to do it any other way you must use a class like I exampled (I'm a little ambiguous, but I'm 90% sure ).
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  11. #11
    Registered User
    Join Date
    Sep 2005
    Posts
    85
    Are you my Prof???? LOL! But I get the x_max and y_max from either default values or form the user supplied command line. I want to assign an in to x_max and y_max after I parse the command line and then declare the array using those values or the defaults

  12. #12
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Look up dynamic allocation of 2-dimensional arrays. Start with this:
    Code:
    int** array;
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  13. #13
    ---
    Join Date
    May 2004
    Posts
    1,379
    Tell me to go back to the C board if you want but is this legal in C++?
    Code:
    int main(){
      int x = 5;
      int arr[x];
      return 0;
    }

  14. #14
    Registered User
    Join Date
    Sep 2005
    Posts
    85
    Ya I know but the 2d array behaves differently

  15. #15
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Does your assignment say to do it exactly like that? because using the class would be more sensible.

    However searching google.com:
    http://www.eskimo.com/~scs/C-faq/q6.19.html

    Code:
    #include <iostream>
    
    void function(int* aryp, int nrows, int ncolumns);
    
    int main()
    {
      int max_x = 10;
      int max_y = 10;
        
      int array[max_x][max_y];
    
      function(&array[0][0], max_x, max_y);
    
      std::cout << array[1][1];
      std::cin.get();
    }
    
    void function(int* aryp, int nrows, int ncolumns)
    {
      aryp[1 * ncolumns + 1] = 50;
      //	{ ... array[i][j] is accessed as aryp[i * ncolumns + j] ... }
    }
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help passing nxn array to 2d array.
    By beglaryanh in forum C Programming
    Replies: 2
    Last Post: 06-06-2009, 05:23 PM
  2. Zero out two dimensional array
    By davo666 in forum C Programming
    Replies: 16
    Last Post: 01-08-2009, 04:28 AM
  3. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 02:56 AM
  4. Replies: 5
    Last Post: 11-20-2001, 11:48 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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