Thread: C Programming Pascals Triangle

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    29

    C Programming Pascals Triangle

    I have to write a program to print pascals triangle and stores it in a pointer to a pointer , which I am not entirely sure how to do. I also have to write the file and read it, then create a binary file. Assignment is attached. I am not the best with programming and especially with pointers. I will post my code below.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void writePascalTriangle(char *fileName, int heightOfTriangle, int **triangle)
    {
        FILE *fp;
        fp=fopen("writePascalTriangle.txt", "w");
        fclose(fp);
    }
    
    
    void readPascalTriangle(char *fileName, int *heightOfTriangle, int **triangle)
    {
        FILE *fp2;
        fp2=fopen("writePascalTriangle.txt", "r");
        fclose(fp2);
    
    
    }
    
    
    int **getPascalTriangle(int n)
    {
       int c;
       int **result = 1;
    
    
       for( c = 1 ; c <= n ; c++ )
             result = &result*c;
    
    
       return ( &result );
    }
    
    
    
    
    int main()
    {
       int i, n, r;
    
    
       printf("Enter the number of rows you wish to see in pascal triangle\n");
       scanf("%d",&r);
    
    
       for ( i = 0; i < r; i++ )
       {
          for ( n = 0; n<= ( r - i - 2 ); n++ )
             printf(" ");
    
    
          for( n = 0; n <= i; n++ )
    
    
             printf("%d ", getPascalTriangle(i)/(getPascalTriangle(n)*getPascalTriangle(i-n)));
    
    
          printf("\n");
       }
    
    
       return (0);
    }
    Attached Images Attached Images

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    29
    I am just not entirely sure how to store the value of the getPascalsTriangle in a pointer to a pointer? As in the other thread he does not store it in a pointer.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    read info on this link Pointers in C - Tutorial - Cprogramming.com

    Maybe you will understand why this line below is bad code.
    You are returning the address of a local variable this is always a bad thing to do in C.

    Code:
    return ( &result );
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    29
    Yes i understand, I did not mean to type that in this forum.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stuck on Programming Problem (Pascal's Triangle)
    By Mason Jarman in forum C Programming
    Replies: 7
    Last Post: 12-17-2013, 06:58 PM
  2. Replies: 4
    Last Post: 10-10-2011, 09:43 AM
  3. Problem with nCr - Pascals Triangle
    By mike_g in forum C Programming
    Replies: 7
    Last Post: 03-11-2008, 04:34 PM
  4. Triangle
    By howeezy in forum C++ Programming
    Replies: 7
    Last Post: 10-10-2005, 09:36 AM
  5. Replies: 1
    Last Post: 01-30-2002, 01:04 PM

Tags for this Thread