473,465 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to display various pattern using for loop?

GraxGunz
13 New Member
Expand|Select|Wrap|Line Numbers
  1.   for (int i = 1; i <= 6; i++)
  2.   {
  3.     for (int j = 1; j <= i; j++)
  4.       cout << setw(3) << j;
  5.     cout << endl;
  6.   }
  7.  
above code will display below pattern:
1
1 2
1 2 3
1 2 3 4 5
1 2 3 4 5 6

Expand|Select|Wrap|Line Numbers
  1.   for (int i = 6; i >= 1; i--)
  2.   {
  3.     for (int j = 1; j <= i; j++)
  4.       cout << setw(3) << j;
  5.     cout << endl;
  6.   }
  7.  
and this code will display pattern below:
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

as what i understand
Expand|Select|Wrap|Line Numbers
  1.   for (int i = 6; i >= 1; i--)
  2.   {
  3.     cout << endl;
  4.   }
  5.  
above code is for creating the coloum
and
Expand|Select|Wrap|Line Numbers
  1.     for (int j = 1; j <= i; j++)
  2.       cout << setw(3) << j;
  3.  
is for print out the number from 1 to 6

my question here is what should i change or added he code above so that my output will display the pattern below:
__________1
________2_1
______3_2_1
____4_3_2_1
__5_4_3_2_1
6_5_4_3_2_1

and

1_2_3_4_5_6
__1_2_3_4_5
____1_2_3_4
______1_2_3
________1_2
__________1

note: _ is space

how can i print out my numbers that the alignment shift to the right? should i add space(" ") using if else loop for cout? or i might have a better solution??
Oct 26 '11 #1
1 8018
weaknessforcats
9,208 Recognized Expert Moderator Expert
Here is where you use a manipulator.

Write a function that takes an ostream& and pass in cout. Then use the ostrream& to display an underscore, then return the ostreanm&:

Expand|Select|Wrap|Line Numbers
  1. cout << "Hello!" << MyFunction(cout) << endl;
  2.  
where MyFunction is:

Expand|Select|Wrap|Line Numbers
  1. ostream& MyFunction(ostream& os)
  2. {
  3.     os << "_";
  4.     return os;
  5. }
  6.  
Oct 26 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: CV | last post by:
How can I match 'n' number of neighbouring words of a pattern using regular expressions? For example, suppose I am looking for the pattern "length xyz cm" in some text. where xyz is a number -...
4
by: Nicolla MacPherson | last post by:
Hi I'm a newbie and want to display a pattern that will increment with each line of display. I thought i might be able to count the rows and use that info to increment my display. I've got my...
1
by: bborden | last post by:
Novice Access programmer here. I would like to display an image using the Toolbox Image object by calling the images file name using: =fPictureFiles(!!,1) in the Picture control. Below...
7
by: c.verma | last post by:
I have a web application. There is a page which has a datagrid on it.The datagrid displays the data that comes from SAP. SAP sends the chinese characters to this grid. Before I display CHinese...
1
by: colinjack | last post by:
Hi All, I've been using the original (non-event based) asynchronous design pattern using delegates. It works fine but performance wise I know using the ThreadPool is supposed to be far better,...
1
by: lalithabhamidi | last post by:
I am trying to display an image using MagickimageDisplay using C API. But it asks for X server.So i am unable to display my image. Any Suggestions??
3
by: nico3334 | last post by:
I'm filling in a Report with SQL data using VB code. I'm using LOOP and MoveNext. Before using MoveNext, I would like to be able to check whether the new data is equal to the previous data that was...
5
by: boss1 | last post by:
hi all, i have a problem with loop in select statement.i m using code : <select name = "s" size = "1" > <option selected>P-Code</option> <option...
1
by: jelumalai | last post by:
I am using display:none with using Table. When show the onClick using javascript. Then it will show, again i will hide, content only hidden, but that table doesn't hide. <script> function...
4
by: selvamariappan | last post by:
Hi all , how to display various color for rows in datagrid in vb.net 2005, Not a Alternate colors, Thanks and regards selvamariappan.c
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.