| | |
Need Help With Printing Pascal's Triangle In C
![]() |
I'm sure it's perfectly fine to
- use i and j without having declared them first,
- use the mystical !! binary operator,
- fill values of 1 into the triangle where values of 1 do not belong,
- print the contents of your datastructure without putting spaces in between numbers,
- wonder why people think your code has problems without ever having tested it.
- use i and j without having declared them first,
- use the mystical !! binary operator,
- fill values of 1 into the triangle where values of 1 do not belong,
- print the contents of your datastructure without putting spaces in between numbers,
- wonder why people think your code has problems without ever having tested it.
>Narue can u elaborate on reasons y my code sucks as well wud fail ??
You need an explanation? Clearly you don't take pride in your own work, or you would have at least tried to compile that code before posting it. But, I'll give you the benefit of the doubt. Let's make your code compilable and then presentable (note that I made no changes to your logic, though I assumed by !! you meant ||):
And the output on my system is (drumroll please):
That's certainly a different way of displaying Pascal's triangle. Compare that with a correct program:
And you get a more recognizable table:
>y wud it suck???????????
No, it's not that it "wud" suck, it's that it does suck. And as for why, because it's not compilable, and even if it were, it would still be wrong and broken. Is that so hard to understand?
As a side note, those dumbass abbreviations don't make you look any smarter. This isn't a chatroom, where slow typers need to abbreviate everything to get their message posted in time. Take your time, use correct spelling and grammer, and people will respect you more for it.
You need an explanation? Clearly you don't take pride in your own work, or you would have at least tried to compile that code before posting it. But, I'll give you the benefit of the doubt. Let's make your code compilable and then presentable (note that I made no changes to your logic, though I assumed by !! you meant ||):
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> int main ( void ) { int i, j; int ary[10][10], n; printf ( "Enter the number of rows:" ); fflush ( stdout ); if ( scanf ( "%d", &n ) != 1 || n < 0 || n > 10 ) return EXIT_FAILURE; for ( i = 0; i < n; i++ ) { for ( j = 0; j <= i; j++ ) { if ( j == 0 || j == 1 ) ary[i][j] = 1; else ary[i][j] = ary[i - 1][j - 1] + ary[i - 1][j]; } } for ( i = 0; i < n; i++ ) { for ( j = 0; j <= i; j++ ) printf ( "%-6d", ary[i][j] ); printf ( "\n" ); } return EXIT_SUCCESS; }
Enter the number of rows: 10
1
1 1
1 1 1244889
1 1 12448904730113
1 1 124489159750038203521
1 1 12448927219894141785248203648
1 1 1244893846478621398418223821728203648
1 1 124489497096792986320443780590305858208203648
1 1 124489510954573395728837364379474366410387894689588966
1 1 12448961219946850527456113216677148010204113155878483784349588968 C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> int main ( void ) { int i, j; int n; printf ( "Enter the number of rows: " ); fflush ( stdout ); if ( scanf ( "%d", &n ) != 1 || n < 0 || n > 10 ) return EXIT_FAILURE; for ( i = 0; i < n; i++ ) { int result = 1; for ( j = 0; j < n; j++ ) { if ( j > 0 ) result = result * ( i + j ) / j; printf ( "%-6d", result ); } printf ( "\n" ); } return EXIT_SUCCESS; }
Enter the number of rows: 10
1 1 1 1 1 1 1 1 1 1
1 2 3 4 5 6 7 8 9 10
1 3 6 10 15 21 28 36 45 55
1 4 10 20 35 56 84 120 165 220
1 5 15 35 70 126 210 330 495 715
1 6 21 56 126 252 462 792 1287 2002
1 7 28 84 210 462 924 1716 3003 5005
1 8 36 120 330 792 1716 3432 6435 11440
1 9 45 165 495 1287 3003 6435 12870 24310
1 10 55 220 715 2002 5005 11440 24310 48620No, it's not that it "wud" suck, it's that it does suck. And as for why, because it's not compilable, and even if it were, it would still be wrong and broken. Is that so hard to understand?
As a side note, those dumbass abbreviations don't make you look any smarter. This isn't a chatroom, where slow typers need to abbreviate everything to get their message posted in time. Take your time, use correct spelling and grammer, and people will respect you more for it.
I'm here to prove you wrong.
•
•
Join Date: Oct 2006
Posts: 3
Reputation:
Solved Threads: 0
I'm sorry to disappoint everyone. But what exactly are you calculating, its certainly not Pascal's Triangle.
If you're in any doubt of what the first few rows of the triangle should look like see: http://en.wikipedia.org/wiki/Pascal_triangle
And some clean code I've found that shows how to calculate any given row in C, is at: <removed spam link>
I know the above posts were made ages ago, but I've been search for code and thought I may as well respond to the above and stop any other 'students' from failing
If you're in any doubt of what the first few rows of the triangle should look like see: http://en.wikipedia.org/wiki/Pascal_triangle
And some clean code I've found that shows how to calculate any given row in C, is at: <removed spam link>
I know the above posts were made ages ago, but I've been search for code and thought I may as well respond to the above and stop any other 'students' from failing
Last edited by WolfPack; Nov 1st, 2006 at 5:53 am. Reason: Removed external link, that looks like a ploy to get users to login to that site.
•
•
•
•
I'm sorry to disappoint everyone. But what exactly are you calculating, its certainly not Pascal's Triangle.
But I guess you won't be able to reply since on of the mod's is probably going to lock this thread
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: frequency problem,can u plz do the documentation for mi
- Next Thread: Frequency of characters entered.
| Thread Tools | Search this Thread |
* adobe api array arrays binarysearch calculate centimeter char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking hardware highest homework i/o ide inches incrementoperators intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation pattern pdf performance pointer posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h





