943,671 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 12759
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 3rd, 2007
0

Help with Nested Loops, C

Expand 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


  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
WhYuLoOkIn is offline Offline
55 posts
since Jan 2007
Apr 3rd, 2007
0

Re: Help with Nested Loops, C

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?
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,717 posts
since May 2006
Apr 3rd, 2007
0

Re: Help with Nested Loops, C

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???
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
WhYuLoOkIn is offline Offline
55 posts
since Jan 2007
Apr 3rd, 2007
1

Re: Help with Nested Loops, C

Click to Expand / Collapse  Quote originally posted by WhYuLoOkIn ...
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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Apr 3rd, 2007
0

Re: Help with Nested Loops, C

yes the output is expected to look like that.
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
WhYuLoOkIn is offline Offline
55 posts
since Jan 2007
Apr 3rd, 2007
0

Re: Help with Nested Loops, C

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.

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 11
Solved Threads: 17
Junior Poster
mariocatch is offline Offline
103 posts
since Apr 2007
Apr 3rd, 2007
1

Re: Help with Nested Loops, C

Click to Expand / Collapse  Quote originally posted by WhYuLoOkIn ...
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. }
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Apr 3rd, 2007
0

Re: Help with Nested Loops, C

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
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
WhYuLoOkIn is offline Offline
55 posts
since Jan 2007
Apr 3rd, 2007
0

Re: Help with Nested Loops, C

Click to Expand / Collapse  Quote originally posted by WhYuLoOkIn ...
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. }
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Apr 3rd, 2007
0

Re: Help with Nested Loops, C

Click to Expand / Collapse  Quote originally posted by mariocatch ...
Here's a simply way to do it with a while loop.
And how does that poorly formatted program help with
Click to Expand / Collapse  Quote originally posted by WhYuLoOkIn ...
I'm trying to write a program in C using nested loops which ...
:rolleyes:
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,717 posts
since May 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Nested loop in turbo C
Next Thread in C Forum Timeline: A binary search tree code





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


Follow us on Twitter


© 2011 DaniWeb® LLC