A logic problem, or a loop problem?

The exercise is:
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
#include <iostream>
#include <cmath>
using namespace std;
/*
Create a table of simple multiplication which populates a 
2 dimensional array to store and print a 5 by 6 multiplication
table starting at user defined values and increasing by one. 
For example if the user specified 3 and 4 the table would look like:
 	3	4	5	6	7      8
4	12	16	20	24	28     32     
5	15	20	25	30	35     40
6	18	24	30	36	42     48
7	21	28	35	42	49     56
8	24	32	40	48	56     64
*/

int main (void)
{
int i = 0;
int j = 0;
int MultTable [i][j];
int x = 0;
int y = 0;
cout << "Enter two values to start multiplying." << endl;
cin >> x;
cin >> y;
cout << endl;
cout << "\t" << x << "\t" << x+1 << "\t" << x+2 << "\t" << x+3 << "\t" << x+4 << "\t" << x+5 << endl;
for (i = y; i <= (y+4); i++)
	{
	cout << i << "\t";
	for (j = x; j <= (x+5); j++)
		{
		MultTable [i][j] = (i*j);
		cout << MultTable [i][j] << "\t";
		}
		cout << endl;
	}
return 0;
}


The output is


	3	4	5	6	7	8
4	12	16	20	24	28	0	
5	15	20	25	30	35	40	
6	18	24	30	36	42	48	
7	21	28	35	42	49	56	
8	24	32	40	48	56	64	


The error on the first line at 4*8 propagates through any run where 8 is calculated, like this:



	5	6	7	8	9	10
7	35	42	49	1028802884	63	70	
8	40	48	56	64	72	80	
9	45	54	63	72	81	90	
10	50	60	70	80	90	100	
11	55	66	77	88	99	110	


If 8 isn't in the matrix, it's fine:



	10	11	12	13	14	15
11	110	121	132	143	154	165	
12	120	132	144	156	168	180	
13	130	143	156	169	182	195	
14	140	154	168	182	196	210	
15	150	165	180	195	210	225	


WUT?
I'm surprised you aren't having much worse problems.

What dimensions does your array MultTable have?
ans: 0 x 0
proof:
1
2
3
int i = 0;
int j = 0;
int MultTable [i][j];

MultTable[0][0] might be valid, I'm not sure...

I suggest an array size of:int MultTable [5][6];
You can then only refer to elements [0][0] through [4][5].
No others exist, especially MultTable[14][14] = 196;

You will have to offset the array indexes:
1
2
3
4
5
6
7
8
9
10
for (i = y; i <= (y+4); i++)
	{
	cout << i << "\t";
	for (j = x; j <= (x+5); j++)
		{
		MultTable [i-y][j-x] = (i*j);// NOTE: indexes are offset so they stay in range
		cout << MultTable [i-y][j-x] << "\t";
		}
		cout << endl;
	}

This may fix your program.
What dimensions does your array MultTable have?
ans: 0 x 0

That's true, on that line, the array is initialized to a null array. Some people have said they don't like to initialize variables until they are necessary, but I like declaring them all in the beginning so I can play with them later and delete the ones I don't use.

I suggest an array size of:int MultTable [5][6];


This would work for this particular problem, but similar to above, I like leaving the array a little more flexible; at some point I may change the size of the array when playing with it, and if it's hardcoded in, I may forget I had done that and start doing things outside of the bounds of the array.

No others exist, especially MultTable[14][14] = 196;

Huh?

Of course MultTable [14][14] doesn't exist. I don't want a 14x14 2d array. However, inside MultTable[i][j], MultTable [3][4] does exist, which contains the values [i][j] which are 14*14 which = 196.

Offsetting the values of i and j does seem to make the output consistent. Thank you!
You're welcome.
macleight wrote:
Of course MultTable [14][14] doesn't exist.

Yet you were referring to this element in your code without the index offsets.
These offsets are essential.

I, of course, leave you at your will regarding array size declarations.
That's true, on that line, the array is initialized to a null array

who told you that is an array initialization. You have declaration problems. Array indexes must be const. Your code isn't going to cross the first 3 lines of main(). How in the world you got that output? copied it?
who told you that is an array initialization. You have declaration problems.


You seem angry and anti-fun.

Array indexes must be const.


Works fine this way every time I do it.

Your code isn't going to cross the first 3 lines of main().


But it does.

How in the world you got that output? copied it?


Yeah, that's what I do. I scour the internet for obscure code, then keep searching until I find seemingly appropriate output from somewhere else, that isn't ACTUALLY correct (but close), and post it on here, hoping that I can snare one of the people that enjoy problem solving and helping people on this forum into my wicked, wicked game. In the nude.
Works fine this way every time I do it.


Some compilers will let you do this even though it is not standard; you have no guarantee that it will compile on other compilers.

Waqar is correct in saying that array indexes must be const.

But even if that did work, you are creating an array of size 0 x 0 which makes no sense at all.

Go look up new to learn how to create dynamic arrays.

Yeah, that's what I do. I scour the internet for obscure code, then keep searching until I find seemingly appropriate output from somewhere else, that isn't ACTUALLY correct (but close), and post it on here, hoping that I can snare one of the people that enjoy problem solving and helping people on this forum into my wicked, wicked game. In the nude.


Lol.
Topic archived. No new replies allowed.