Page 1 of 3 123 LastLast
Results 1 to 10 of 25

Thread: C prog. nested loops?

  1. #1
    Join Date
    Aug 2007
    Beans
    79

    C prog. nested loops?

    I didnt really feel like I should post in this section because I'm just learning to program C on my own from C Primer Plus but i'm stuck on one of these exercises in chapter 6 question 2. ok here is the exercise

    2. Use nested loops to produce the following pattern:

    $
    $$
    $$$
    $$$$
    $$$$$
    ------------------------------------------------
    so I am suppose to create this using a for loop I'm pretty sure so if u can explain this to me with out giving me the answer I would really appreciate it and if you could explain it without giving me things that dont come until later chapters i'd appreciate it

  2. #2
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,270
    Distro
    Kubuntu

    Re: C prog. nested loops?

    Code:
    #include <stdio.h>
    
    int main()
    {
      int i, j;
      for ( i = 0; i < 5; ++i )
      {
        for ( j = 0; j < i+1; ++j )
        {
          printf( "$" );
        }
        printf( "\n" );
      }
    }

    I can't believe you are already "cheating" on what appears to be a first assignment!

    Here's the explanation: The outer loop increments the variable i by 1 each time it loops, for 5 loops, and just at the end of the loop prints a carriage return. The interior loop is the one responsible for printing the $ character(s). It prints a number of $ character(s) equal the number of times the outer for-loop has been executed. I hope this makes sense.
    Last edited by dwhitney67; September 9th, 2007 at 12:19 AM.

  3. #3
    Join Date
    Aug 2007
    Beans
    79

    Re: C prog. nested loops?

    I am not cheating I didn't want the answer I wanted someone to explain how to do it. He didnt explain well enough(at least to me) what I need to do to make a specific number of rows and have things and a specific part of the row. I'm not going to look at the answer you posted unless I really really have to. I would rather someone explain how to make a specifc number of columns and rows and place things in a specific spot. and yes that does make sense
    ps I dont cheat I'm trying to learn it and I didnt ask for the answer
    Last edited by caoinan; September 9th, 2007 at 12:22 AM.

  4. #4
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,270
    Distro
    Kubuntu

    Re: C prog. nested loops?

    Quote Originally Posted by caoinan View Post
    I am not cheating I didn't want the answer I wanted someone to explain how to do it. He didnt explain well enough(at least to me) what I need to do to make a specific number of rows and have things and a specific part of the row. I'm not going to look at the answer you posted unless I really really have to. I would rather someone explain how to make a specifc number of columns and rows and place things in a specific spot. and yes that does make since
    ps I dont cheat I'm trying to learn it and I didnt ask for the answer
    I edited my post with an explanation of the "algorithm". Please let me know if you still have any questions.

  5. #5
    Join Date
    Aug 2007
    Beans
    79

    Re: C prog. nested loops?

    thank you I after looking how you did it I realized I was trying to make it harder than what it was. could you explain how do i put things like letters from the alphabet in a specific spot in a column or row. I seem to be having trouble understanding nested loops in that way so without straight up giving me the answer how would I make a pyramid out of letters. I'm really not trying to just get the answer I just needed a more in depth explanation then the one given in the book which was only like 1 page

  6. #6
    Join Date
    Jun 2007
    Location
    Porirua, New Zealand
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: C prog. nested loops?

    Quote Originally Posted by caoinan View Post
    thank you I after looking how you did it I realized I was trying to make it harder than what it was. could you explain how do i put things like letters from the alphabet in a specific spot in a column or row. I seem to be having trouble understanding nested loops in that way so without straight up giving me the answer how would I make a pyramid out of letters. I'm really not trying to just get the answer I just needed a more in depth explanation then the one given in the book which was only like 1 page
    I haven't seen the book or the explanation it has, but I'll give it a go.
    For a pyramid of letters, the outer loop would control the number of lines. I'd probably tackle the details of the lines within the pyramid in two stages - (1) getting to the right place on the line, and (2) getting the data out there. Here's a hint for part 1: the computer able to distinguish between "nothing" and "whitespace" (the blank part before the data). Whitespace is commonly "filled" with a number of "spaces" that you can work out beforehand.
    Forum DOs and DON'Ts
    Never assume that information you find using a search engine is up-to-date.
    Please use CODE tags.
    A low-volume blog

  7. #7
    Join Date
    Aug 2007
    Beans
    79

    Re: C prog. nested loops?

    and could you explain this part a little more
    Code:
    for(row = 0; row < i+1; row++)
    I know how the loops work just an explanation of what makes it stop when it does i guess

  8. #8
    Join Date
    Jun 2007
    Location
    Porirua, New Zealand
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: C prog. nested loops?

    Quote Originally Posted by caoinan View Post
    and could you explain this part a little more
    Code:
    for(row = 0; row < i+1; row++)
    I know how the loops work just an explanation of what makes it stop when it does i guess
    Presumably the code is for the inner loop. This loop depends on whatever is in "i" to stop - it works out what "i+1" is each time through and compares "row" with it in order to decide when to stop.
    Forum DOs and DON'Ts
    Never assume that information you find using a search engine is up-to-date.
    Please use CODE tags.
    A low-volume blog

  9. #9
    Join Date
    Dec 2006
    Location
    Australia
    Beans
    1,097
    Distro
    Xubuntu 15.10 Wily Werewolf

    Re: C prog. nested loops?

    Code:
    for ( i = 0; i < 5; ++i )
    This loop controls the number of rows to be printed. So it prints from row 0 to row 4:

    row 0
    row 1
    row 2
    row 3
    row 4

    That's five rows, fulfilling the i=0 and i < 5 constraints.

    Code:
    for(row = 0; row < i+1; row++)
    This loop controls the number of $ printed for each row.

    For every row, it adds 1 to the row number, and prints that value of $s. So it the loop is in row 0, it wlll print 0 + 1 number of $s, which is just one $. If the loop is in row 1, it will print 1 + 1, which is $$. In row 3, it will print $$$, and so on.

    To summarise with a diagram:

    When i = 0, print 0 + 1 = $
    When i = 1, print 1 + 1 = $$
    When i = 2, print 2 + 1 = $$$
    When i = 3, print 3 + 1 = $$$$
    When i = 4, print 4 + 1 = $$$$$

  10. #10
    Join Date
    Aug 2007
    Beans
    79

    Re: C prog. nested loops?

    ok thank for explaining that part for me I finally get it now. if you could explain in pseudocode what to do for this question with a small explanation (use comments to make it easier for me)
    4. Have a program request a user to enter an uppercase letter. Use nested loops to produce a pattern like this:

    A
    ABA
    ABCBA
    ABCDCDA
    ABCDEDCBA

    The pattern should extend to the character entered. For example, the preceding pattern would result from an input of E...

    this is supposed to be a pyramid
    I'm really not trying to get the answer but I know thats what it looks like I'm just trying to understand how to use the nested for loops to produce the output I want
    Last edited by caoinan; September 9th, 2007 at 01:08 AM.

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •