Thread: New to C Programming, need help with a nesting loop

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    5

    Question New to C Programming, need help with a nesting loop

    Hey, all! I'm new to C programming and programming in general, and I am having trouble with a code that I can't get to work.

    They want me to produce the following pattern:

    F
    FE
    FED
    FEDC
    FEDCB
    FEDCBA

    This is my code so far:

    Code:
    #include <stdio.h>
    void pattern(void)
    {
        const int ROWS = 0;
        const int CHARS = 6;
        int row;
        char ch;
        
        for (row = 7; row > ROWS; row--)
        {
            for (ch = ('F' - row);  ch > ('F' - CHARS); ch--)
                printf("%c", ch);
            printf("\n");
        }
    }
    It is producing the following pattern:

    A
    BA
    CBA
    DCBA
    EDCBA

    No matter how much I've been tinkering around with it, I cannot get it to produce the desired result. Could I have some help? Thank you!

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    You should post all of the code. It looks like you're only missing a main() which calls pattern(), but this should be included for completeness.

    You're using a nested loop, which is the right approach, but the logic is strange and incorrect. It appears you tried solving this problem by typing code into your editor - instead, you should solve the problem by hand on paper, and then translate that logic into code.

    Here are some hints.

    The number of characters to print in the inner loop grows with each run of the outer loop. Perhaps the inner loop can be controlled with a variable that is modified with each iteration of the outer loop.

    If the inner loop starts counting at zero, then the count increases while the character to print decreases. Think subtraction.

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    haha you're reading C primer Plus? I just seen that the other day as I was going through it. Why don't you follow the example in the book?
    Find the pattern:

    it always prints out the 6th then works its way backward off the 6th.
    Code:
    first time, 
    print 6th
    start over
    print 6th,5th
    starts over,
    print 6th, 5th,4th,
    starts over
    repeats until end point,  is what? 6
    start count needs to always be set back to start point, then another count to and check for where you want to end again, then kick it out of inner loop, increment some number var start again. or increment then kick out ... either way you're going to need more var to keep track and maybe some if statements and take a break too while you're at it.

    that is what I'd try until I got it right.
    off the top of my head:
    Code:
    int a = 0,b = 1, c = 1, d = 6;
    for ( a = 0; a < 6; a++)
    { d = 6;
       for ( b = 1; b <= c; b++)
       {
           printf(" %d \n", --d);
        }
     c++;
    }
    outer loop controls the up to 6 times
    inner loop count check gets set to one more each pass
    d gets printed , but that part still does not look right.
    but it might just work with a little fiddling.
    Last edited by userxbw; 09-28-2017 at 01:38 PM.

  4. #4
    Registered User
    Join Date
    Sep 2017
    Posts
    5
    Quote Originally Posted by userxbw View Post
    haha you're reading C primer Plus? I just seen that the other day as I was going through it. Why don't you follow the example in the book?
    Find the pattern:

    it always prints out the 6th then works its way backward off the 6th.
    Code:
    first time, 
    print 6th
    start over
    print 6th,5th
    starts over,
    print 6th, 5th,4th,
    starts over
    repeats until end point,  is what? 6
    start count needs to always be set back to start point, then another count to and check for where you want to end again, then kick it out of inner loop, increment some number var start again. or increment then kick out ... either way you're going to need more var to keep track and maybe some if statements and take a break too while you're at it.

    that is what I'd try until I got it right.
    off the top of my head:
    Code:
    int a = 0,b = 1, c = 1, d = 6;
    for ( a = 0; a < 6; a++)
    { d = 6;
       for ( b = 1; b <= c; b++)
       {
           printf(" %d \n", --d);
        }
     c++;
    }
    outer loop controls the up to 6 times
    inner loop count check gets set to one more each pass
    d gets printed , but that part still does not look right.
    but it might just work with a little fiddling.
    Thanks, man! It unfortunately didn't work but I ended up getting it right with the following:

    Code:
    #include <stdio.h>
    void pattern(void)
    {
        const int ROWS = 6;
        const int CHARS = 6;
        int row;
        char ch;
        
        for (row = 0; row < ROWS; row++)
        {
            for (ch = 'F';  ch >= 'F' - row; ch--)
                printf("%c", ch);
            printf("\n");
        }
    }
    I changed the inner loop to have ch = 'F' and tweaked my constants which were a bit loopy.

    This is my first programming language, so I'll keep stuff like this in mind and put pencil to paper more often.

    Also, yes I am going through C Primer Plus for my C Language class. It's pretty helpful.

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Code:
    #include <stdio.h>
    
    int main()
    {
    
    int a = 0,b = 1, c = 1, d;
    
    for ( a = 0; a < 6; a++)
    { d = 71;
       for ( b = 1; b <= c; b++)
       {
           printf(" %c", --d);
          
        }
        printf("\n");
     c++;
    }
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Sep 2017
    Posts
    5
    I just fixed it. Here's the code I used:

    Code:
    #include <stdio.h>
    void pattern(void)
    {
        const int ROWS = 6;
        const int CHARS = 6;
        int row;
        char ch;
        
        for (row = 0; row < ROWS; row++)
        {
            for (ch = 'F';  ch >= 'F' - row; ch--)
                printf("%c", ch);
            printf("\n");
        }
    }
    Yes, I am using C Primer. It's a good book!

  7. #7
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Joey Secrist View Post
    I just fixed it. Here's the code I used:

    Code:
    #include <stdio.h>
    void pattern(void)
    {
        const int ROWS = 6;
        const int CHARS = 6;
        int row;
        char ch;
        
        for (row = 0; row < ROWS; row++)
        {
            for (ch = 'F';  ch >= 'F' - row; ch--)
                printf("%c", ch);
            printf("\n");
        }
    }
    Yes, I am using C Primer. It's a good book!
    and it works, keew, now run mine and see what that does for you. so you'll see another way to do it, then make int d in my loop work in your loops. if your curiosity gets the best of you.

    just swap out and change value; change
    Code:
    char ch = 'F';
    // to
    int ch = 70;
    replace the F with that number in your loop.
    Last edited by userxbw; 09-28-2017 at 03:04 PM.

  8. #8
    Registered User
    Join Date
    Sep 2017
    Posts
    5
    Quote Originally Posted by userxbw View Post
    Code:
    #include <stdio.h>
    
    int main()
    {
    
    int a = 0,b = 1, c = 1, d;
    
    for ( a = 0; a < 6; a++)
    { d = 71;
       for ( b = 1; b <= c; b++)
       {
           printf(" %c", --d);
          
        }
        printf("\n");
     c++;
    }
        return 0;
    }
    Thanks for the input. Your code came out as the following:

    F
    F E
    F E D
    F E D C
    F E D C B
    F E D C B A



    Very interesting. Mine didn't include spaces as yours did. Now I know a different way. Thank you very much!

  9. #9
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Joey Secrist View Post
    Thanks for the input. Your code came out as the following:

    F
    F E
    F E D
    F E D C
    F E D C B
    F E D C B A



    Very interesting. Mine didn't include spaces as yours did. Now I know a different way. Thank you very much!
    you're welcome, and
    look at printf
    Code:
     printf(" %c", --d);
    their is a space in there to give space .. hehe
    Last edited by userxbw; 09-28-2017 at 05:01 PM.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Quote Originally Posted by userxbw View Post
    just swap out and change value; change
    Code:
    char ch = 'F';
    // to
    int ch = 70;
    replace the F with that number in your loop.
    It is actually safer and better practice to use the character constant 'F'.

    If you work out the hints I gave in post #2, you'll find that you don't even need a variable to hold the characters to be printed.

  11. #11
    Banned
    Join Date
    Aug 2017
    Posts
    861
    printf("%d", sizeof('\n')); prints 4 because a character literal is an int , in C, and not a char .
    This is supposed to print the ASCII value of the character, as %d is the escape sequence for an integer.
    So the value given as argument of printf is taken as integer when printed. char ch = 'a'; printf("%d", ch);Jan 19, 2011



    Code:
    printf ("Characters: %c %c \n", 'a', 65);
    printf - C++ Reference

    that's all I'm saying.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    In C, character constants are of type int, as you've shown.
    Question 8.9

    However, the char data type is sufficient for handling basic characters:

    Quote Originally Posted by C11-draft
    6.2.5 Types

    ...

    3 An object declared as type char is large enough to store any member of the basic execution character set. If a member of the basic execution character set is stored in a char object, its value is guaranteed to be nonnegative. If any other character is stored in a char object, the resulting value is implementation-defined but shall be within the range of values that can be represented in that type.
    But more specifically, I was taking issue with your use of numeric values in lieu of character literals. Computers may use different character sets, so 65 may not always correspond to 'A'. It is good practice to strive for portability whenever possible.
    Last edited by Matticus; 09-28-2017 at 08:02 PM.

  13. #13
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Matticus View Post
    In C, character constants are of type int, as you've shown.
    Question 8.9

    However, the char data type is sufficient for handling basic characters:



    But more specifically, I was taking issue with your use of numeric values in lieu of character literals. Computers may use different character sets, so 65 may not always correspond to 'A'. It is good practice to strive for portability whenever possible.
    notice the specs
    Code:
    ASCII value
    so what more needs to be learned in order to utilize that method? why are you trying to put limits on what man as already establish?

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Quote Originally Posted by userxbw View Post
    so what more needs to be learned in order to utilize that method?
    As I've mentioned, different character sets exist, so (for example) the value of 'A' is not guaranteed to be 65 on a given architecture.

    Granted, ASCII-based character sets are by far the most common, but this is not universal. See for example EBCDIC.

    It is therefore more portable to use the character literal 'A', and let the particular machine use its underlying numeric value.

    Additionally, using character literals also makes the code easier to read and modify, which is also good practice.

  15. #15
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Quote Originally Posted by Joey Secrist View Post
    I just fixed it. Here's the code I used:

    Code:
    #include <stdio.h>
    void pattern(void)
    {
        const int ROWS = 6;
        const int CHARS = 6;
        int row;
        char ch;
        
        for (row = 0; row < ROWS; row++)
        {
            for (ch = 'F';  ch >= 'F' - row; ch--)
                printf("%c", ch);
            printf("\n");
        }
    }
    I like Joey's solution better than mine, because he didn't need a variable to count the columns. Here's my solution:
    Code:
    #include<stdio.h>
    int main(void) {
      for (int r = 0; r < 6; r++) {
        for (int c = 0; c <= r; c++) {
          printf("%c", 'F' - c);
        }
        printf("\n");
      }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures nesting
    By justine in forum C Programming
    Replies: 3
    Last Post: 11-26-2012, 06:25 AM
  2. Nesting classes
    By DarkAlex in forum C++ Programming
    Replies: 4
    Last Post: 03-29-2009, 08:58 PM
  3. Nesting switchstatments
    By Da-Nuka in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2005, 03:05 PM
  4. Nesting
    By coo_pal in forum C Programming
    Replies: 1
    Last Post: 01-27-2003, 11:16 PM
  5. Nesting Parentheses
    By XZSNPP in forum C++ Programming
    Replies: 6
    Last Post: 01-18-2003, 10:01 PM

Tags for this Thread