Thread: Printing a pattern to the screen

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

    Printing a pattern to the screen

    Hi everybody. I'm in my first programming course ever and have a few questions about an assignment that we've been given.

    I'm trying to print the following pattern to the screen:
    Printing a pattern to the screen-pattern-png

    The pattern is supposed to contain 5 rows and each subsequent row has 2 additional asterisks from the row above making roughly a pyramid shape.

    I've been working on creating code to do this using For loops (this was part of the instructions) and here's what I have so far:

    Code:
    #include <stdio.h>
    
    int main ()
    
    
    {
        int row;
        int col;
    
    
        for (row = 1; row <= 5; row++) //rows
        {
            for (col = 1; col <= row; col++) //columns
            {
                printf_s("*");
            }
    
                printf_s("\n");
        }
        return 0;
    } 
    

    The problem with my code is that I am not accounting for the required empty spaces to get the alignment correct. With the above current code here's what the output looks like:
    Printing a pattern to the screen-loop-png

    I'm hoping someone can point me in the right direction as to how to re-write my code to get the correct alignment.

    Thank you in advance.

  2. #2
    Registered User
    Join Date
    May 2015
    Posts
    90
    I guess you can always say that the point of the pyramid is always in the middle, and the middle is the base / 2, right?
    I guess that gives you a starting ammount of spaces, then it decreases linearly no?

    Maybe try to get the right ammount of asterisks per row first (right now you have only half), and then we'll see about the spaces.

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    23
    Quote Originally Posted by Fiskker View Post
    I guess you can always say that the point of the pyramid is always in the middle, and the middle is the base / 2, right?
    I guess that gives you a starting ammount of spaces, then it decreases linearly no?

    Maybe try to get the right ammount of asterisks per row first (right now you have only half), and then we'll see about the spaces.
    I think I also neglected to mention that I am also struggling to find the right code that will also give me the correct number of asterisks per row.

  4. #4
    Registered User
    Join Date
    May 2015
    Posts
    90
    Quote Originally Posted by PhantomJoe View Post
    I think I also neglected to mention that I am also struggling to find the right code that will also give me the correct number of asterisks per row.
    Oh, okay.

    Well, you were fine by looping each row, and then every column, and printing an asterisk until the row was matched, that is:

    1 asterisk in first row.
    2 asterisks in second row.
    .
    n asterisks in n rows.


    Now you need:
    1 asterisk in first row
    3 asterisks in second row.
    .
    (2n-1) asterisks in n row.


    So your condition is now not the ammount of rows, but the double minus one, do you see this? If not I can try and explain it a bit more.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Like so
    Code:
    for (row = 1; row <= 5; row++) //rows
      int numspaces = ?
      for ( s = 0 ; s < numspaces ; s++ ) printf(" ");
    Look at your diagram, and figure out a numeric expression for numspaces as a function of the row number.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2017
    Posts
    23
    Quote Originally Posted by Fiskker View Post
    Oh, okay.

    Well, you were fine by looping each row, and then every column, and printing an asterisk until the row was matched, that is:

    1 asterisk in first row.
    2 asterisks in second row.
    .
    n asterisks in n rows.


    Now you need:
    1 asterisk in first row
    3 asterisks in second row.
    .
    (2n-1) asterisks in n row.


    So your condition is now not the ammount of rows, but the double minus one, do you see this? If not I can try and explain it a bit more.
    Mathematically I understand your logic. 2*rows -1 will give me the correct number of asterisks per row (2*1-1=1 and 2*2-1=3 and so on) but how do I account for the leading blank spaces?

  7. #7
    Registered User
    Join Date
    Sep 2017
    Posts
    23
    Quote Originally Posted by Salem View Post
    Like so
    Code:
    for (row = 1; row <= 5; row++) //rows
      int numspaces = ?
      for ( s = 0 ; s < numspaces ; s++ ) printf(" ");
    Look at your diagram, and figure out a numeric expression for numspaces as a function of the row number.
    In the first row I need 3 leading spaces, in the second row I need 2 leading spaces, and in the third row I need 1 leading space (and no leading spaces for the 4th row). I'll be honest, I'm really struggling to find an expression for numspaces as a function of the row number. Am I overlooking something obvious?

  8. #8
    Registered User
    Join Date
    May 2015
    Posts
    90
    Quote Originally Posted by PhantomJoe View Post
    Mathematically I understand your logic. 2*rows -1 will give me the correct number of asterisks per row (2*1-1=1 and 2*2-1=3 and so on) but how do I account for the leading blank spaces?
    But I want to see how you code it, what would you do?
    What needs to be changed in the for loop that loops the columns, what is the new condition?

    Quote Originally Posted by PhantomJoe View Post
    In the first row I need 3 leading spaces, in the second row I need 2 leading spaces, and in the third row I need 1 leading space (and no leading spaces for the 4th row). I'll be honest, I'm really struggling to find an expression for numspaces as a function of the row number. Am I overlooking something obvious?
    Okay so you are describing it, how do we code something that prints decreasingly?
    We call the print function less and less overtime until a limit is reached, you could use a loop to do this, similar to what you've done so far.

    Mathematically:
    F(rows) = spaces
    F(1) = 3
    F(2) = 2
    F(3) = 1
    F(4) = 0

    F(x) = ?

  9. #9
    Registered User
    Join Date
    Sep 2017
    Posts
    23
    Quote Originally Posted by Fiskker View Post
    But I want to see how you code it, what would you do?
    What needs to be changed in the for loop that loops the columns, what is the new condition?



    Okay so you are describing it, how do we code something that prints decreasingly?
    We call the print function less and less overtime until a limit is reached, you could use a loop to do this, similar to what you've done so far.

    Mathematically:
    F(rows) = spaces
    F(1) = 3
    F(2) = 2
    F(3) = 1
    F(4) = 0

    F(x) = ?
    Lol, so I've tried all kinds of changes to the for loop for columns but I either wind up printing a single asterisk or I create an infinite loop with infinity asterisks.

  10. #10
    Registered User
    Join Date
    Oct 2017
    Posts
    36
    Hello PhantomJoe, I passive some sense of anxiety and frustration, because the solution has not been made clear to you. I have being faced with similar problems. Calm down and take a break from the challenge engage in something that relaxes you and later on come back to the task and review all the suggestions provided by forum members. I bet you will surmount the challenge before you. Take is slow with your approach and have some patience with your self.

  11. #11
    Registered User
    Join Date
    May 2015
    Posts
    90
    Quote Originally Posted by PhantomJoe View Post
    Lol, so I've tried all kinds of changes to the for loop for columns but I either wind up printing a single asterisk or I create an infinite loop with infinity asterisks.
    I understand, but if you don't provide information about what you are doing and what the problem is, meaning what exactly is happening with your current code, then it becomes hard to see what the mistake is and how to correct it.

  12. #12
    Registered User
    Join Date
    Sep 2017
    Posts
    23
    Quote Originally Posted by Fiskker View Post
    I understand, but if you don't provide information about what you are doing and what the problem is, meaning what exactly is happening with your current code, then it becomes hard to see what the mistake is and how to correct it.
    I know I need to incorporate the logic you laid out to get the correct number of asterisks (row*2-1) but because I'm unsure of how to implement that I've just been trying to insert it in different places in the inner loop.

    In the below example I've used it in the inner loop in the test condition portion and that produces an infinite loop.

    Code:
    int main()
    {
        int row;
        int col;
        
        for (row = 1; row <= 4; row++) //rows
        {
            for (col = 1; col = row*2-1; col++) //columns
            {
                printf_s("*");
            }
    
    
            printf_s("\n");
        }
    
    
        return 0;
    }
    When I use it in the initiate location of the inner loop I get just a single asterisk (code below):
    Code:
    int main()
    {
        int row;
        int col;
        
        for (row = 1; row <= 4; row++) //rows
        {
            for (col = row*2-1; col <= row; col++) //columns
            {
                printf_s("*");
            }
    
    
            printf_s("\n");
        }
    
    
        return 0;
    }

    I also want to say thank you for your help and I'm trying to not get frustrated but as this is my first ever exposure to programming I sometimes feel like I'm in over my head.
    Last edited by PhantomJoe; 10-21-2017 at 02:24 PM.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You're still trying to solve too much of the problem at once.
    Code:
    for (row = 1; row <= 5; row++) {
      int numspaces = ?
      printf("Numspaces=%d\n",numspaces);
    }
    Just figure out how to make this print
    Numspaces=4
    Numspaces=3
    Numspaces=2
    Numspaces=1
    Numspaces=0

    As soon as you do, the rest falls into place.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    Sep 2017
    Posts
    23
    Quote Originally Posted by Salem View Post
    You're still trying to solve too much of the problem at once.
    Code:
    for (row = 1; row <= 5; row++) {
      int numspaces = ?
      printf("Numspaces=%d\n",numspaces);
    }
    Just figure out how to make this print
    Numspaces=4
    Numspaces=3
    Numspaces=2
    Numspaces=1
    Numspaces=0

    As soon as you do, the rest falls into place.
    Hi Salem. So I think I figured out the right code to get your above output:
    Code:
    int main()
    {
        int row;
        int col;
        int numspaces;
        
        for (row = 4; row >= 0; row--) 
        {        
                printf_s("Numspaces=%d\n", numspaces = row);
        }
            printf_s("\n");
        
    
    
        return 0;
    }
    And it prints the following:
    Printing a pattern to the screen-numspaces-png

    I'm sorry to be so dense but I'm not sure how to connect this to the assignment?

  15. #15
    Banned
    Join Date
    Aug 2017
    Posts
    861
    I hope this helps more than hinders,
    Code:
    #include <stdio.h>
    
    int main ()
    
    
    {
        int row;
        int col;
        int space = 16;
    
    
        for (row = 1; row <= 5; row++) //rows
        {
            for (col = 1; col <= row ; col++) //columns
            {
                    //printf("*");
                    //printf("%d",space);
                    printf("%*c",space,'*');
            }
    space--;
                printf("\n");
        }
        return 0;
    }
    but it does demonstrate how to get the star moved over before printing, it is not an complete answer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pattern Printing
    By sunny` in forum C Programming
    Replies: 2
    Last Post: 03-18-2012, 09:11 PM
  2. Why is this not printing to screen?
    By lilbo4231 in forum C Programming
    Replies: 2
    Last Post: 03-07-2011, 03:10 PM
  3. need help printing this pattern
    By Blimpy325 in forum C Programming
    Replies: 4
    Last Post: 03-04-2006, 06:40 AM
  4. Printing a pattern correctly???
    By Basia in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 04:34 PM
  5. Printing a Pattern of Stars
    By ProgrammingDlux in forum C++ Programming
    Replies: 13
    Last Post: 03-01-2002, 08:38 AM

Tags for this Thread