Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Loop (continued)
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
JWHooper




PostPosted: Sun Feb 24, 2008 10:23 pm   Post subject: Loop (continued)

I'm having a little difficulty on this one:

Use nested loops that print the following pattern.
code:

1 2 3 4 5 6
  1 2 3 4 5
    1 2 3 4
      1 2 3
        1 2
          1


Here's what I did:
code:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
        for (int i = 6; i >= 1; i--)
        {
                for (int j = 1; j <= i; j++)
                {
                        cout << setw(3) << j;
                }
                cout << endl;
        }

        return 0;
}


The result was:
code:

1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1


I want to get the 2nd through the 6th row kinda moved to right, to make it look like the one above. I'm having a difficulty rewriting the code. Can anyone help, please?




Thanks,

J.
Sponsor
Sponsor
Sponsor
sponsor
OneOffDriveByPoster




PostPosted: Mon Feb 25, 2008 1:06 am   Post subject: Re: Loop (continued)

You can still print spaces to get want...
md




PostPosted: Tue Feb 26, 2008 11:09 am   Post subject: RE:Loop (continued)

use setw perhaps?

#include <iomanip>

and then cout << setw(12) << n;

The easiest way is probably to use string streams. The fastest and best way is probably to output spaces, how many spaces depends on the line.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: