Pascal Triangle doubt - Help!

P

pradeep

Hello friends:

I need to write a pascal triangle. Can I do that in C/C++? If so, how?

Thanks.
 
D

Darren Cubitt

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.

Oh, so I wasn't the only one thinking that! Good!

Sixthly, hand in your work to your teacher

Oh my... how cynical of you.

Probably correct, but still cynical ;)
 
F

Flash Gordon

pradeep said:
Hello friends:

I need to write a pascal triangle. Can I do that in C/C++? If so, how?

The algorithm can certainly be implemented in C (and therefor also in C++).

First you need to understand what a Pascal Triangle is.
Then you need to decide on an appropriate algorithm for implementing it.
Then you need to decide what form your output will take.
If the output is something other than plain text (e.g. graphics) you
need to decide on appropriate libraries.
Then you need to decide which language you want to use, C and C++ are
DIFFERENT languages.
Then you write code to implement the above.

Once you have made the attempt, if you use C you can post it here for
help and advice. If you choose C++ then comp.lang.c++ would be the place
to go.

To learn you need to make a serious attempt and then ask for help with
specific problems and/or ask for a critique of code you have written
(including telling people any problems you are aware of).
 
J

Jens Thoms Toerring

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 ) {
fprintf( stderr, "Invalid order, must be a non-negative integer\n"
);
exit( EXIT_FAILURE );
}

if ( ! ( l = malloc( o * sizeof *l ) ) ) {
fprintf( stderr, "Running out of memory\n" );
exit( EXIT_FAILURE );
}

*l = 1;
pt( o, l );
free( l );
return 0;
}
Regards, Jens
 
O

osmium

pradeep said:
I need to write a pascal triangle. Can I do that in C/C++? If so, how?

Sure you can. I think the link is probably as good a starting point as any.
I don't see any obvious reason to choose C++ over C so choose *a* language
for the problem and dig in. Don't ask questions about C/C++, it irritates
the inmates - you should know that after a year on the froup.

http://en.wikipedia.org/wiki/Pascal's_triangle
 
P

pradeep

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");
}
}

Thanks.
 
J

Jens Thoms Toerring

Jens Thoms Toerring said:
if ( ! ( l = malloc( o * sizeof *l ) ) ) {

Make that line

if ( ! ( l = malloc( ( o || 1 ) * sizeof *l ) ) ) {

or it won't work correctly for 0th order.

Regards, Jens
 
B

Barry Schwarz

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?

Each row contains i numbers. Each number occupies five characters.
Therefore each row occupies 5*i characters. There are n rows. Row
n-1 is the last. It contains (n-1)*5*i characters. Each earlier row
contains less. To center each or these earlier rows directly above
the last row, compute the difference between the number of characters
in the current row and the number of characters in the last row. You
want half this difference to be skipped at the start of the current
row. Simply print this many blanks prior to printing x[0] for that
row.
I think the program should ask what n should be. Can I do that
in C/C++? If so, how?

Use printf and fflush to print a request for the number of rows. Use
fgets read the user's input and strtol to convert the input to an
integer. While you are at it you can test the input to make sure the
user did not enter too many characters and have strtol indicate if the
string entered was a valid integer or not.
#include<stdio.h>

const n = 15;
int a,b,i,j,x[100];

main()
{
for(i = 0; i < n; i++) x = 0;


Are you aware that because you defined x at file scope, all 100
elements are automatically initialized to zero. On the other hand,
the variables should not be at file scope anyway. Move them into
main.
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");
}
}
 
B

Ben Bacarisse

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).]
 
J

Jens Thoms Toerring

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).]

Grrrr, Perl syndrom, so I vote for '(o + !o)', that looks the most
weird and does what I wanted at the same time - either 'o' or, if
'o' is 0, 1;-) '(o + 1)' would allocate one element more than is
strictly needed)...
Regards, Jens
 
L

LL

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 ) {
Here what does *e equal to?
 
L

LL

LL said:
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 ) {
Here what does *e equal to?
#include <stdio.h>

int main() {
char* s="abc";

// Is this 'b'?
printf("%c", *(s+1)); // Yes.
}
 
A

Albert

Richard said:
...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.
 
P

Phil Carmody

Albert said:
Could you provide some evidence for this please?

Is the making of the prior style obsolete enough?

Did you think of checking a recent standards document before
posting?

Phil
 
J

Jens Thoms Toerring

LL said:
Jens said:
o = strtol( argv[ 1 ], &e, 10 );
if ( *e != '\0' || o < 0 ) {
Here what does *e equal to?

To the first character that couldn't be converted. So if
everything of argv[1] was part of a number it points to
the trailing '\0', if it was e.g. "10e" it will point to
the 'e' in the string and if it was "abc" it points to
the leading 'a'. The test admittedly misses the case that
argv[1] is the empty string ('o' is then set to 0). To
remedy this it would have to be

if ( ! *argv[1] || *e || o < 0 ) {

Regards, Jens
 
G

Guest

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.

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"?

The term "homework" covers more than just homework in a formal course
of education. For the word "teacher" just substitute "competition
judge".
 
A

Albert

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"?
How have I referenced 'cheating' at all?
The term "homework" covers more than just homework in a formal course
of education.
That's fine, actually - but, could you please find an *older* post from
clc that supports your statement? (and I'm not trying to be smart/funny)
; if you have a serious issue with the way I've written the question
please rephrase it.

For the word "teacher" just substitute "competition
Okay - please remind me if any of my future posts shows that I've
forgetten to.
 

Ask a Question

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.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top