Thread: for loop

  1. #1
    Registered User
    Join Date
    Dec 2022
    Posts
    3

    Post for loop

    hello guys,
    I'm trying to do a shape of star.
    I have to do print line 1-4 again in lines 6-9.
    Total 9 lines.

    // line 6 same like line 4
    // line 7 same like line 3
    // line 8 same like line 2
    // line 9 same like line 1


    Can anyone tell me how to do it without back every line again.

    Here's the link to my code :
    GDB online Debugger | Code, Compile, Run, Debug online C, C++

    Thanks a lot

    Code:
    #include <stdio.h>
    
    int
    main ()
    {
      int s, i, j;
    
      // for star
      printf ("Enter size (10-20)\n");
      scanf (" %d", &s);
      for (i = 1; i < 2; i++)
        {
          for (j = 1; j <= 2 * s - 1; j++)
    	{
    	  if (j == s)
    	    printf ("o");
    	  else
    	    printf ("-");
    
    	}
          printf ("\n");
        }
      for (i = 2; i < 3; i++)
        {
          for (j = 1; j <= 2 * s - 1; j++)
    	{
    	  if (j == s + 1 || j == s - 1)
    	    printf ("o");
    	  else
    	    printf ("-");
    	}
          printf ("\n");
        }
      for (i = 3; i < 4; i++)
        {
          for (j = 1; j <= 2 * s - 1; j++)
    	{
    	  printf ("o");
    	}
          printf ("\n");
        }
    
      for (i = 4; i < 5; i++)
        {
          for (j = 1; j <= 2 * s - 1; j++)
    	{
    	  if (j == 2 * s - 2 || j == 2)
    	    printf ("o");
    	  else
    	    printf ("-");
    	}
          printf ("\n");
        }
      for (i = 5; i < 6; i++)
        {
          for (j = 1; j <= 2 * s - 1; j++)
    	{
    	  if (j == 2 * s - 3 || j == 3)
    	    printf ("o");
    	  else
    	    printf ("-");
    	}
          printf ("\n");
        }
      // line 6 same like line 4
      // line 7 same like line 3
      // line 8 same like line 2
      // line 9 same like line 1
    }
    Attached Files Attached Files
    Last edited by Salem; 12-15-2022 at 02:09 PM. Reason: Inlined the code

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Better to post source code in Code Blocks on this site.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I'm trying to do a shape of star.
    Start by drawing out several examples on graph paper.

    The point of the exercise is to find the patterns between the size of the star
    Code:
      printf ("Enter size (10-20)\n");
      scanf (" %d", &s);
    and what the final shape is.

    Say you wanted to draw a triangle of height 6 (h=6)
    Code:
    .....*
    ....***
    ...*****
    ..*******
    .*********
    ***********
    So there are a couple of things to notice
    - the number of leading spaces counts down one for each successive row.
    - the number of stars increases by two for each successive row.

    Code:
    for ( row = 0 ; row < h ; row++ ) {
      leading_spaces = h - row - 1;
      num_stars = row * 2 + 1;
    }
    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.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    What is "a shape of star"?
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Dec 2022
    Posts
    3
    Quote Originally Posted by john.c View Post
    What is "a shape of star"?
    I was not clear soorry.
    It is not a shape of star.
    I just want to do 9 lines.
    The first 5 lines is like I did and the other four lines has to be like I mentioned.

  6. #6
    Registered User
    Join Date
    Dec 2022
    Posts
    3

    my shape

    This shape I mean.
    Attached Images Attached Images for loop-photo-jpg 

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    OK, how does that get bigger or smaller, depending on the value of s you type in?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-22-2016, 09:08 AM
  2. Replies: 4
    Last Post: 11-05-2015, 03:29 PM
  3. Replies: 1
    Last Post: 03-28-2015, 08:59 PM
  4. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  5. Replies: 23
    Last Post: 04-05-2011, 03:40 PM

Tags for this Thread