Changing array from 1d to 2d in a function

I am confused as to how i am supposed to pass the 1d array to the function, create a 2d array (multiplication table) and then send it back to the main to be output the the screen. Also, anything that is passed to or from the function must use global variables. I know i am not THAT close, but this is what i have so far. Any suggestions would be greatly appreciated.

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<iostream>
#include<iomanip>
#include<cmath>
#include<string>

using namespace std;

void multiply_me(int nums[]);
int j = 0;
int i = 0;
int main()
{
	const int max = 11;
	int mult[max] = {0,1,2,3,4,5,6,7,8,9,10};

	cout<<"This program will output the multiplication tables for the integers 0-10"<<endl;
	
	multiply_me(mult);
	 
	for(i = 0; i<11; i++)
		for(j = 0; j<11; j++)
			cout<<mult[x][y];


	return 0;
}

void multiply_me(int nums[])
{
	const int x = 11;
	const int y = 11;

	int nums[x][y];

	for(i = 0; i<12; i++)
		for(j = 0; j<12; j++)
			 nums[x][y] = nums[i]*nums[j];
	

	return;
}
closed account (SECMoG1T)
1
2
3
4
5
nums[x][y];//// line 33 this in no global array hence line 22 is an error

You can make your function multiply_me return a 2d array or make it a simple parameteru
//what exactly will that 2d array contain; am not sure multiplication results or just value 1-12?
because if it will contain the results you will need a 12 by 12 array instead
Last edited on
Like so:
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<iostream>
#include<iomanip>
#include<cmath>
#include<string>

using namespace std;

const int x = 11;
const int y = 11;

void multiply_me(int result_nums[x][y], int nums[]); // Note
int j = 0;
int i = 0;
int main()
{
	const int max = 11;
	int mult[max] = {0,1,2,3,4,5,6,7,8,9,10};

	cout<<"This program will output the multiplication tables for the integers 0-10"<<endl;

	int result_mult[x][y]; // Note
	
	multiply_me(result_mult, mult); // Note
	 
	for(i = 0; i<11; i++)
		for(j = 0; j<11; j++)
			cout<<result_mult[i][j]; // Note: [EDIT] Bug removed pointed out by andy1992


	return 0;
}

void multiply_me(int result_nums[x][y], int nums[])
{
	for(i = 0; i<11; i++) // Note: 11
		for(j = 0; j<11; j++) // Note: 11
			 result_nums[i][j] = nums[i]*nums[j]; // Note: i/j instead of x/y
	

	return;
}
Last edited on
closed account (SECMoG1T)
That is eexactly what you were asking for @coder guided you step by step, good luck

//except for line 27 am sure he did it so fast, replace x, y with i, j
closed account (48T7M4Gy)
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<iostream>

int** multiply_me(int[], const int);

int main()
{
	const int max = 11;
	int mult[max] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

	std::cout << "This program will output the multiplication tables for the integers 0-10" << std::endl;

	int** answer = multiply_me(mult, max);

	for (int i = 0; i < max; i++)
	{
		for (int j = 0; j < max; j++)
		{
			std::cout << answer[i][j] << "  ";
		}

		std::cout << std::endl;
	}


	return 0;
}

int** multiply_me(int numbers[], const int size)
{

	int** table = new int*[size];

	for (int i = 0; i < size; ++i)
		table[i] = new int[size];

	for (int i = 0; i < size; i++)
	{
		for (int j = 0; j < size; j++)
			table[i][j] = i * j;
	}

	return table;
}
Thank you for the input!
Topic archived. No new replies allowed.