Basic 2D array problem

Hi

I'm just trying to learn C++, so please bear with me if this is a stupid question.

I am trying to fill a 2D array with the times table of numbers 1 through 12.

The code attached shows what I have done. It appears to work fine, up to a point and then when it should fill in the value of 132 for 11*12, it fills in 1 and appears to crash the program.

Can someone see what I am doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <iostream>
using namespace std;

int main() {

	int timesTable[12][12];

	for (int i = 1; i < 13; i++) {

		for (int j = 1; j < 13; j++) {

			timesTable[i][j] = i * j;

			cout << timesTable[i][j] << " ";

		}
		cout << endl;


	}

	return 0;
}
Last edited on
First, your question is not stupid at all.
Second, you just made the typical "off by one error" just remember the array elements begin with 0 and not 1. Therefore, on lines 8 and 11, your for loop should begin with 0 and interact with each element until it reaches 11 or j < 12. Below is what I mean.

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

int main() {

    int timesTable[12][12];

    for (int i = 0; i < 12; i++) {

	   for (int j = 0; j < 12; j++) {

		  timesTable[i][j] = i * j;

		  //cout << i << " * " << j << " = " << i * j << endl;

		  cout << timesTable[i][j] << " ";

	   }
	   cout << endl;


    }

    return 0;
}
Thanks chicofeo

But that returned a table of 0 to 11. Such that the first row was filled with zeros and every column started with zeros.

What I have done now, is leave the code as it was originally, but made the array timesTable [13][13], and it appears to work.

Since my for loops are starting at 1, does it mean that the zero position of each column and the entire zero row is just filled with garbage?
It gives me the end result I am after, but just trying to understand what is really happening.

Since my for loops are starting at 1, does it mean that the zero position of each column and the entire zero row is just filled with garbage?


yes, also a waste of run-time memory...
does it mean that the zero position of each column and the entire zero row is just filled with garbage?


The zero position in your array is the first element.

In working with arrays, a common type of mistake is the off-by-one error . This is an easy mistake to make because array subscripts start at 0 rather than 1. For example, look at the following code:

1
2
3
4
5
// This code has an off-by-one error.
const int SIZE = 100;
int numbers[SIZE];
for (int count = 1; count <= SIZE; count++)
numbers[count] = 0;


The intent of this code is to create an array of integers with 100 elements, and store the
value 0 in each element. However, this code has an off-by-one error. The loop uses its counter variable, count , as a subscript with the numbers array. During the loop’s execution, the variable count takes on the values 1 through 100, when it should take on the values 0 through 99. As a result, the first element, which is at subscript 0, is skipped. In addition, the loop attempts to use 100 as a subscript during the last iteration. Because 100 is an invalid subscript, the program will write data beyond the array’s boundaries.

Below is the link to the array tutorial:

http://www.cplusplus.com/doc/tutorial/arrays/
So really what I need to do in my case is this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main() {

	int timesTable[12][12];

	for (int i = 0; i < 12; i++) {

		for (int j = 0; j < 12; j++) {

			timesTable[i][j] = (i+1) * (j+1);

			cout << timesTable[i][j] << " ";

		}
		cout << endl;


	}

	cout << endl << "That is the 12 times tables!";
	return 0;
Topic archived. No new replies allowed.