Thread: Floyd triangle - hint plz

  1. #16
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by whiteflags View Post
    In effect, c = 0, m++; is c = m++; the assignment to zero may as well have never happened.
    That's not correct.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Hodor
    I had no itention of writing that at all
    Then what is your intention? Except for the case where m == -1, which will not happen here unless there is arithmetic overflow, this:
    Code:
    c = !++m;
    is equivalent to:
    Code:
    c = 0;
    m++;
    EDIT:
    Oh wait, I may have gotten the precedence wrong for the comma operator myself. Oh well. I still don't see the point of using it here.
    Last edited by laserlight; 11-19-2013 at 09:58 PM.
    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

  3. #18
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    ',' is evaluated after '='.
    i dont believe in competition in da field of cboard posts, u see, i believe in a collection of posts each 2 be admired for der own personal statement. when in doubt, ask Willy!

  4. #19
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by laserlight View Post
    No, you don't understand the comma operator.
    I don't want to argue, but I respectfully state with 100% confidence that it is you and whiteflags not understanding the comma operator. The result of the left hand side of expression is discarded, not what the expression does.

    I've seen the comma operator used in this way in many projects and I am honestly baffled what's so difficult about it.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Hodor
    I don't want to argue, but I respectfully state with 100% confidence that it is you and whiteflags not understanding the comma operator. The result of the left hand side of expression is discarded, not what the expression does.
    I agree: I remembered the precedence table wrongly. (Yes, I checked C99 to be sure prior to editing my previous post.)

    On the other hand, I would argue that the comma operator is unnecessary conciseness here. Just separate it to two statements.
    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

  6. #21
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by laserlight View Post
    I agree: I remembered the precedence table wrongly. (Yes, I checked C99 to be sure prior to editing my previous post.)

    On the other hand, I would argue that the comma operator is unnecessary conciseness here. Just separate it to two statements.
    I have an aversion to using the curly braces. But seriously, if I'd known it was confusing I wouldn't have used it but as I said I come across it commonly so didn't even think about it, it just came out.

  7. #22
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Quote Originally Posted by whiteflags View Post
    Well since we are all producing solutions now:

    Code:
    >>> def floyd(rowcount = 5):
    ...     return [ list(range(i*(i-1)//2+1, i*(i+1)//2+1)) for i in range(1, rowcount+1) ]
    ...
    >>> floyd()
    [[1], [2, 3], [4, 5, 6], [7, 8, 9, 10], [11, 12, 13, 14, 15]]
    >>>
    That seems to be the hard part.
    Two points:

    1. +1 for using haskell (although this is a C board... so wtf??)
    2. Your solution relies on knowing the end row length, which largely simplifies the solution (Beyond the fact that it's also written in Haskell)

    My solution could also have been much simpler with that restriction. Conclusion: Haskell is still useless!

  8. #23
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The comma operator is almost always used to obfuscate. I'm not just being salty: Commas are used to separate the arguments in function invocation, but that is not the comma operator being used, because the Standard stipulates that the order in which arguments are evaluated is unspecified. When you can't even be sure at a glance alone that an operator is being used, it's bad in that way, isn't it? Using it effectively would depend entirely of your memory on the rules.

  9. #24
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by whiteflags View Post
    The comma operator is almost always used to obfuscate. I'm not just being salty: Commas are used to separate the arguments in function invocation, but that is not the comma operator being used, because the Standard stipulates that the order in which arguments are evaluated is unspecified. When you can't even be sure at a glance alone that an operator is being used, it's bad in that way, isn't it? Using it effectively would depend entirely of your memory on the rules.
    I'll adjust the habit.

  10. #25
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by nonpuz View Post
    Two points:

    1. +1 for using haskell (although this is a C board... so wtf??)
    2. Your solution relies on knowing the end row length, which largely simplifies the solution (Beyond the fact that it's also written in Haskell)

    My solution could also have been much simpler with that restriction. Conclusion: Haskell is still useless!
    Your point is entirely invalidated, because I used python.

  11. #26
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Quote Originally Posted by whiteflags View Post
    Well since we are all producing solutions now:
    Always a first time for this, so I present this little baby.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int i,j = 1;
        int line_limit = 1,limit = 91;
    
        while(j <= limit)
        {
            for(i = 0; i < line_limit;i++)
            {
                printf("%d ",j++);
            }
    
            printf("\n");
            line_limit++;
        }
    
        return 0;
    }

  12. #27
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Ok, I've made a much clearer and more efficient implementation

    Code:
    #include <stdio.h>
    #include <math.h>
    
    /* meh */
    int issquare(unsigned n)
    {
        int t = sqrt(n + 0.5);
        return t*t == n;
    }
    
    
    int istrinum(unsigned n)
    {
        return issquare(8 * n + 1);
    }
    
    /* could probably be improved */
    unsigned prevtrinum(unsigned n)
    {
        int i, largest = 0;
        for (i = 0; i < n; i++)
            if (istrinum(i))
                largest = i;
        return largest;
    }
    
    /* could probably be improved */
    unsigned linelen(unsigned n, unsigned cnt)
    {
        unsigned prev;
    
        prev = prevtrinum(n);
        if (prev > 0)
            cnt = linelen(prev, cnt + 1);
        else
            cnt++;
        return cnt;
    }
    
    /* could probably be improved */
    void floyd_r(unsigned n)
    {
        unsigned len, prev, i;
        static const char delim[] = {'\n', ' '};
    
        if (n == 0)
            return;
    
        prev = prevtrinum(n);
        if (prev > 0)
            floyd_r(prev);
        len = linelen(n, 0);
        for (i = len; i > 0; i--)
            printf("%2d%c", n - i + 1, delim[i > 1]);
    }
    
    int main(void)
    {
        floyd_r(91);
    
        return 0;
    }
    Edit: This is what happens when I'm not allowed to use the comma operator
    Last edited by Hodor; 11-20-2013 at 01:12 AM.

  13. #28
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    ok after reading the 27 posts, all i can say is O.....M....G......

    This post went a little nuts didnt it???

    not to mention doing all the work for the new kid.....

  14. #29
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Crossfire View Post
    ok after reading the 27 posts, all i can say is O.....M....G......

    This post went a little nuts didnt it???

    not to mention doing all the work for the new kid.....
    He posted a solution. So... does it matter after they have a solution of their own?

  15. #30
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're all wrong. This is the correct solution:

    Floyd triangle - hint plz-floydtriangle-png

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Floyd's algorithm
    By Olar Adrian in forum C Programming
    Replies: 2
    Last Post: 05-14-2011, 09:43 AM
  2. I want a hint
    By Dan. in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2011, 03:36 PM
  3. I need a hint.
    By hyrule in forum C++ Programming
    Replies: 4
    Last Post: 09-04-2005, 09:31 PM
  4. Help with Floyd algorithm
    By diko in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 05-04-2005, 11:27 AM
  5. i need hint...
    By jk81 in forum C Programming
    Replies: 3
    Last Post: 09-12-2002, 08:38 PM