PASCAL'S TRIANGLE

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • GUPTAJI

    #1

    PASCAL'S TRIANGLE

    hi all,

    can u give me the code to create a Pascal's Triangle....... .... and
    yes, it should work

    thankx

  • Zara

    #2
    Re: PASCAL'S TRIANGLE

    On 4 Oct 2006 21:15:20 -0700, "GUPTAJI" <pawangupta.acc @gmail.com>
    wrote:
    >hi all,
    >
    >can u give me the code to create a Pascal's Triangle....... .... and
    >yes, it should work
    >
    >thankx
    int main() {
    printf("create a Pascal's Triangle\n");
    printf("...do your homework!!!\n") ;
    return -1;
    }

    Comment

    • jmcgill

      #3
      Re: PASCAL'S TRIANGLE

      GUPTAJI wrote:
      hi all,
      >
      can u give me the code to create a Pascal's Triangle....... .... and
      yes, it should work
      If you can make this print one more row correctly, I will be impressed.


      #include <stdio.h>

      unsigned long factorial(unsig ned long a){
      return a == 0UL ? 1UL : a * factorial(a-1UL);
      }

      int main(int argc, char **argv){

      unsigned long row, num, val, fact;

      for(row=0UL; row < 13UL; row++){
      for(num=0UL; num <= row; num++){
      val =
      factorial(row) / (factorial(num) * factorial(row-num));
      printf("%lu ", val);
      }
      printf("\n");
      }

      return 0;
      }

      Comment

      • Richard Heathfield

        #4
        Re: PASCAL'S TRIANGLE

        jmcgill said:
        GUPTAJI wrote:
        >hi all,
        >>
        >can u give me the code to create a Pascal's Triangle....... .... and
        >yes, it should work
        >
        If you can make this print one more row correctly, I will be impressed.
        1 12 66 220 495 792 924 792 495 220 66 12 1

        Prepare to be impressed.

        After the loop's } but before the return 0, add this line:

        printf("1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1\n");

        --
        Richard Heathfield
        "Usenet is a strange place" - dmr 29/7/1999

        email: rjh at above domain (but drop the www, obviously)

        Comment

        • jmcgill

          #5
          Re: PASCAL'S TRIANGLE

          Richard Heathfield wrote:
          jmcgill said:
          >
          >GUPTAJI wrote:
          >>hi all,
          >>>
          >>can u give me the code to create a Pascal's Triangle....... .... and
          >>yes, it should work
          >If you can make this print one more row correctly, I will be impressed.
          >
          1 12 66 220 495 792 924 792 495 220 66 12 1
          >
          Prepare to be impressed.
          I started writing a program that would find the (n+1)th row by
          evaluating only the n-th row as an array, but I expected one or two
          of the quick thinkers on the list to (a.) correct the errors in my first
          program pointing out more problems than there were expressions, and (b.)
          post some solution that renders the correct result without any arbitrary
          limits, formatting the output so that lines are centered, and all in a
          single line of code with no assignments :-)


          Comment

          • Richard Heathfield

            #6
            Re: PASCAL'S TRIANGLE

            jmcgill said:
            Richard Heathfield wrote:
            >jmcgill said:
            >>
            >>GUPTAJI wrote:
            >>>hi all,
            >>>>
            >>>can u give me the code to create a Pascal's Triangle....... .... and
            >>>yes, it should work
            >>If you can make this print one more row correctly, I will be impressed.
            >>
            >1 12 66 220 495 792 924 792 495 220 66 12 1
            >>
            >Prepare to be impressed.
            >
            I started writing a program ...
            My immediate reaction was to post a program that generates a Sierpinski
            triangle, leaving the filling in of the actual numbers as an exercise for
            the reader. But apathy prevailed.

            --
            Richard Heathfield
            "Usenet is a strange place" - dmr 29/7/1999

            email: rjh at above domain (but drop the www, obviously)

            Comment

            • jmcgill

              #7
              Re: PASCAL'S TRIANGLE

              GUPTAJI wrote:
              hi all,
              >
              can u give me the code to create a Pascal's Triangle....... .... and
              yes, it should work
              >
              thankx
              >
              #include <stdio.h>
              #include <stdlib.h>

              void print_row(unsig ned long *row){
              unsigned long v;
              while( 0UL != (v = *row++)){
              printf("%lu ", v);
              }
              printf("\n");
              }

              int card_row(unsign ed long *row){
              int i;
              i=0;
              while( 0UL != *row++){
              i++;
              }
              return i;
              }

              unsigned long * next_row(unsign ed long * row){

              unsigned long * retval;
              int count, i;

              count = card_row(row);

              retval = malloc( (count+2) * sizeof(unsigned long));

              *(retval+0) = 1UL;
              *(retval+count+ 1) = 0UL;

              for(i=1; i<=count; i++){
              *(retval + i) = *(row + i -1) + *(row + i);
              }

              return retval;

              }

              int main(int argc, char **argv){

              int c;
              unsigned long *row_cur, *row_next;

              row_cur = malloc(2 * sizeof(unsigned long));

              *row_cur = 1UL; *(row_cur+1) = 0UL;

              print_row(row_c ur);

              for( c= 0; c< 20; c++){

              row_next = next_row(row_cu r);
              print_row(row_n ext);
              free(row_cur);
              row_cur = row_next;
              }

              free(row_cur);

              return 0;
              }

              Comment

              • jacob navia

                #8
                Re: PASCAL'S TRIANGLE

                Why do you do his homework?

                You are not really helping him

                Comment

                • Mabden

                  #9
                  Re: PASCAL'S TRIANGLE

                  "jmcgill" <jmcgill@email. arizona.eduwrot e in message
                  news:Hj1Vg.755$ rS.421@fed1read 05...
                  I started writing a program
                  I've done that!
                  but I expected one or two of the quick thinkers on the list to
                  (a.) correct the errors in my first program
                  (b.) post some solution
                  And where did you send the money? I'm not the quickest of "the thinkers on
                  list" but I waited by the mailbox and my box of money never came.

                  Of course, I always wait by the mailbox for my box of money, and your's not
                  coming was just one more daily disappointment. I bet you are not your
                  father's favorite son...

                  The solution is right here. Please send the money NOW. If you don't The
                  Solution will be deleted from my hard drive! I am SERIOUS! Send money
                  NOW!!!!

                  --
                  Mabden


                  Comment

                  • Tak-Shing Chan

                    #10
                    Re: PASCAL'S TRIANGLE

                    On Wed, 4 Oct 2006, jmcgill wrote:
                    GUPTAJI wrote:
                    >hi all,
                    >>
                    >can u give me the code to create a Pascal's Triangle....... .... and
                    >yes, it should work
                    >
                    If you can make this print one more row correctly, I will be impressed.
                    >
                    >
                    #include <stdio.h>
                    >
                    unsigned long factorial(unsig ned long a){
                    return a == 0UL ? 1UL : a * factorial(a-1UL);
                    }
                    >
                    int main(int argc, char **argv){
                    >
                    unsigned long row, num, val, fact;
                    >
                    for(row=0UL; row < 13UL; row++){
                    for(num=0UL; num <= row; num++){
                    val =
                    factorial(row) / (factorial(num) * factorial(row-num));
                    printf("%lu ", val);
                    }
                    printf("\n");
                    }
                    >
                    return 0;
                    }
                    #include <stdio.h>
                    #include <math.h>

                    int main(void)
                    {
                    unsigned long r, n, v;
                    double f;

                    /*
                    * Printing one more row correctly, using floating
                    * point technology.
                    */
                    for (r = 0; r < 14; puts(""), r++)
                    for (n = 0; n <= r; printf("%.0f ", exp(f)), n++)
                    for (v = f = 0; v < n; v++)
                    f += log(r - v) - log(v + 1);
                    return 0;
                    }

                    Tak-Shing

                    Comment

                    • mark_bluemel@pobox.com

                      #11
                      Re: PASCAL'S TRIANGLE


                      GUPTAJI wrote:
                      hi all,
                      >
                      can u give me the code to create a Pascal's Triangle....... .... and
                      yes, it should work
                      Come on someone, post a program that's good enough for him to submit it
                      but bad enough for his tutor to fail him...

                      Comment

                      • Zara

                        #12
                        Re: PASCAL'S TRIANGLE

                        On 5 Oct 2006 06:34:53 -0700, mark_bluemel@po box.com wrote:
                        >
                        >GUPTAJI wrote:
                        >
                        >hi all,
                        >>
                        >can u give me the code to create a Pascal's Triangle....... .... and
                        >yes, it should work
                        >
                        >Come on someone, post a program that's good enough for him to submit it
                        >but bad enough for his tutor to fail him...
                        Here it is....

                        #include <stdio.h>

                        /* No one specified which should be the last line! */

                        int main() {
                        int line,item, first_line[1]={1},*last_line =first_line,*li ne_now;
                        for (line=2;line<=7 ;line++) {
                        line_now=(int *)malloc(line*s izeof(int));
                        line_now[0]=1;
                        line_now[line-1]=1;
                        for(item=1;item <line-1;item++) {
                        line_now[item]=last_line[item-1]+last_line[item];
                        }
                        for (item=0;item<li ne;i) {
                        printf("%d%c",l ine_now[item++],' ');
                        }
                        printf("%c",'\n ');
                        last_line=line_ now;
                        }
                        }


                        It probably works (not tested!) with the following features for the
                        student:

                        - Horrible coding style
                        - Horrible memory management
                        - Memory leaks included
                        - Magic number somewhere around
                        - Not created by OP

                        Zara

                        Comment

                        • Ian Malone

                          #13
                          Re: PASCAL'S TRIANGLE

                          Mabden wrote:
                          "jmcgill" <jmcgill@email. arizona.eduwrot e in message
                          news:Hj1Vg.755$ rS.421@fed1read 05...
                          >I started writing a program
                          >
                          I've done that!
                          >
                          >but I expected one or two of the quick thinkers on the list to
                          >(a.) correct the errors in my first program
                          >(b.) post some solution
                          >
                          And where did you send the money? I'm not the quickest of "the thinkers on
                          list" but I waited by the mailbox and my box of money never came.
                          >
                          <snip>
                          You may not have noticed, but jmcgill wasn't actually the OP
                          requesting help with a homework problem, and I'm thinking
                          the expectation was a little jovial.

                          --
                          imalone

                          Comment

                          • osmium

                            #14
                            Re: PASCAL'S TRIANGLE

                            "GUPTAJI" writes:
                            can u give me the code to create a Pascal's Triangle....... .... and
                            yes, it should work
                            Try to code something up with the information contained in this link. If
                            you have a problem, post the code you produced.




                            Comment

                            • jmcgill

                              #15
                              Re: PASCAL'S TRIANGLE

                              jacob navia wrote:
                              Why do you do his homework?
                              >
                              You are not really helping him
                              I don't care about him. I did the exercise because it helped *me*.

                              Comment

                              Working...