943,608 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 556
  • C++ RSS
May 20th, 2009
0

For Loop....Need Help

Expand Post »
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....

Click image for larger version

Name:	For_loop.JPG
Views:	28
Size:	24.6 KB
ID:	10180


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--;
	}
Last edited by dirnthelord; May 20th, 2009 at 1:17 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dirnthelord is offline Offline
18 posts
since Jan 2009
May 20th, 2009
0

Re: For Loop....Need Help

Trial and error I reckon and nested for loops.
Last edited by iamthwee; May 20th, 2009 at 12:59 pm.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 20th, 2009
0

Re: For Loop....Need Help

Sure, just post the code for one of them you're having trouble with.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
May 20th, 2009
1

Re: For Loop....Need Help

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. You must 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 ...
Last edited by amrith92; May 20th, 2009 at 1:35 pm.
Reputation Points: 130
Solved Threads: 22
Junior Poster
amrith92 is offline Offline
187 posts
since Jul 2008
May 21st, 2009
0

Re: For Loop....Need Help

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!
c++ Syntax (Toggle Plain Text)
  1.  
  2.  
  3. for(int x=5;x>0;x--)
  4. {
  5. for(int b=0;b<x;b++)
  6. {
  7. cout << b+1;
  8. }
  9. for(int c=5;c>x;c--)
  10. {
  11. cout << " ";
  12. }
  13. for(int e=4;e>x;e--)
  14. {
  15. cout << " ";
  16. }
  17. for(int d=x;d>0;d--)
  18. {
  19. cout << d;
  20. }
  21. cout << endl;
  22. }

this is just for learning,try to make your own codes!
Reputation Points: 7
Solved Threads: 2
Junior Poster in Training
arshad115 is offline Offline
64 posts
since Nov 2008
May 21st, 2009
0

Re: For Loop....Need Help

Click to Expand / Collapse  Quote originally posted by arshad115 ...
i am going to tell the first one only,you can try the rest by yourself.

c++ Syntax (Toggle Plain Text)
  1.  
  2.  
  3. for(int x=5;x>0;x--)
  4. {
  5. for(int b=0;b<x;b++)
  6. {
  7. cout << b+1;
  8. }
  9. for(int c=5;c>x;c--)
  10. {
  11. cout << " ";
  12. }
  13. for(int e=4;e>x;e--)
  14. {
  15. cout << " ";
  16. }
  17. for(int d=x;d>0;d--)
  18. {
  19. cout << d;
  20. }
  21. cout << endl;
  22. }

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..

c++ Syntax (Toggle Plain Text)
  1. int c=1,a=5,d=1;
  2. for(int i=1;i<=5;i++)
  3. {
  4. for(int x=5;x>=i;x--)
  5. {
  6. cout<<c;
  7.  
  8. if(c>=a)
  9. { cout<<setw(d);
  10. for(int y=5;y>=x;y--)
  11. {
  12. cout<<c;
  13. c--;
  14. }
  15. }
  16. c++;
  17. }
  18. c=1;
  19. a--;
  20. d+=2;
  21. cout<<endl;
  22. }

what should I do?
Last edited by dirnthelord; May 21st, 2009 at 11:52 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dirnthelord is offline Offline
18 posts
since Jan 2009
May 21st, 2009
0

Re: For Loop....Need Help

thats good,just keep on trying!

you will get the results!
Reputation Points: 7
Solved Threads: 2
Junior Poster in Training
arshad115 is offline Offline
64 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help with loop
Next Thread in C++ Forum Timeline: I got stuck on loading the matrix from file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC