1. 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

2. sorry the array_ini function will initialize the array...Iknow how to do

3. 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.

4. IS there a way to do it without the class?????

5. 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. 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;
}```

7. 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. 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.

9. 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. 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 ).

11. 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

Code:
`int** array;`

13. 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. Ya I know but the 2d array behaves differently

15. Does your assignment say to do it exactly like that? because using the class would be more sensible.

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] ... }
}```