Thread: help solving this triangle loop

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    90

    help solving this triangle loop

    Hi all, i was having trouble solving this triangle loop , the printed triangle should look like this:
    Code:
                1
              212
            32123
          4321234
        543212345
      65432123456
    7654321234567
    can anyone tell me the loops for it ?

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Sure:
    Code:
    int i, j;
    for (i = 1; i < 8; i++)
            for (j = i; j > 0; j++)
            for (j = 1; j < i; j++)
    Last edited by Kennedy; 05-06-2010 at 11:31 AM. Reason: Left off a couple.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    for the first loop u print \n rite ? and what do u print for the second and third loops ?

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Personally, I'd write this puppy to a string, then I would right justify the string at printf time.

    EDIT:

    SHOW SOME CODE. . .

    FOR CRYING OUT LOUD READ THE FORUM RULES!!!!

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    im not allowed to use include <string>

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by everyone0 View Post
    im not allowed to use include <string>
    Okay, thanks for that. What does this have to do with the price of tea in China?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by everyone0
    im not allowed to use include <string>
    <string> is not a standard C header, so that is not a concern. You do not need to #include <string.h> to implement Kennedy's suggestion.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    i dont understand what a string is , the instructor didn't teach us that yet

  9. #9
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by laserlight View Post
    <string> is not a standard C header, so that is not a concern. You do not need to #include <string.h> to implement Kennedy's suggestion.
    No you don't.

    #include <stdio.h>

    is the ONLY thing you'll need to do this task. That and a couple of strings.

    Strings are also known as arrays of char. If you don't know arrays yet, then it makes the project more interesting.

    Please show some code so we may help you. Also, it is a good idea to post the EXACT project verbiage so we know what you are supposed to be doing.

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    mm k

    here is the question:

    3-Write a C program that displays a triangle of numbers in the form:

    (the above form i already posted it)

    here is my work so far:

    Code:
    #include <stdio.h>
    #include <conio.h>
      main()
    {
       int i,j;
      for(i=1;i<=7;i++)
     {
    for(j=1;j<=i;j++)
        {
       printf("%d\t",j);
        }
       printf("\n");
     }
       getch();
    }
    this is printing something a bit close to what i want so im getting closer can anyone tell me what to do ?
    Last edited by everyone0; 05-06-2010 at 12:09 PM.

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Indentation!
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  12. #12
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Okay, so you have it printing the RIGHT side of the lines. . . and, what's the \t for?

    Start out by printing the LEFT side. Also, since you know how many positions this requires per line, and you know how many positions each data segment requires, you can put in a loop that prints spaces for the left hand side of the lines.

  13. #13
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    i dont know how do i print the left side , i know its supposed to be with using a space but i dont know how can u plz tell me ?

  14. #14
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Think about the problem. Each line is how long? For each line, how long is the data? How many spaces should this be? How do I figure out the number of "real" data per line?

    After you have these questions answered, ask yourself this: How would I put what I just computed on paper into code such that I do it in a loop?

  15. #15
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    can't u just tell me please ? , thats why im here to ask this question because my instructor is an ugly person who don't want to help anyone , not even with the simplest questions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive Triangle Function
    By w2look in forum C Programming
    Replies: 14
    Last Post: 11-13-2010, 02:31 PM
  2. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03:48 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  4. Just in case: "Odd" Triangle Challenge (for me)
    By BB18 in forum C Programming
    Replies: 3
    Last Post: 10-09-2004, 12:02 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM