2d arrays and Functions

Hi, I have this code here
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <cstdlib>
#include <iostream>
#include <cstdio>

using namespace std;

void generate_solid_earth(int x, int y, char earth, char **array) {
     for (int i = 0; i < x; i++) {
         for (int j = 0; j < y; i++) {
            array[x][y] = earth; 
         }
     }
}
void print_world(char **array, int x, int y) {
     for (int i = 0; i < x + 1; i++) {
         for (int j = 0; j < y; i++) {
             if (i == x + 1) {
                cout << '\n';      
             }
             else {
                cout << array[x][y];    
             } 
         }
     }
}

int main(int argc, char *argv[])
{
    //Solid Earth Generation
    int x;
    int y;
    char earth = '.';
    cout << "Please Enter X" << endl;
    cin >> x;
    cout << "Please Enter Y" << endl;
    cin >> y;
    char world[x][y];
    generate_solid_earth(x,y,earth,world);
    system("PAUSE");
    return EXIT_SUCCESS;
}


I get an error involving this bit
1
2
    char world[x][y];
    generate_solid_earth(x,y,earth,world);


Any help

Thanks

EDIT: Heres the error 38 F:\C++\Thing\main.cpp cannot convert `char (*)[((unsigned int)((int)y))]' to `char**' for argument `4' to `void generate_solid_earth(int, int, char, char**)'
Last edited on
char** is not equivalent to char[][]. Also, dimensions of an array must be constants. Even though some compilers allow variables, this is not standard c++.
See http://www.cplusplus.com/forum/articles/17108/
WHat if I made it an array of array's, would that work?

EDIT: Sorry, thats the same thing :(
Last edited on
Yeah, passing a multi-dimensional array doesn't work very well. However I believe a vector could be used in place of the array.
Last edited on
Im not sure on the syntax for that, could you please explain?

EDIT: Solved, I used a 1d array as a 2d array and it has worked, I use the way said in the above link and it worked, Thank you all for your help
Last edited on
Here's a quick example of using a vector similar to how you were trying to use an array, I had to take a bit of a crash course in the vector class but it works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <vector>
#include <iostream>
using namespace std;

void fillVector(int height, int width, char fill, vector<vector<char> > &mdVector)
{
	for (int row = 0; row < height; row++)
	{
		for (int col = 0; col < width; col++)
		{
			mdVector[row][col] = fill;
		}
	}
}

void printVector(int height, int width, vector<vector<char> > &mdVector)
{
	for (int row = 0; row < height; row++)
	{
		for (int col = 0; col < width; col++)
		{
			cout << mdVector[row][col];
		}
		cout << endl;
	}
}

int main()
{
	int height, width;
	char fill;
	
	cin >> height;
	cin >> width;
	cin >> fill;

	vector<vector<char> > mdVector(height, vector<char> (width));

	fillVector(height, width, fill, mdVector);
	printVector(height, width, mdVector);

	return 0;
}


It sets up a vector to emulate a 2D array, with line 37 taking place of array[x][y]; and then passes it to the functions by reference. You can index an element in a vector like you would an array. This is probably rather lacking in way of explaining how it works, but I figured I'd put togethor some working code using a vector to show you an alternative to an array (while learning about vectors myself at the same time). xD
Topic archived. No new replies allowed.