Question about algorithm in loop

Sorry in advance for such a simple question I'm very new to c++ and just started last week. I'm trying to make a table that makes 3 rows and 11 columns and can't figure out why my algorithm is off so much. the output is supposed to read

1 0 1 2 3 4 5 6 7 8 9
2 0 2 4 6 8 10 12 14 16 18
3 0 3 6 9 12 15 18 21 24 27

Any help to point me in the right direction would be much appreciated.


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()
{
	for (int x = 1; x < 4; x++ )
	{ 
		cout << x << " ";
		for (int y = 0; y < 10; y++)
		{

			cout << y << " ";
			y = x * y;

		} //end for

		cout << endl;
	} //end for
	cout << endl;
	return 0;

} //end of main function  
Last edited on
In the inner for loop you are changing y to equal x*y, which means that loop will not go with y from 0 to 9 as expected. Instead, you should just cout "x * y" directly instead of screwing up your loop variable.
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()
{
    for (int x = 1; x <=3; x++ )
    {
        cout << "[R" << x << "] -> ";

        for (int y = 1; y <= 11; y++)
        {
            cout << 'C' << y << " ";

            //what do you want that for?
            //y = x * y;
        }

        cout << endl;
    }
    
    cout << endl;
    return 0;
}
Last edited on
Z : Thanks you! I knew it was something stupid.. I just cout like you stated and it worked flawlessly.

m4ster : thanks for writing that up.. it actually made me understand it a little more but I'm gonna keep playing around with it just to see different results.

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()
{
	for (int x = 1; x < 4; x++ )
	{ 
		cout << x << " ";
		for (int y = 0; y < 10; y++)
		{

			cout << x* y << " ";
			//y = x * y;

		} //end for

		cout << endl;
	} //end for
	cout << endl;
		system ("pause");
	return 0;


} //end of main function   
Topic archived. No new replies allowed.