Trying to make a triangle

This is a discussion on Trying to make a triangle within the C Programming forums, part of the General Programming Boards category; the output should be: Code: Size: 4 Seed: 1 1 2 4 7 3 5 8 6 9 0 Updated ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    9

    Trying to make a triangle

    the output should be:
    Code:
    Size: 4  Seed: 1
    1  2  4  7
       3  5  8
          6  9
             0
    Updated code:
    Code:
    main()
    {
            size = 6;
            seed = 3;
    
            for(row = 0; row < size; row++)
            {
            cnum = seed;
    
                    for(col = 0; col < size; col++)
                    {
                            if(col == row)
                            {
                                    printf("%i ", cnum + row);
                            }
                            else if(col > row)
                            {
                                    if((cnum = cnum + col) <= 9)
                                    {
                                            printf("%i ", cnum);
                                    }
                                    else
                                    {
                                            cnum = cnum - 10;
                                            printf("%i ", cnum);
                                    }
                            }
                            else
                            {
                                    printf("  ");
                            }
                    }
            printf("\n");
            }
    }
    Last edited by frterwil; 03-01-2009 at 05:43 PM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    I am begginer also but I see that you put
    Code:
    for(col = 1; col < size; col++)
    isn't it must be
    Code:
    for(col=1; col<=size;++col)

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    Not sure but maybe it will help. In you if statement you have cnum assigned to crow, but you are not using it anywhere.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    9
    I know, I was trying something that didn't work. I must have missed that part, but it doesn't make any difference in the program.

  5. #5
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    What do you mean by "Trying to make a triangle"?

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    9
    i actually just found out the it is called a Parkside Triangle. That is what I need to make. Instead of me failing to properly describe it here:
    http://www.wisegeek.com/what-is-parksides-triangle.htm

    I can get the first row, but after that it all goes to hell.

  7. #7
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Code:
    1  2  4  7
       3  5  8
          6  9
             0
    I think with your settings (seed=1, size=4), the output should end in a line that contains a 1 instead of a 0...

    Hint 0: no Parkside Triangle can contain zeros.

    I can get the first row, but after that it all goes to hell.
    Well, can you spot any obvious difference between the first row and all the other rows by looking at the output of your program?

    Hint 1:
    Try to get the correct numbers first, then use indenting to make it look as specified (I didn't test it, but this should solve your problem)

    Hint 2:
    In every row, the first number only depends on the seed, not on the size


    Apart from that, it is desirable to explicitly state that seed, size, row, col, cnum and main() are of type int (or function returning int respectively).

    Greets,
    Philip

    EDIT: you can compute the value of any of these numbers in constant time by using only seed, row and col. It's probably not the goal of your exercise to find this expression, but once you have it, the rest is easy. If you can't find it immediately, stick to your current approach.
    Last edited by Snafuist; 03-01-2009 at 07:29 PM.
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    9
    Okay, I've got the 1st numbers in each row correct, but I can't figure out why the other numbers are off. I'm on about hour 7 on something that shouldn't be that hard so I'll a little frustrated. Any help is appreciated, thanks.

    Code:
    Desired Output:
    1 2 4 7
      3 5 8
        6 9
          0
    
    Actual:
    1 3 4 5
      3 6 7
        6 0
          0
    Here is my code:
    Code:
    main()
    {
            printf("Enter size and seed:  ");
            scanf("\n%i %i", &size, &seed);
            rnum = seed;
    
            for(row = 1; row <= size; row++)
            {
                            for(col = 1; col <= size; col++)
                            {
                                    if(col == row)
                                    {
                                            if(row == 1)
                                            {
                                                    printf("%i ", seed);
                                            }
                                            else
                                            {
                                                    rnum = y + row;
                                                    if(rnum > 9)
                                                    {
                                                            printf("%i ", rnum%10);
                                                    }
                                                    else
                                                    {
                                                            printf("%i ", rnum);
                                                    }
                                            }
                                    y = rnum;
                                    }
                                    else if(col > row)
                                    {
                                            cnum = rnum + col;
                                            printf("%i ", cnum%10);
                                    }
                                    else
                                    {
                                            printf("  ");
                                    }
                            }
            printf("\n");
            }
    }
    Last edited by frterwil; 03-01-2009 at 11:03 PM.

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    when seed == 1, row ==1 and col == 2 you have rnum == seed == 1
    Code:
    cnum = rnum + col;
    printf("%i ", cnum%10);
    Now tell me why do you think that 1+2 should be 2 and not 3?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 05:19 AM
  2. make all rule
    By duffy in forum C Programming
    Replies: 9
    Last Post: 09-11-2003, 02:05 PM
  3. Question about atheists
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 08-11-2003, 12:50 PM
  4. 'functions' in make?
    By mart_man00 in forum C Programming
    Replies: 1
    Last Post: 06-21-2003, 03:16 PM
  5. Replies: 6
    Last Post: 04-20-2002, 07:35 PM

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