Help complete typical question with for loops

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

Join Date: Nov 2004
Posts: 27
Reputation: Der_sed is an unknown quantity at this point 
Solved Threads: 0
Der_sed Der_sed is offline Offline
Light Poster

Help complete typical question with for loops

 
0
  #1
Nov 15th, 2004
the typical question which ask user to display THIS

*
**
***
****
*****
******
*******
********
*********
**********

using a single cout<<"*" & cout " " with the help of for loops ONLY.


ive come to this this prgram but can anyone tell me where i am going wrong and how to correct this error pls!!! :o



  1. #include <iostream.h>
  2. #include <conio.h>
  3. int main()
  4. {
  5. int a,b=1,c=10;
  6.  
  7. for ( a=10 ; a >=0 ; a--)
  8. {
  9.  
  10. for ( ; b>0 ; b--)
  11. {
  12. cout<<"*";
  13. }
  14.  
  15. for ( ; c>0 ; c--)
  16. {
  17. cout<<" ";
  18. }
  19. }
  20. getch();
  21. return 0;
  22. }




whats wrong here!!!!



THE PROBLEM CONTINUES ON.....but for now i need help till here :cry:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 56
Reputation: jasweb2002 is an unknown quantity at this point 
Solved Threads: 2
jasweb2002 jasweb2002 is offline Offline
Junior Poster in Training

Re: Help complete typical question with for loops

 
0
  #2
Nov 15th, 2004
First thing I see on quick glance is you need an endline command at the end of the for loop.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 56
Reputation: jasweb2002 is an unknown quantity at this point 
Solved Threads: 2
jasweb2002 jasweb2002 is offline Offline
Junior Poster in Training

Re: Help complete typical question with for loops

 
0
  #3
Nov 15th, 2004
Oh yeah, also your b and c variables are only initialized to work the first time through. After the first run b and c are equal to zero and therefore those two nested for loops will do nothing. I am heading off to class so I can't fix the problem for you right now but that is whats going to wrong. You need to find a way to reinitialize b and c for every pass through.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 12
Reputation: Extreme is an unknown quantity at this point 
Solved Threads: 0
Extreme Extreme is offline Offline
Newbie Poster

Re: Help complete typical question with for loops

 
0
  #4
Nov 15th, 2004
Hi....I am a c++ begginer and i hav a solution for ur problem of ur program...Check out the program I wrote..the program is below...Plz message me if ur problem is solved or if u want any help...


#include<iostream.h>

#include<conio.h>

void main()

{

int i, j;

for(i=0; i<10; i++)

{

for(j=0; j<=i; j++)

cout<<'*';

cout<<endl;

}

getch();

}
>>>Extreme<<<
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 27
Reputation: Der_sed is an unknown quantity at this point 
Solved Threads: 0
Der_sed Der_sed is offline Offline
Light Poster

Re: Help complete typical question with for loops

 
0
  #5
Nov 15th, 2004
THANX A LOT ETXREME!!!!! :o



but as our forum admin says u shud not help people do their homework!!!


definetly ive been tempted to copy ur program..........BUT !!!!!


as jasweb pointed out i must some re-initialise b and c to one less and one greater value !!!!!........but where exactly in the loops???? :mad:


pls tach me how to do that.....coz more important than assignment marks is that i understand how to do this tpe of stuff.......
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,971
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 920
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Help complete typical question with for loops

 
0
  #6
Nov 15th, 2004
Just Extreme's code a little tidied up for Dev C++
Hope you can see the flow of things.
  1. // Dev C++ code
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. int k, m;
  10.  
  11. for(k = 1; k <= 10; k++)
  12. {
  13. // outputs 1,2,3,...,10 stars
  14. for(m = 0; m < k; m++)
  15. cout << "*";
  16. // end each inner loop with a new line
  17. cout << endl;
  18. }
  19. cin.get(); // wait
  20. return 0;
  21. }
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 27
Reputation: Der_sed is an unknown quantity at this point 
Solved Threads: 0
Der_sed Der_sed is offline Offline
Light Poster

Re: Help complete typical question with for loops

 
0
  #7
Nov 15th, 2004
Originally Posted by vegaseat
Just Extreme's code a little tidied up for Dev C++
Hope you can see the flow of things.
  1. // Dev C++ code
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. int k, m;
  10.  
  11. for(k = 1; k <= 10; k++)
  12. {
  13. // outputs 1,2,3,...,10 stars
  14. for(m = 0; m < k; m++)
  15. cout << "*";
  16. // end each inner loop with a new line
  17. cout << endl;
  18. }
  19. cin.get(); // wait
  20. return 0;
  21. }


AHHH!!......so he's using the first loop variable in the test expression of the second loop!!!!!!!! :mrgreen:
NOW I GET IT!!!!!!!!!!!!!!!!!!!!!!!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 56
Reputation: jasweb2002 is an unknown quantity at this point 
Solved Threads: 2
jasweb2002 jasweb2002 is offline Offline
Junior Poster in Training

Re: Help complete typical question with for loops

 
0
  #8
Nov 15th, 2004
So you don't need to use cout << " "; as you originally stated?
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 27
Reputation: Der_sed is an unknown quantity at this point 
Solved Threads: 0
Der_sed Der_sed is offline Offline
Light Poster

Re: Help complete typical question with for loops

 
0
  #9
Nov 15th, 2004
MOOCH: MOOCH: !!!!!!!!



THNX ALOT PPL.......u solved alot of headache............esp. u EXTREME..........for clarifying the problem solvin technique.....and all others who helped me understand this simple problem...thnx alot :o




THIS FORUM ROX!!!! :evil:
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