954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

For Loop....Need Help

I am trying to implement these patters using for loop....I am having difficulties with them....If someone can help, would be great!!
I attached the pattern to a Image....

For_loop.JPG


I tries this code for the third pattern...I have no Idea how to get them done....can anyone post codes for all of them? int n=1,y=1,i=1,x=5;

for (i=1;i<=5;i++)
{ cout<<setw(x);
for(n=1;n<=y;n++)
{

cout<<"5";
}
cout<<endl;

y++;
x--;
}

Attachments For_loop.JPG 24.55KB
dirnthelord
Newbie Poster
18 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

Trial and error I reckon and nested for loops.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Sure, just post the code for one of them you're having trouble with.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 
I tries this code for the third pattern...I have no Idea how to get them done....can anyone post codes for all of them?

This statement, to me, sounds like you are trying to get us to do your homework for you. Youmust show some measure of good faith and post the code that you have already written (not the ones that you can get to work, obviously), so that we are in a better position to correct any mistakes in it. We (at least, I) will not waste time to write full homework assignments, unless it is our own assignments :P ...

amrith92
Junior Poster
188 posts since Jul 2008
Reputation Points: 130
Solved Threads: 23
 

i am going to tell the first one only,you can try the rest by yourself.

think of first figure as four triangles!

1.thats counts and prints in ascending order
2.thats counts and prints in descending order
3.one that prints spaces " " in ascending order
4.this one also prints spaces " " in ascending order

first of all determine the number of rows you want to print.
here we have 5

so this means we will have to make an outer loop that runs 5 times
,then come the nested loops!....

then all you have to do is,check all the conditions.do some dry runs on your copy first.this way you will understand!

you wont be able to do this unless you try it yourslef!
just practice practice and practice!

here is the code,i had a similar assignment ,way back.changed it a bit.but it prints "5" twice,you figure it out yourself!

for(int x=5;x>0;x--)
	{
		for(int b=0;b<x;b++)
		{
			cout << b+1;
		}
		for(int c=5;c>x;c--)
		{
			cout << " ";
		}
		for(int e=4;e>x;e--)
		{
			cout << " ";
		}
		for(int d=x;d>0;d--)
		{
			cout << d;
		}
		cout << endl;
	}


this is just for learning,try to make your own codes!

arshad115
Junior Poster in Training
65 posts since Nov 2008
Reputation Points: 7
Solved Threads: 2
 

i am going to tell the first one only,you can try the rest by yourself.

for(int x=5;x>0;x--)
	{
		for(int b=0;b<x;b++)
		{
			cout << b+1;
		}
		for(int c=5;c>x;c--)
		{
			cout << " ";
		}
		for(int e=4;e>x;e--)
		{
			cout << " ";
		}
		for(int d=x;d>0;d--)
		{
			cout << d;
		}
		cout << endl;
	}

this is just for learning,try to make your own codes!


Thank you.....after little bit of effort I got this result,but not exactly what is supposed to print..

int c=1,a=5,d=1;
for(int i=1;i<=5;i++)
{
	for(int x=5;x>=i;x--)
	{
		cout<<c;
		
		if(c>=a)
		{	cout<<setw(d);
			for(int y=5;y>=x;y--)
			{
				cout<<c;
				c--;
			}
		}	
		c++;
	}
	c=1;
	a--;
	d+=2;
	cout<<endl;
}


what should I do?

dirnthelord
Newbie Poster
18 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

thats good,just keep on trying!

you will get the results!

arshad115
Junior Poster in Training
65 posts since Nov 2008
Reputation Points: 7
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You