Thread: Floyd triangle - hint plz

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    Exclamation Floyd triangle - hint plz

    Code:
    /* Shown below is an Floyd's triangle*/
    1
    2 3
    4 5 6
    7 8 9 10
    11 .... 15
    ..
    ..
    79 ......... 91
    /* Give me the idea to generate this, thankq */

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Welcome to the forum. Please post your attempt to solve this problem. Or, lacking an attempt, tell us what ideas you have thought of so far.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    2
    Code:
    #include <stdio.h>
    
    
    int main()
    {
    	int i, j, num = 1;
    	
    	for (i = 1; i <= 91; i++)
    	{
    		for (j = 1; j <= i; j++)
    		{
    			printf("%d ", num);
    			num++;
    			if (num == 92)
    				break;
    		}
    		
    		printf("\n");
    		if (num == 92)
    			break;
    	}
    	
    	return 0;
    		
    }
    Stop insulting other.

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by dipen45 View Post
    Stop insulting other.
    Hodor
    Last edited by Hodor; 11-19-2013 at 08:25 PM.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You don't know what an insult actually is, but we'll stop "insulting" when you decide to post in one forum.

  7. #7
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Not quite...here's my solution:

    Code:
    #include <stdio.h>
    
    void printFloyd(int size);
    
    int main(void)
    {
        printFloyd(91);
        return 0;
    }
    
    void printFloyd(int size)
    {
        int num, cols, cols_per_row;
    
        cols_per_row = 1; /* # columns to start out with */
        for (num = 1, cols = 0; num <= size; ++num) {
            printf("%3d ", num);
            ++cols; /* # columns in this row so far */
    
            /* Did we hit our column limit for this row? */
            if (cols >= cols_per_row) {
                printf("\n");
                cols = 0;           /* New row, so reset column count */
                cols_per_row++;     /* Next row has +1 more columns */
            }
        }
    }
    Hodor you're missing some logic in yours (as well as the i in your printf() statement)

  8. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by nonpuz View Post

    Hodor you're missing some logic in yours (as well as the i in your printf() statement)
    Yeah, I typed it straight into the website and then realised I should have checked it first =)

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int i = 0, c = 0, m = 0, n = 91;
        static const char delim[] = {' ', '\n'};
    
        while (i < n) {
            printf("%d%c", i + 1, delim[c == m]);
            if (c == m)
                c = 0, m++;
            else
                c++;
            i++;
        }
        return 0;
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hodor, this:
    Code:
            if (c == m)
                c = 0, m++;
    could be simplified to:
    Code:
            if (c == m)
                c = m++;
    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

  10. #10
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by laserlight View Post
    Hodor, this:
    Code:
            if (c == m)
                c = 0, m++;
    could be simplified to:
    Code:
            if (c == m)
                c = m++;
    Hodor? You sure?

    Edit:
    Code:
    c = !++m;
    would work...
    Last edited by Hodor; 11-19-2013 at 09:15 PM.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What do you want that bit of code to do? The comma operator just discards the thing on the left, so the 0 is completely ignored. If you want some sort of if statement, then maybe you're looking for ?: ?

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Hodor
    Code:
    c = !++m;
    would work...
    If your intention is to write:
    Code:
    c = 0;
    m++;
    Then just write that instead of trying to be unnecessarily concise and making it difficult to understand for both yourself and those who read your code.
    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

  14. #14
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by laserlight View Post
    If your intention is to write:
    Code:
    c = 0;
    m++;
    Then just write that instead of trying to be unnecessarily concise and making it difficult to understand for both yourself and those who read your code.
    I had no itention of writing that at all; I was merely trying to make sense of your earlier comment. Hmm

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    In effect, c = 0, m++; is c = m++; the assignment to zero may as well have never happened.

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