for loop question

This is a discussion on for loop question within the C Programming forums, part of the General Programming Boards category; Hi everybody, I stumbled upon this site while looking for some help to a problem I have and this seems ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    2

    Question for loop question

    Hi everybody,

    I stumbled upon this site while looking for some help to a problem I have and this seems to be a great community. My question has to do with a for loop and the printing of a triangle. What I need to do is print a triangle like this:

    Triangle
    Code:
              *
            ***
          *****
        *******
      *********
    ***********
    However, I am getting something like this:

    Triangle
    Code:
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********
    ***********
    My code so far is:
    Code:
    #include <stdio.h>
    
    main()
    {
      int width=0, row=0, column=0;
    
      scanf("%d", &width);
    
      for(row=1; row<=width; row++)
        {
          for(column=1; column<=row; column++)
            printf("*");
          printf("\n");
        }
    
      return 0;
    }

    So my question is how do I get the first asterisk to be in the middle of the triangle like this first example I gave. I would appreciate any help you can give me since I am very new to computer programming. Thanks!


    Code tags added by Hammer

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Lesson number one, read this . I've added them to your post this time round...

    Now to your question:
    >>how do I get the first asterisk to be in the middle
    Either put the spaces out yourself, or use the %10s type method.

    This has been done many times before, I'm sure a board search will find you an example.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    Re: for loop question

    Originally posted by neo505
    Hi everybody,

    I stumbled upon this site while looking for some help to a problem I have and this seems to be a great community. My question has to do with a for loop and the printing of a triangle. What I need to do is print a triangle like this:

    Triangle
    Code:
              *
            ***
          *****
        *******
      *********
    ***********
    However, I am getting something like this:

    Triangle
    Code:
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********
    ***********
    My code so far is:
    Code:
    #include <stdio.h>
    
    main()
    {
      int width=0, row=0, column=0;
    
      scanf("%d", &width);
    
      for(row=1; row<=width; row++)
        {
          for(column=1; column<=row; column++)
            printf("*");
          printf("\n");
        }
    
      return 0;
    }

    So my question is how do I get the first asterisk to be in the middle of the triangle like this first example I gave. I would appreciate any help you can give me since I am very new to computer programming. Thanks!


    Code tags added by Hammer

    how about tryin to use gotoxy() functions.....
    that would help....
    TEACH ME....
    :-);-)

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Actually, I think the output is supposed to look like this:
    Code:
         *
        ***
       *****
      *******
     *********
    ***********
    Oh. It looks like its centred in the reply box. Stupid non-fixed width fonts.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    If you take a close look at your figures, then you'll see that the difference is that in the first picture the spaces are after the last '*' and in the second picture, the spaces are before the first '*'. Since you know the length of the longest line of '*', you can calculate how many spaces should be in front of the first '*'.

    The length of the longest line of '*' is stored in your width variable.

    >how about tryin to use gotoxy() functions.....

    The gotoxy() function is not a standard C-function so it will not be supported by all compilers.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    2
    I was really trying to get a triangle output like the one Xsquared did. I am trying to get the first asterisk in the middle of the rest of the triangle. Can anybody help?

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by neo505
    I was really trying to get a triangle output like the one Xsquared did. I am trying to get the first asterisk in the middle of the rest of the triangle. Can anybody help?
    This has been done many times before, I'm sure a board search will find you an example.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21