Passying 2 int to a function then trying to use them to set a array

I am trying to call the following function as so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int displayTable(int x, int y)
{
	int mTable[x][y];

	cout << "\n\n___________________________________________\n";

	for (int i = 0; i < y; i++)
	{
		for (int i2 = 0; i < x; i++)
		{
			cout << "|";
			mTable[i2][i] = i2 + i;
		}
		cout << "|\n";
		cout << "------------------------------------------------------\n";
	}

	return mTable;
}


I am getting a error saying that x and y in the int mTable[x][y]; have to be const. Not sure how to get around this because I don't know what the size of the array is going to be until the user gives me x and y.

Thanks for the help.
Last edited on
Well the return value of your function is currently an int =) hint hint

think about the difference between and int and an int[][]
Guessing this has something to do with the address of the array but not sure how to go about fixing it atm. Here is all the code I have so far, if that helps.

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
52
53
54
55
56
57
58
59
60
61
62
#include "stdafx.h"
#include <iostream>
using namespace std;

int userInput();
int displayTable(int x, int y);

void main()
{
	displayTable(userInput(),userInput());
}

int userInput()
{
	int num;

	do
	{
		cout << "Please enter a positive integer for your multiplication table: ";
		cin >> num;

		if(cin.fail())
		{
			cin.clear();
			cin.ignore(1000, '\n');
			cout << "\nInvaild integer, try again.\n";
			continue;
		}

		cin.ignore(1000, '\n');
		
		if (cin.gcount() > 1)
		{
			cout << "\nInvaild integer, try again.\n";
			continue;
		}
		
		break;
	}while(true);

	return (num);
}

int displayTable(int x, int y)
{
	int mTable[x][y];

	cout << "\n\n___________________________________________\n";

	for (int i = 0; i < y; i++)
	{
		for (int i2 = 0; i < x; i++)
		{
			cout << "|";
			mTable[i2][i] = i2 * i;
		}
		cout << "|\n";
		cout << "------------------------------------------------------\n";
	}

	return mTable;
}
Last edited on
Dynamic allocation required. If you are declaring in static required constant value say int mTable[4][5];


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
int displayTable(int x, int y)
{
	int **mTable = (int*) malloc (sizeof(int*)*y);
                int *temp = (int*)mtable;

               for (int i = 0; i < y; i++)  {
                 temp[i] = (int*) malloc (sizeof (int)* x);
               }
      
              
	cout << "\n\n___________________________________________\n";

	for (int i = 0; i < y; i++)
	{
		for (int i2 = 0; i < x; i++)
		{
			cout << "|";
			mTable[i2][i] = i2 + i;
		}
		cout << "|\n";
		cout << "------------------------------------------------------\n";
	}

	return mTable;
}
@richardforc

I tried putting in the code you added above but I throws up errors (I even tried changing mtable to mTable in the second statement).

Is there a easier way to go about setting up this array based on user input? Trying to find a way that I can understand at my level and build on from there.

Thanks again for all the help.
Oh good heavens, just use a 2d vector...
Really I see absolutely no reason to use an array in your case...
You would want to change the return value of your function to an integer pointer if you really want to use arrays. If you want to dynamically alloc in c++ you have to first make an array of pointers of size x or y then call new on each of those pointers of the other size... Not very simple and very c-like.

1
2
3
4
5
6
int **parray;
int x,y;

paray = new int* [x];
for(int i=0; i<x; i++)
paray[i] = new int [y];


however you can't forget to delete each individual pointer...
Topic archived. No new replies allowed.