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 */
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 */
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.
Here also: Idea please - C++ Forum
Stop insulting other.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; }
You don't know what an insult actually is, but we'll stop "insulting" when you decide to post in one forum.
Not quite...here's my solution:
Hodor you're missing some logic in yours (as well as the i in your printf() statement)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 */ } } }
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; }
Hodor, this:
could be simplified to:Code:if (c == m) c = 0, m++;
Code:if (c == m) c = m++;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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 ?: ?
Well since we are all producing solutions now:
That seems to be the hard part.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]] >>>
If your intention is to write:Originally Posted by Hodor
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.Code:c = 0; m++;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
In effect, c = 0, m++; is c = m++; the assignment to zero may as well have never happened.