P
pradeep
Hello friends:
I need to write a pascal triangle. Can I do that in C/C++? If so, how?
Thanks.
I need to write a pascal triangle. Can I do that in C/C++? If so, how?
Thanks.
Richard said:pradeep said:
That remains to be seen.
By making an effort that we can help you to improve upon, not by
asking someone else to do it for you - which is how your request
reads at present.
Sixthly, hand in your work to your teacher
pradeep said:Hello friends:
I need to write a pascal triangle. Can I do that in C/C++? If so, how?
pradeep said:I need to write a pascal triangle. Can I do that in C/C++? If so, how?
pradeep said:I need to write a pascal triangle. Can I do that in C/C++? If so, how?
Jens Thoms Toerring said:if ( ! ( l = malloc( o * sizeof *l ) ) ) {
Thanks for all the answers.
It's not homework - it's for my little sister's birthday!
Please see bellow what I tried. It works but does not look
nice. Any ideas how to make it more symmetric and look nicer?
I think the program should ask what n should be. Can I do that
in C/C++? If so, how?
#include<stdio.h>
const n = 15;
int a,b,i,j,x[100];
main()
{
for(i = 0; i < n; i++) x = 0;
for(i = 0; i < n; i++)
{
a = 0; b = 1;
for(j = 0; j < i; j++)
{
x[j] = a+b; printf("%5i", x[j]);
a = b; b = x[j+1];
}
printf("\n");
}
}
Make that line
if ( ! ( l = malloc( ( o || 1 ) * sizeof *l ) ) ) {
or it won't work correctly for 0th order.
Make it the above and it won't work for orders > 1
[|| yields zero or one. (o ? o : 1) works as does (o + !o) or even
(o + 1).]
Here what does *e equal to?Jens said:pradeep said:I need to write a pascal triangle. Can I do that in C/C++? If so, how?
You can do that in C (and rather likely also in C++). And since
I'm in the mood for doing homework today here's one of the many
ways you could do it:
#include <stdio.h>
#include <stdlib.h>
void pt( long o, long *l ) {
long t = *l;
if ( o )
for ( pt( --o, l ), printf( "1 " ); o; t = l[ --o ] )
printf( "%ld ", l[ o ] = l[ o - 1 ] + t );
puts( "1" );
}
int main( int argc, char *argv[ ] ) {
long o, *l;
char *e;
if ( argc < 2 ) {
fprintf( stderr, "Usage: pt order\n" );
exit( EXIT_FAILURE );
}
o = strtol( argv[ 1 ], &e, 10 );
if ( *e != '\0' || o < 0 ) {
#include <stdio.h>LL said:Here what does *e equal to?Jens said:pradeep said:I need to write a pascal triangle. Can I do that in C/C++? If so, how?
You can do that in C (and rather likely also in C++). And since
I'm in the mood for doing homework today here's one of the many
ways you could do it:
#include <stdio.h>
#include <stdlib.h>
void pt( long o, long *l ) {
long t = *l;
if ( o )
for ( pt( --o, l ), printf( "1 " ); o; t = l[ --o ] )
printf( "%ld ", l[ o ] = l[ o - 1 ] + t );
puts( "1" );
}
int main( int argc, char *argv[ ] ) {
long o, *l;
char *e;
if ( argc < 2 ) {
fprintf( stderr, "Usage: pt order\n" );
exit( EXIT_FAILURE );
}
o = strtol( argv[ 1 ], &e, 10 );
if ( *e != '\0' || o < 0 ) {
If a programming competition problem involved calculating values inRichard said:...hand in your work to your teacher
Could you provide some evidence for this please?Richard said:...int main(void) is the modern style...
Albert said:Could you provide some evidence for this please?
Albert said:Could you provide some evidence for this please?
LL said:Here what does *e equal to?Jens said:o = strtol( argv[ 1 ], &e, 10 );
if ( *e != '\0' || o < 0 ) {
Are you referring to what you have quoted or the OP?Phil said:Is the making of the prior style obsolete enough?
No, I didn't.Did you think of checking a recent standards document before
posting?
Richard Heathfield wrote:...hand in your work to your teacher
If a programming competition problem involved calculating values in
Pascals' triangle, then one can't 'hand in [their] work to [their]
teacher.' This is because there are programming competitions for
pre-university students. I point this out so that if I happen to post a
question (like the OP) that would require someone else doing most of the
work, that people in clc don't assume me (or anyone in a similar
situation) to be doing a tertiary course on algorithm design/programming.
How have I referenced 'cheating' at all?so in your world it is ok to cheat in a "programming competition for
pre-university students" whilst it is not ok to cheat a "tertiary
course
on algorithm design/programming"?
That's fine, actually - but, could you please find an *older* post fromThe term "homework" covers more than just homework in a formal course
of education.
Okay - please remind me if any of my future posts shows that I'vejudge".
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.