passing 2-d array to a function

I'm new to c++ programming and I'm trying to pass a 2d array into a function for input, but I keep getting the
"cannot convert parameter 1 from 'unsigned int [12][3]' to 'unsigned int *"
error. Also I can't use templetes and I have to use 2d array, its a requirement by my professor to use 2d arrays. This problem is from the book
"C++ Primer Plus 5th edition" by Stephen Prata

Thank you

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
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
using namespace std;

void monthlysales(unsigned int*, unsigned int, unsigned int);

const unsigned int x = 12;	
const unsigned int z = 3;	

const string year[z] =
{"Year 1","Year 2","Year 3",};

const string months[x] =
{"Janurary ","Feburary ","March    ",
 "April    ","May      ","June     ",
 "July     ","August   ","September",
 "October  ","November ","December "};

int main()	
{
 unsigned int totalbooksold[x][z];

 monthlysales(totalbooksold, x, z);

 system("pause");
 return 0;
}

void monthlysales(unsigned int booksoldmonthly[x][z], unsigned int y, unsigned int s)
{
for (int i = 0; i < x; i++)
	{
	cout	<< "Enter the amount of book sold for " << months[i]
			<< " of " << year[0] << ": ";
	cin		>> booksoldmonthly[i][s-3];
	}
for (int i = 0; i < x; i++)
	{
	cout	<< "Enter the amount of book sold for " << months[i]
			<< " of " << year[1] << ": ";
	cin		>> booksoldmonthly[i][s-2];
	}
for (int i = 0; i < x; i++)
	{
	cout	<< "Enter the amount of book sold for " << months[i]
			<< " of " << year[2] << ": ";
	cin		>> booksoldmonthly[i][s-1];
	}
	return;
}

Your prototype says you are passing a pointer, but when you pass a 2-d array that's a pointer to a pointer. The prototype should say 'unsigned int**'.
I've tried that by changeing
1
2
3
4
5
void monthlysales(unsigned int**, unsigned int, unsigned int);
/*from void monthlysales(unsigned int*, unsigned int, unsigned int) from line 5*/

void monthlysales(unsigned int **booksoldmonthly[x][z], unsigned int y, unsigned int s)	
/*from void monthlysales(unsigned int **booksoldmonthly[x][z], unsigned int y, unsigned int s)	from line 29 */


But I still get the
"cannot convert parameter 1 from 'unsigned int [12][3]' to 'unsigned int *"
from line 23
and also the erros from line 35, 41, 47
" binary '>>' : no operator found which takes a left-hand operand of type 'std::istream'"
The prototype should say unsigned int**, as should the definition. unsigned int **booksoldmonthly[x][z] is a 4-d array, which does not match the prototype. Just use unsigned int** booksoldmonthly with both. Having the sizes of the dimensions doesn't do anything for the function and I believe is in fact not allowed with the first dimension.
Er, his data is a straight 2D array. He should not be passing an unsigned int**, because his data is not an array of pointers, but an array of integers.

The compiler is complaining because it is not an automatic cast from a 2D array to a pointer/1D array, and because his prototype does not match the actual function definition.

To fix:

5
6
7
8
const unsigned int x = 12;
const unsigned int z = 3;

void monthlysales(unsigned int booksoldmonthly[x][z], unsigned int y, unsigned int s);

Hope this helps.
Problem solved Thanks for your help.
Here is the solved code for those who may encounter the same problem as me.

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
#include <iostream>	
#include <string>
using namespace std;

void monthlysales(unsigned int**, unsigned int, unsigned int);

const unsigned int x = 12;
const unsigned int z = 3;

const string year[z] =	

{"Year 1","Year 2","Year 3",};

const string months[x] =  
{"Janurary ","Feburary ","March    ",
 "April    ","May      ","June     ",
 "July     ","August   ","September",
 "October  ","November ","December "};

int main()	
{
unsigned int** totalbooksold = new unsigned int* [x]

for(int i = 0; i < 12; i++)								
{
    totalbooksold[i] = new unsigned int[z];
}

monthlysales(totalbooksold, x, z);

 system("pause");
 return 0;
}

void monthlysales(unsigned int** booksoldmonthly, unsigned int y, unsigned int s)
{
// Some codes here that I'm to lazy to ctrl+c ctrl+v
}
Topic archived. No new replies allowed.