2D array with unknown values- multiplication table

I want to print out a multiplication table based on how big the user wants it to be. I also want this program to do this via functions.

Right now, the code runs, but I get a few errors:
1. "Stack around the variable 'table' was corrupted.
2. A buffer overrun has occurred in multTable.exe which has corrupted the program's internal state

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
/*
Create a multiplication table with numbers 1-10
*/

#include <iostream>
using namespace std;

int main()
{
	int table[10][10];

	//assigns each element
	for (int i = 1; i <= 10; i++)
	{
		for (int j = 1; j <= 10; j++)
		{
			table[i][j] = i * j;
		}
	}
	//prints the table
	for (int i = 1; i <= 10; i++)
	{
		for (int j = 1; j <= 10; j++)
		{
			cout << table[i][j] << '\t';
		}
	}
	system("pause");
	return 0;
}
You are accessing elements that don't belong to the array.

Think about this:
When you're accessing an array with an index, the first element is always zero.

Consider the following code:

1
2
3
int arr[10];
arr[0] = 123;//assigning value to the first element of "arr"
arr[9] = 321;//assigning value to the last element of "arr" 


Try to figure out why your for loops are incorrect.

so they should be:
 
for(int i = 0; i < 10; i++)


I want the multiplication table to start out at 1 (not zero).

The main goal is to do this using functions though.
Last edited on
Now it prints the table how i want... But I am extremely confused how to pass multidimensional arrays to functions... I understand single dimension arrays, but I am so confused going for 2D.

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
/*
Create a multiplication table with numbers 1-10
*/

#include <iostream>
using namespace std;

int main()
{
	int table[11][11];

	//assigns each element
	for (int i = 0; i < 11; i++)
	{
		for (int j = 0; j < 11; j++)
		{
			table[i][j] = i * j;
		}
	}
	//prints the table
	for (int i = 1; i < 11; i++)
	{
		for (int j = 1; j < 11; j++)
		{
			cout << table[i][j] << '\t';
		}
	}
	system("pause");
	return 0;
}
Passing multidimensional arrays to functions isn't all that different from normal arrays. Here is one way of doing it:
1
2
3
4
5
6
7
8
void doFunc(int** array, int height, int width) {
    // do something with the array, for example:
    for (int row = 0; i < height; ++row) {
        for (int col = 0; i < width; ++col) {
            cout << array[row][col] << '\t';
        }
    }
}


EDIT:
Also, for your problem with the arrays, how about you just have a 10x10 array and simply set the result of the table to be table[i][j] = (i+1) * (j+1)?
Last edited on
ok, so when i try to initialize the 2D array in main...

1
2
3
int row = 10;
int col = 10;
int *table = new int[row][col];


it says: Error: expression must have a constant value. Which makes sense as you have to know at least the last value. I understand this concept, I just don't know how to go from here.
Topic archived. No new replies allowed.