Changing a 1d array to a 2d with global variables

Hi, i am writing a program that will pass an array with the integers 0-10 to a function that will create a multiplication table from those integers. I am supposed to be only using global variables to pass anything to and from the function. I am having trouble changing my 1d array to the 2d array in my function, i think i'll be able to do the global variable part. Here is what i have so far, still workin' on it though.

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

using namespace std;

void multiply_me(int nums[]);

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);

	cout<<mult[max]<<endl;

	return 0;
}

void multiply_me(int nums[])
{
	int i = 0;
	int j =0;

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

	return;
}
The problem is that you're using the same array for both your input and your output. As you write the output, you destroy the input....

Try changing the code so that multiply_me() prints out the table directly.
What does the error "expression must have pointer-to-object type" mean?

Is this close?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void multiply_me(int nums[])
{
	const int x = 11;
	const int y = 11;
	int i = 11;
	int j =0;
	int nums[x][y];

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

	cout<<nums[x][y];

	return;
}
You have declared two different "nums" arrays: one is the parameter to the function and the other is a local variable. You should call them something different. Actually, why do you need to local variable (line 7) at all? You' just need to print the multiplication table, not store it.

Since line 13 is outside both 4 loops, this function will produce just one line of output. Actually, not even a line, just a single number.

There are 11 numbers in the array that you pass to multiply_me(), but you're accessing it like there are 12. Why not pass in the size of the array so multiply_me() doesn't have to guess?

You're making this more complicated than it needs to be. All you need is:
- a loop to print the rows of the table.
- within that loop, a loop to print each value in the row.
Topic archived. No new replies allowed.