C++ nest loop program - Help

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2006
Posts: 9
Reputation: steveincolorado is an unknown quantity at this point 
Solved Threads: 0
steveincolorado steveincolorado is offline Offline
Newbie Poster

C++ nest loop program - Help

 
1
  #1
Sep 9th, 2006
Hi I am trying to write a small C++ program using nested loops so that when the program executes I have the following output displayed

  1. **
  2. ****
  3. ******
  4. ********
  5. **********
  6. ************

Here is the code I have written. I seem to be having a problem with proper loop execution. I would appreciate any help in determining where I have gone wrong. I print out 14 stars ad that is it.

Thank you Steve Swickard
  1. //I include any header files I need here
  2. #include <iostream>
  3. #include <string>
  4. using std::cin;
  5. using std::string;
  6. using std::cout;
  7. using std::endl;
  8.  
  9. int main()
  10. {
  11. //I declare variables and constants here
  12. const char STAR ='*';
  13. int i =0;
  14. int j =0;
  15. int k =0;
  16.  
  17.  
  18. for(; i<7; i++) // my first loop or out loop to execute
  19. //6 times
  20.  
  21. {
  22.  
  23. for (;j<k;j=j++) // this loop determines how many * to print
  24. {
  25. cout << STAR;
  26. }
  27. {
  28. for (;k !=14; k=k+2) // this loop determines the max # of *
  29. //to print
  30.  
  31. cout << endl;
  32. }
  33. }
  34. return 0;
  35.  
  36. } //end of main function
Last edited by Dave Sinkula; Sep 9th, 2006 at 7:34 pm. Reason: Added [code][/code] tags -- learn to use them yourself.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: C++ nest loop program - Help

 
1
  #2
Sep 9th, 2006
Format your code properly with code tags and finish your for statements (3 parameters, not 2) and repost...

Then we have something readable to work with.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 9
Reputation: steveincolorado is an unknown quantity at this point 
Solved Threads: 0
steveincolorado steveincolorado is offline Offline
Newbie Poster

Re: C++ nest loop program - Help

 
1
  #3
Sep 9th, 2006
Originally Posted by WaltP View Post
Format your code properly with code tags and finish your for statements (3 parameters, not 2) and repost...

Then we have something readable to work with.
  1. #include <iostream>
  2. #include <string>
  3. using std::cin;
  4. using std::string;
  5. using std::cout;
  6. using std::endl;
  7. int main()
  8. {
  9. const char STAR ='*';
  10. int k=2;
  11. for(int i=0; i<7; i++)
  12. {
  13.  
  14. for(int j=0;j<k;j=j++)
  15. {
  16. cout << STAR;
  17. }
  18. {
  19. for(int k=0;k < 14; k= k+2)
  20. cout << endl;
  21. }
  22. }
  23. return 0;
  24. } //end of main function
I believe I have it right. I apologize as this is my first post with code. If I understand right I needed to enclose my code in [] right? I re-wrote the for statements although I thought that it was okay to declare variables earlier in the program and just place a ";" in the place of the the variable declaration in the for statement? I was unable to get the k variable to compile without first declaring it.

Thank you for any assistance you might provide

Steve
Last edited by Dave Sinkula; Sep 9th, 2006 at 7:35 pm. Reason: Fixed [code][/code] tags.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: C++ nest loop program - Help

 
1
  #4
Sep 9th, 2006
Originally Posted by steveincolorado View Post
If I understand right I needed to enclose my code in [] right?
No. You enclose the code in tags. You could possiblly use the PREVIEW button to see if your post looks right...


Originally Posted by steveincolorado View Post
I re-wrote the for statements although I thought that it was okay to declare variables earlier in the program and just place a ";" in the place of the the variable declaration in the for statement? I was unable to get the k variable to compile without first declaring it.
Yes you should declare your variables earlier in the program. And yes you can just ";" if you know what you're doing. The first position is not a variable declaraction is a variable initialization, the first value the variable has thru the loop.

Also, formatting is of primary importance. Every time you use { indent 4 spaces. Just before you use every }, unindent.

Formatted properly, you code looks like:
  1. #include <iostream>
  2. #include <string>
  3. using std::cin;
  4. using std::string;
  5. using std::cout;
  6. using std::endl;
  7. int main()
  8. {
  9. int i;
  10. int j;
  11. int k;
  12.  
  13. const char STAR ='*';
  14. k=2; // What does this statement do for you?
  15. // What's the value the second time thru the I loop?
  16. for(i=0; i<7; i++)
  17. {
  18. for(j=0;j<k;j=j++)
  19. {
  20. cout << STAR;
  21. }
  22. { // Why are you starting a new block?
  23. for(k=0;k < 14; k= k+2)
  24. cout << endl;
  25. }
  26. }
  27. return 0;
  28. } //end of main function
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: C++ nest loop program - Help

 
1
  #5
Sep 9th, 2006
I don't see the reason for the 3rd loop
  1. for(int k = 0; k < 14; k += 2)
  2. cout << endl;
You'll be outputting 7 newlines between rows of stars... is that what you meant to do?
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 9
Reputation: steveincolorado is an unknown quantity at this point 
Solved Threads: 0
steveincolorado steveincolorado is offline Offline
Newbie Poster

Re: C++ nest loop program - Help

 
1
  #6
Sep 9th, 2006
Originally Posted by WaltP View Post
No. You enclose the code in tags. You could possiblly use the PREVIEW button to see if your post looks right...



Yes you should declare your variables earlier in the program. And yes you can just ";" if you know what you're doing. The first position is not a variable declaraction is a variable initialization, the first value the variable has thru the loop.

Also, formatting is of primary importance. Every time you use { indent 4 spaces. Just before you use every }, unindent.

Formatted properly, you code looks like:
  1. #include <iostream>
  2. #include <string>
  3. using std::cin;
  4. using std::string;
  5. using std::cout;
  6. using std::endl;
  7. int main()
  8. {
  9. int i;
  10. int j;
  11. int k;
  12.  
  13. const char STAR ='*';
  14. k=2; // What does this statement do for you?
  15. // What's the value the second time thru the I loop?
  16. for(i=0; i<7; i++)
  17. {
  18. for(j=0;j<k;j=j++)
  19. {
  20. cout << STAR;
  21. }
  22. { // Why are you starting a new block?
  23. for(k=0;k < 14; k= k+2)
  24. cout << endl;
  25. }
  26. }
  27. return 0;
  28. } //end of main function
WaltP it never occured to me at the time to preview my post but your absolutely correct and thank you for the pointer on initialization of the variable.

My thinking was that I since I needed to print six lines of star's that I would have the outer loop run six times, then I needed an inner loop to run six times but increment by 2 such that I would have another loop run 2 times, then 4, then 6, then 8, 10 and finally 12 times so that my cout <<STAR; statement would be printed by the same number each time through so that I would have the following output:

** print 2 times
**** print 4 times
****** print 6 times
******** print 8 times
********** print 10 times
************ print 12 times

I seem to have one loop running while the others do not run or they seem not not run I just want to know where I am going wrong thank you

Steve
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: C++ nest loop program - Help

 
1
  #7
Sep 9th, 2006
Originally Posted by steveincolorado View Post
WaltP it never occured to me at the time to preview my post but your absolutely correct and thank you for the pointer on initialization of the variable.
Hmmmm.... no comment

Originally Posted by steveincolorado View Post
My thinking was that I since I needed to print six lines of star's that I would have the outer loop run six times, ...
Good thought.

Originally Posted by steveincolorado View Post
then I needed an inner loop to run six times but increment by 2 such that I would have another loop run 2 times, then 4, then 6, then 8, 10 and finally 12 times so that my cout <<STAR; statement would be printed by the same number each time through so that I would have the following output:

** print 2 times
**** print 4 times
****** print 6 times
******** print 8 times
********** print 10 times
************ print 12 times
You just complexed it out of reason.

Let's think about this... (yeah, I know that's what we're doing... )
You have one outer loop that goes from 0 to 6
for(i=0; i<7; i++)
That's 7 lines... What's wrong here? That you can fix easily.

Now you want 6 lines of 2, 4, 6, 8... *s. Your loop goes 0, 1, 2, 3...
Is there a correlation between what you want and your loop?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 9
Reputation: steveincolorado is an unknown quantity at this point 
Solved Threads: 0
steveincolorado steveincolorado is offline Offline
Newbie Poster

Re: C++ nest loop program - Help

 
1
  #8
Sep 9th, 2006
Originally Posted by WaltP View Post
Hmmmm.... no comment


Good thought.


You just complexed it out of reason.

Let's think about this... (yeah, I know that's what we're doing... )
You have one outer loop that goes from 0 to 6
for(i=0; i<7; i++)
That's 7 lines... What's wrong here? That you can fix easily.

Now you want 6 lines of 2, 4, 6, 8... *s. Your loop goes 0, 1, 2, 3...
Is there a correlation between what you want and your loop?
Okay for your first question I should change the for statement to i<6

Yes now that you have pointed it out I see that there is a correlation between the two sets.

My outer loop is:
0, 1,2,3,4,5
the difference between that and what I need to print is
2,3,4,5,6,7
so the inner loop actually would appread to be the outer loop number +1 right?
so I could add 1 to i for the inner loop such as j=i+2; ?

Thank you
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: C++ nest loop program - Help

 
1
  #9
Sep 9th, 2006
Originally Posted by steveincolorado View Post
Okay for your first question I should change the for statement to i<6

Yes now that you have pointed it out I see that there is a correlation between the two sets.

My outer loop is:
0, 1,2,3,4,5
the difference between that and what I need to print is
2,3,4,5,6,7
so the inner loop actually would appread to be the outer loop number +1 right?
so I could add 1 to i for the inner loop such as j=i+2; ?

Thank you
In both cases -- BINGO! Two loops is all you need. One outer for the lines, one inner for the *s based on teh line you're on...
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 9
Reputation: steveincolorado is an unknown quantity at this point 
Solved Threads: 0
steveincolorado steveincolorado is offline Offline
Newbie Poster

Re: C++ nest loop program - Help

 
1
  #10
Sep 9th, 2006
Thank you. I am currently coding the program and I will see if I have it thank for your help

Steve
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



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