MikeKarolczak
asked on
Pascal' Triangle in C, not in C++
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;
}
}
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;
}
}
ASKER
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
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
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);
}
}
#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);
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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...
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...
sorry gj
I was taking 5 minutes :)
I was taking 5 minutes :)
No worries Kocil...
forgot all about the * width specifier...
forgot all about the * width specifier...
on the other side,
I was taking soo long cause I didn't know what is setw()
I was taking soo long cause I didn't know what is setw()
ASKER
I accepted the Answer from Kocil because it's very simple and it solves my problem. But thank you gj62 for your help.
#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