For Loop....Need Help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2009
Posts: 9
Reputation: dirnthelord is an unknown quantity at this point 
Solved Threads: 0
dirnthelord dirnthelord is offline Offline
Newbie Poster

For Loop....Need Help

 
0
  #1
May 20th, 2009
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:	12
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: For Loop....Need Help

 
0
  #2
May 20th, 2009
Trial and error I reckon and nested for loops.
Last edited by iamthwee; May 20th, 2009 at 12:59 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: For Loop....Need Help

 
0
  #3
May 20th, 2009
Sure, just post the code for one of them you're having trouble with.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 153
Reputation: amrith92 is on a distinguished road 
Solved Threads: 17
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster

Re: For Loop....Need Help

 
1
  #4
May 20th, 2009
Originally Posted by dirnthelord View Post
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.
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 53
Reputation: arshad115 is an unknown quantity at this point 
Solved Threads: 2
arshad115's Avatar
arshad115 arshad115 is offline Offline
Junior Poster in Training

Re: For Loop....Need Help

 
0
  #5
May 21st, 2009
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!
  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!
Somebody Rip My Heart Out, And Leave Me Here To Bleed!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 9
Reputation: dirnthelord is an unknown quantity at this point 
Solved Threads: 0
dirnthelord dirnthelord is offline Offline
Newbie Poster

Re: For Loop....Need Help

 
0
  #6
May 21st, 2009
Originally Posted by arshad115 View Post
i am going to tell the first one only,you can try the rest by yourself.

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

  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 53
Reputation: arshad115 is an unknown quantity at this point 
Solved Threads: 2
arshad115's Avatar
arshad115 arshad115 is offline Offline
Junior Poster in Training

Re: For Loop....Need Help

 
0
  #7
May 21st, 2009
thats good,just keep on trying!

you will get the results!
Somebody Rip My Heart Out, And Leave Me Here To Bleed!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC