# Nested for loops displaying 3 rows/11 column numbers

Page 1 of 1

## 3 Replies - 31586 Views - Last Post: 22 February 2011 - 11:53 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=46304&amp;s=1eb0e7edf7e88831b1eb2a97a3acd654&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 cj2525

Reputation: 1
• Posts: 5
• Joined: 14-March 08

# Nested for loops displaying 3 rows/11 column numbers

Posted 14 March 2008 - 09:10 PM

I am new at programming and I am having trouble getting loops. Below is what I have for this problem. "Write a nested loop that displays a table with 3 rows and 11 columns." The first column has 1 through 3 and the following columns have the results of multiplying the first column by the numbers 0 through 9. It comes out as
1 0 1 2 3 4 5 6 7 8 9
2 0 1 3 7
3 0 1 4

but it is supposed to come out as:

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

What should I be doing?

[code]#include <iostream>

using std::cout;
using std::endl;

int main()
{
int y = 1;
int x = 1;

for (;x < 4; )
{
cout << x << " ";
for (y = 0; y < 10; y++)
{

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

} //end for

x = x + 1; //increment by 1
cout << endl;
} //end for
cout << endl;

return 0;
} //end of main function

Is This A Good Question/Topic? 0

## Replies To: Nested for loops displaying 3 rows/11 column numbers

### #2 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,235
• Joined: 14-September 07

## Re: Nested for loops displaying 3 rows/11 column numbers

Posted 14 March 2008 - 10:57 PM

You need a [*/code] tag at the end without the *.

First off, why not have your first for loop increment x when it's finished?
Here is some rewritten code that is easier to read (but you'll still need to fix your algorithm to have the correct output:

```#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

```

### #3 tehxaton

Reputation: 0
• Posts: 1
• Joined: 07-April 08

## Re: Nested for loops displaying 3 rows/11 column numbers

Posted 07 April 2008 - 05:12 AM

Thanks, for help on getting started with this. I'm curious how you could adapt this code, so that instead of multiplying like the OP wanted, instead use pow(x,y). So that the out put would look something like this.

1 1 1 1 1 1 1 1 1 1 1
2 1 2 4 8 16 32 64 128 256 512
3 1 3 9 27 81 243 729 2187 6561 19683

It's got me scratching my head and I would appreciate any help.

KYA, on 14 Mar, 2008 - 10:57 PM, said:

You need a [*/code] tag at the end without the *.

First off, why not have your first for loop increment x when it's finished?
Here is some rewritten code that is easier to read (but you'll still need to fix your algorithm to have the correct output:

```#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

```

Reputation:

## Re: Nested for loops displaying 3 rows/11 column numbers

Posted 22 February 2011 - 11:53 AM

```#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
for (int i=1;i <4; i++){
cout << i;
for (int j=0; j<10; j++)
{
cout << i*j;
}
cout << endl;
}

system("PAUSE");
return EXIT_SUCCESS;
}

```