Need Help With Printing Pascal's Triangle In C

Closed Thread

Join Date: Jul 2005
Posts: 3
Reputation: uzone24 is an unknown quantity at this point 
Solved Threads: 0
uzone24 uzone24 is offline Offline
Newbie Poster

Re: Need Help With Printing Pascal's Triangle In C

 
0
  #11
Jul 29th, 2005
y wud it suck???????????
Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Need Help With Printing Pascal's Triangle In C

 
0
  #12
Jul 29th, 2005
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.
Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 716
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Need Help With Printing Pascal's Triangle In C

 
0
  #13
Jul 29th, 2005
>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 ||):
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main ( void )
  5. {
  6. int i, j;
  7. int ary[10][10], n;
  8.  
  9. printf ( "Enter the number of rows:" );
  10. fflush ( stdout );
  11.  
  12. if ( scanf ( "%d", &n ) != 1 || n < 0 || n > 10 )
  13. return EXIT_FAILURE;
  14.  
  15. for ( i = 0; i < n; i++ ) {
  16. for ( j = 0; j <= i; j++ ) {
  17. if ( j == 0 || j == 1 )
  18. ary[i][j] = 1;
  19. else
  20. ary[i][j] = ary[i - 1][j - 1] + ary[i - 1][j];
  21. }
  22. }
  23.  
  24. for ( i = 0; i < n; i++ ) {
  25. for ( j = 0; j <= i; j++ )
  26. printf ( "%-6d", ary[i][j] );
  27. printf ( "\n" );
  28. }
  29.  
  30. return EXIT_SUCCESS;
  31. }
And the output on my system is (drumroll please):
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
That's certainly a different way of displaying Pascal's triangle. Compare that with a correct program:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main ( void )
  5. {
  6. int i, j;
  7. int n;
  8.  
  9. printf ( "Enter the number of rows: " );
  10. fflush ( stdout );
  11.  
  12. if ( scanf ( "%d", &n ) != 1 || n < 0 || n > 10 )
  13. return EXIT_FAILURE;
  14.  
  15. for ( i = 0; i < n; i++ ) {
  16. int result = 1;
  17.  
  18. for ( j = 0; j < n; j++ ) {
  19. if ( j > 0 )
  20. result = result * ( i + j ) / j;
  21.  
  22. printf ( "%-6d", result );
  23. }
  24.  
  25. printf ( "\n" );
  26. }
  27.  
  28. return EXIT_SUCCESS;
  29. }
And you get a more recognizable table:
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 48620
>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.
I'm here to prove you wrong.
Quick reply to this message  
Join Date: Oct 2006
Posts: 3
Reputation: willzyba is an unknown quantity at this point 
Solved Threads: 0
willzyba willzyba is offline Offline
Newbie Poster

Re: Need Help With Printing Pascal's Triangle In C

 
0
  #14
Nov 1st, 2006
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
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.
Quick reply to this message  
Join Date: Oct 2006
Posts: 2,839
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 298
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: Need Help With Printing Pascal's Triangle In C

 
0
  #15
Nov 1st, 2006
I'm sorry to disappoint everyone. But what exactly are you calculating, its certainly not Pascal's Triangle.
Yes it is, if you follow this link (the one you posted), you will see that it is exactly the same as the one in Narue's post, but tilted 45 degrees.

But I guess you won't be able to reply since on of the mod's is probably going to lock this thread
Quick reply to this message  
Closed Thread

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC