Help with Nested Loops, C

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2007
Posts: 55
Reputation: WhYuLoOkIn is an unknown quantity at this point 
Solved Threads: 3
WhYuLoOkIn's Avatar
WhYuLoOkIn WhYuLoOkIn is offline Offline
Junior Poster in Training

Help with Nested Loops, C

 
0
  #1
Apr 3rd, 2007
Please help I'm trying to write a program in C using nested loops which has an output of 1-8 (newline) 2-9 (newline) 3-10 (newline) 4-11(newline) 5-12. I wrote a program but I know it has to be a simplier way and I don't think I used nested loops correctly because the first for statement is irrilevant. PLEASE HELP


  1.  
  2. include <stdio.h>
  3.  
  4. int main()
  5. {
  6. int k, j, i,l, m, n;
  7.  
  8. for(i=0; i<1; i++)
  9. for(k=1; k<9; k++) printf("%d", k);
  10.  
  11. printf("\n\n");
  12.  
  13. for(j=2; j<10; j++) printf("%d", j);
  14.  
  15. printf("\n\n");
  16.  
  17. for(l=3; l<11; l++) printf("%d", l);
  18.  
  19. printf("\n\n");
  20.  
  21. for(m=4; m<12; m++) printf("%d", m);
  22.  
  23. printf("\n\n");
  24.  
  25. for(n=5; n<13; n++) printf("%d", n);
  26.  
  27.  
  28. return 0;
  29.  
  30. }
Doing what they say couldn't be done!!!!
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: Help with Nested Loops, C

 
0
  #2
Apr 3rd, 2007
First, format your code properly.

What does your book/instructor say about a FOR loop and/or statement? What's it do? What does it execute?

What does the program you wrote do? What does it not do?
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: Jan 2007
Posts: 55
Reputation: WhYuLoOkIn is an unknown quantity at this point 
Solved Threads: 3
WhYuLoOkIn's Avatar
WhYuLoOkIn WhYuLoOkIn is offline Offline
Junior Poster in Training

Re: Help with Nested Loops, C

 
0
  #3
Apr 3rd, 2007
The for loop allows one or more statements to be repeated for a specific number of times. It initializes a value, does a conditional test and increments or decrements???
Doing what they say couldn't be done!!!!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,035
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Help with Nested Loops, C

 
1
  #4
Apr 3rd, 2007
Originally Posted by WhYuLoOkIn View Post
Please help I'm trying to write a program in C using nested loops which has an output of 1-8 (newline) 2-9 (newline) 3-10 (newline) 4-11(newline) 5-12. I wrote a program but I know it has to be a simplier way and I don't think I used nested loops correctly because the first for statement is irrilevant. PLEASE HELP


[
Is the output expected to look like this?:

12345678
23456789
345678910
4567891011
56789101112

I am having trouble understanding your question.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 55
Reputation: WhYuLoOkIn is an unknown quantity at this point 
Solved Threads: 3
WhYuLoOkIn's Avatar
WhYuLoOkIn WhYuLoOkIn is offline Offline
Junior Poster in Training

Re: Help with Nested Loops, C

 
0
  #5
Apr 3rd, 2007
yes the output is expected to look like that.
Doing what they say couldn't be done!!!!
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 103
Reputation: mariocatch is an unknown quantity at this point 
Solved Threads: 17
mariocatch mariocatch is offline Offline
Junior Poster

Re: Help with Nested Loops, C

 
0
  #6
Apr 3rd, 2007
Here's a simply way to do it with a while loop. Just add more cases if you need to add another line of numbers.

  1.  
  2. bool Done = false;
  3.  
  4. while(!Done)
  5. {
  6. cout << i++ << ' ';
  7. if(i == 8)
  8. {
  9. i = 2;
  10. cout << '\n';
  11. }
  12.  
  13.  
  14. cout << i++ << ' ';
  15.  
  16. if(i == 9)
  17. {
  18. i = 3;
  19. cout << '\n';
  20. }
  21. cout << i++ << ' ';
  22.  
  23. if(i == 10)
  24. {
  25. i = 4;
  26. cout << '\n';
  27. }
  28. cout << i++ << ' ';
  29.  
  30.  
  31. if( i == 11)
  32. Done = true;
  33. }
Last edited by mariocatch; Apr 3rd, 2007 at 6:48 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,035
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Help with Nested Loops, C

 
1
  #7
Apr 3rd, 2007
Originally Posted by WhYuLoOkIn View Post
yes the output is expected to look like that.
This could be one of many ways of doing it. Take a look at it. Look also at the format for posting code.

  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. int a = 1;
  6. int b = 0;
  7. int c = 8;
  8.  
  9. for(a = 1; a <= 5; a++)
  10. {
  11. for(b = a; b <= c; b++)
  12. {
  13. printf("%d", b);
  14. }
  15. c++;
  16. putchar('\n');
  17. }
  18. getchar();
  19. return 0;
  20. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 55
Reputation: WhYuLoOkIn is an unknown quantity at this point 
Solved Threads: 3
WhYuLoOkIn's Avatar
WhYuLoOkIn WhYuLoOkIn is offline Offline
Junior Poster in Training

Re: Help with Nested Loops, C

 
0
  #8
Apr 3rd, 2007
Thanks I appreciate it, I'm going to study the logic of how you wrote this code, and I will also look at the format for posting. Thanks again
Doing what they say couldn't be done!!!!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,035
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Help with Nested Loops, C

 
0
  #9
Apr 3rd, 2007
Originally Posted by WhYuLoOkIn View Post
Thanks I appreciate it
You're welcome.

Now if you are pushing for more nested for loops, you could always get one more loop in the nest.

  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. int a,b,c;
  6.  
  7. for(a = 0; a < 5; a++)
  8. {
  9. for(b = 1; b <= 8; b++)
  10. {
  11. for(c = b; c <= 8; c++)
  12. {
  13. printf("%d", c);
  14. }
  15. putchar('\n');
  16. }
  17.  
  18. }
  19. getchar();
  20. return 0;
  21. }
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: Help with Nested Loops, C

 
0
  #10
Apr 3rd, 2007
Originally Posted by mariocatch View Post
Here's a simply way to do it with a while loop.
And how does that poorly formatted program help with
Originally Posted by WhYuLoOkIn View Post
I'm trying to write a program in C using nested loops which ...
:rolleyes:
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  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC