Link to home
Start Free TrialLog in
Avatar of MikeKarolczak
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;
   }
}
Avatar of Exceter
Exceter
Flag of United States of America image

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
Avatar of MikeKarolczak
MikeKarolczak

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
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);
 }
}
ASKER CERTIFIED SOLUTION
Avatar of Kocil
Kocil

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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...
sorry gj
I was taking 5 minutes :)
No worries Kocil...

forgot all about the * width specifier...
on the other side,
I was taking soo long cause I didn't know what is setw()
I accepted the Answer from Kocil because it's very simple and it solves my problem. But thank you gj62 for your help.