Thread: Need help on Star Pattern using Loops

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

    Need help on Star Pattern using Loops

    I am stuck at this pattern. Please help.
    Attached Images Attached Images Need help on Star Pattern using Loops-pattern-jpg 

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    You need to show us your attempt to solve the problem, by posting the code in code blocks, then we can help you. We cannot do the work for you. You can only learn to program in C by actually writing code yourself.

    The same is true with anything you want to learn.

  3. #3
    Registered User
    Join Date
    Sep 2022
    Posts
    5
    I have written this code for 1 no. pyramid which is working fine.. I just can't seem to figure out how to repeat it horizontally.
    Code:
    #include<stdio.h>
    int main()
    {
      int i, j, n;
      printf("Enter No. of Rows : ");
      scanf("%d", &n);
    
      for (i = 1; i <= n; i++) {
        for (j = 1; j <= n; j++) {
          if (i + j <= n) {
            printf(" ");
          } else {
            printf("* ");
          }
        }
        printf("\n");
      }
      return 0;
    }
    Last edited by Salem; 09-08-2022 at 11:40 AM. Reason: removed crayola

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The first step is to make it output the same number of trailing spaces as leading spaces.

    So that the pattern is essentially rectangular in shape.
    ....*....
    ...* *...
    ..* * *..
    .* * * *.
    * * * * *


    Once you can do that, all you need to do is repeat the printing of each row.
    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.

  5. #5
    Registered User
    Join Date
    Sep 2022
    Posts
    5
    Ok. I modified the code to print in rectangular shape. But i don't know how to repeat it row wise. Please help on that. Here is the modified code:-
    Code:
    #include<stdio.h>
    int main()
    {
      int n;
      printf("Enter No. of Rows : ");
      scanf("%d", &n);
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
          (i + j <= n) ? printf(" ") : printf(" *");
        }
        printf("\n");
      }
      return 0;
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    No it doesn't.
    Code:
    #include<stdio.h>
    int main()
    {
      int n;
      printf("Enter No. of Rows : ");
      scanf("%d", &n);
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
          (i + j <= n) ? printf(".") : printf(" *");
        }
        printf("\n");
      }
      return 0;
    }
    
    $ gcc bar.c
    $ ./a.out 
    Enter No. of Rows : 5
    .... *
    ... * *
    .. * * *
    . * * * *
     * * * * *
    All you did was push everything along one character.

    You need to print the same number of trailing spaces as leading spaces on each row, so that the total number of characters on each row is the same.

    Then you can repeat printing each row to end up with the same shape printed several times.

    FWIW, your super compressed logic isn't doing you any favours.
    Code:
      for (int i = 1; i <= n; i++) {
        int nSpaces = n - i;
        int nStars = i;
    With that, you can write three simple loops to output
    - the leading spaces
    - the stars
    - the trailing spaces.


    One more thing, when you paste code, use "paste as text" and/or "copy as text".
    Your IDE brings all the html markup with it and makes a mess when pasted here.
    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.

  7. #7
    Banned
    Join Date
    Jul 2022
    Posts
    112
    Your lucky day.

    Code:
    /* main.cpp,  Draw Pyramid,  Created by Great on 9/10/22. */
    #include <stdio.h>
    
    
    #pragma mark - Interface
    int add_array(char *, int, int, char);
    int draw_pyramid(char *, int, char);
    
    
    #pragma mark - Implementation
    int add_array(char *buffer, int index, int number, char character) {
        
        int n = number;
        int i = index;
        
        while (0 != n) {
            
            n = n - 1;
            buffer[i] = character;
            i = 1 + i;
        }
        
        return i;
    }
    int draw_pyramid(char *buffer, int height, char character) {
        
        int i = 0;
        int star = 0;
        int space = height;
        
        while (1 != space) {
            
            star = 1 + star;
            space = space - 1;
            
            i = add_array(buffer, i, space, ' ');
            i = add_array(buffer, i, star, character);
            i = add_array(buffer, i, star - 1, character);
            
            i = add_array(buffer, i, space, ' ');
            i = add_array(buffer, i, space, ' ');
            i = add_array(buffer, i, star, character);
            i = add_array(buffer, i, star - 1, character);
            
            i = add_array(buffer, i, space, ' ');
            i = add_array(buffer, i, space, ' ');
            i = add_array(buffer, i, star, character);
            i = add_array(buffer, i, star - 1, character);
            
            i = add_array(buffer, i, space, ' ');
            i = add_array(buffer, i, space, ' ');
            i = add_array(buffer, i, star, character);
            i = add_array(buffer, i, star - 1, character);
            
            buffer[i] = '\r';
            i = 1 + i;        /* 1 character for newline */
        }
        
        buffer[i - 1] = '\0'; /* Remove extra '\r' character */
        return i;
    }
    int main(int argc, const char *argv[]) {
        
        char buffer[1024];
        int characters = draw_pyramid(buffer, 10, '*');
        
        printf("%d characters \n", characters);
        printf("%s\n", buffer);
        return 0;
    }
    Last edited by kodax; 09-10-2022 at 12:45 AM.

  8. #8
    Registered User
    Join Date
    Sep 2022
    Posts
    5
    Ty salem Got it now..

    Code:
    #include<stdio.h>
    int main()
    {
        int n,r;
        printf("Enter No. of Rows : ");
        scanf("%d",&n);
        printf("Enter No. of Reptitions : ");
        scanf("%d",&r);
        for(int i=1;i<=n;i++)
        {
            for(int repeat=1;repeat<=r;repeat++)
            {
                for(int space=1;space<=n-i;space++)
                {
                printf(" ");
                }
                for(int star=1;star<=i;star++)
                {
                    printf("* ");
                }
                for(int space=1;space<=n-i;space++)
                {
                    printf(" ");
                }
            }
        printf("\n");
        }
    return 0;
    }

  9. #9
    Registered User
    Join Date
    Sep 2022
    Posts
    5
    Ty to u too kodax.. but i was trying to get it through loops only.. I'm just a newbie and haven't covered arrays yet..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with star pattern
    By BuckyBarnes in forum C Programming
    Replies: 6
    Last Post: 10-17-2012, 05:54 PM
  2. Square star pattern
    By Veanne Erida in forum C Programming
    Replies: 4
    Last Post: 09-18-2011, 12:50 AM
  3. Replies: 3
    Last Post: 12-15-2010, 01:35 AM
  4. Wish to create a pattern of numbers using loops
    By bharat_kaushik in forum C Programming
    Replies: 12
    Last Post: 09-04-2009, 11:08 AM
  5. Star Wars or Star Trek
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 09-28-2001, 08:33 AM

Tags for this Thread