Nested for loops displaying 3 rows/11 column numbers

Writing a C++ Nested For loop to display 3 rows and 11 columns

Page 1 of 1

3 Replies - 31459 Views - Last Post: 22 February 2011 - 11:53 AM Rate Topic: -----

#1 cj2525   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,234
  • 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 


Was This Post Helpful? 0
  • +
  • -

#3 tehxaton   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.

View PostKYA, 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 


Was This Post Helpful? 0
  • +
  • -

#4 Guest_quay*


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;
}


Was This Post Helpful? 0

Page 1 of 1