?
Solved

Pascal' Triangle in C, not in C++

Posted on 2003-03-25
9
Medium Priority
?
893 Views
Last Modified: 2011-10-03
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
Comment
Question by:MikeKarolczak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 8

Expert Comment

by:Exceter
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

by:MikeKarolczak
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

by:gj62
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Accepted Solution

by:
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

by:gj62
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

by:Kocil
ID: 8206180
sorry gj
I was taking 5 minutes :)
0
 
LVL 6

Expert Comment

by:gj62
ID: 8206301
No worries Kocil...

forgot all about the * width specifier...
0
 
LVL 5

Expert Comment

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

Author Comment

by:MikeKarolczak
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question