Solved

# Pascal' Triangle in C, not in C++

Posted on 2003-03-25
Medium Priority
895 Views
I need to write Pascal's Triangle in pure C. I wrote it in C++ and now i have to correct it.
I can't use cout or setw because there are C++ functions. I have a problem "converting" it to C. Can anybody help me?

/* Pascal's Triangle in C++, must be in ANSI C*/
#include <iostream.h>
#include <iomanip.h>

int C(int n, int k)
{
if (k>0 && k<n)
return C(n-1, k-1) + C(n-1, k);
else
return 1;
}

void main()
{
int n, k;
cout << "Pascal's Triangle:\n\n";
for (n=0; n<=12; n++)
{
cout << setw(2) << n << setw(36-3*n) << "";
for (k=0; k<=n; k++)
cout << setw(6) << C(n, k);
cout << endl;
}
}
0
Question by:MikeKarolczak
• 3
• 3
• 2
• +1

LVL 8

Expert Comment

ID: 8205708
Try this,

#include <stdio.h>

int C(int n, int k)
{
if (k>0 && k<n)
return C(n-1, k-1) + C(n-1, k);
else
return 1;
}

void main()
{
int n, k;
printf("Pascal's Triangle:\n\n");
for (n=0; n<=12; n++)
{
printf("%i", n);
for (k=0; k<=n; k++)
printf("%i\n",C(n, k));
}
}

Exceter
0

Author Comment

ID: 8205894
it doesn't format the numbers as my first program. because i forgot to mention that the problem is the format of numbers on the screen
It should be like this:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
so this is not the answet to my question
0

LVL 6

Expert Comment

ID: 8206142
Here you go...

#include <stdio.h>
#include <string.h>

int C(int n, int k)
{
if (k>0 && k<n)
return C(n-1, k-1) + C(n-1, k);
else
return 1;
}

void center(char *s, int w)
{
int numSpace;
numSpace = (w - strlen(s))/2;
memmove(s+numSpace, s,strlen(s));
memset(s,32,numSpace);
}

void main()
{
int n, k, j;
char string[100];
printf("Pascal's Triangle:\n\n");
for (n=0; n<=12; n++)
{
j = 0;
memset(string, 0, 100);
for (k=0; k<=n; k++)
j += sprintf(string+j,"%3i ",C(n, k));
center(string, 90);
printf("%s\n", string);
}
}
0

LVL 5

Accepted Solution

Kocil earned 880 total points
ID: 8206169
void main()
{
int n, k;
printf("Pascal's Triangle:\n\n");
for (n=0; n<=12; n++)
{
printf("\n%2i%*c", n,36-3*n,' ');
for (k=0; k<=n; k++)
printf("%6i",C(n, k));
}
}

0

LVL 6

Expert Comment

ID: 8206172
OK, a few notes...

I hardcoded a width of 90, which is nice on my screen, but it could vary.  Make sure your string is at least as long as the width you specify in center(), or bad things will happen.

You can make it a user-parameter by simply asking for a width, malloc'ing the string and using that # for center.

Also, this assumes no more than 3 digit numbers will be in the triangle - you have to change the sprintf formatter if you are going to have a larger triangle.

It also assumes a fixed-width font...
0

LVL 5

Expert Comment

ID: 8206180
sorry gj
I was taking 5 minutes :)
0

LVL 6

Expert Comment

ID: 8206301
No worries Kocil...

forgot all about the * width specifier...
0

LVL 5

Expert Comment

ID: 8206353
on the other side,
I was taking soo long cause I didn't know what is setw()
0

Author Comment

ID: 8206746
I accepted the Answer from Kocil because it's very simple and it solves my problem. But thank you gj62 for your help.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: Iâ€™ve never triâ€¦
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address.Â This address might be address of another variable/address of devices/address of fuâ€¦
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
###### Suggested Courses
Course of the Month7 days, 19 hours left to enroll