944,164 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2770
  • C++ RSS
Nov 15th, 2004
0

Help complete typical question with for loops

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



C++ Syntax (Toggle Plain Text)
  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:
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Der_sed is offline Offline
31 posts
since Nov 2004
Nov 15th, 2004
0

Re: Help complete typical question with for loops

First thing I see on quick glance is you need an endline command at the end of the for loop.
Reputation Points: 11
Solved Threads: 2
Junior Poster in Training
jasweb2002 is offline Offline
56 posts
since Sep 2004
Nov 15th, 2004
0

Re: Help complete typical question with for loops

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.
Reputation Points: 11
Solved Threads: 2
Junior Poster in Training
jasweb2002 is offline Offline
56 posts
since Sep 2004
Nov 15th, 2004
0

Re: Help complete typical question with for loops

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();

}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Extreme is offline Offline
12 posts
since Nov 2004
Nov 15th, 2004
0

Re: Help complete typical question with for loops

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.......
Reputation Points: 10
Solved Threads: 0
Light Poster
Der_sed is offline Offline
31 posts
since Nov 2004
Nov 15th, 2004
0

Re: Help complete typical question with for loops

Just Extreme's code a little tidied up for Dev C++
Hope you can see the flow of things.
C++ Syntax (Toggle Plain Text)
  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. }
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 15th, 2004
0

Re: Help complete typical question with for loops

Quote originally posted by vegaseat ...
Just Extreme's code a little tidied up for Dev C++
Hope you can see the flow of things.
C++ Syntax (Toggle Plain Text)
  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!!!!!!!!!!!!!!!!!!!!!!!
Reputation Points: 10
Solved Threads: 0
Light Poster
Der_sed is offline Offline
31 posts
since Nov 2004
Nov 15th, 2004
0

Re: Help complete typical question with for loops

So you don't need to use cout << " "; as you originally stated?
Reputation Points: 11
Solved Threads: 2
Junior Poster in Training
jasweb2002 is offline Offline
56 posts
since Sep 2004
Nov 15th, 2004
0

Re: Help complete typical question with for loops

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:
Reputation Points: 10
Solved Threads: 0
Light Poster
Der_sed is offline Offline
31 posts
since Nov 2004

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: C++ Compilers
Next Thread in C++ Forum Timeline: Geting elements from a String Array





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


Follow us on Twitter


© 2011 DaniWeb® LLC