Thread: Pascal Triangle

  1. #1
    Unregistered
    Guest

    Pascal Triangle

    Ok, i have written this program w/ a one-dim. array, but the assignment calls for a two-dim. array. With the two-dim array on the output i get a weird negative number as follows :

    Output :

    1
    -895433063 1

    The one's are in the correct places, but the -number is screwing me up.

    Here's my code :

    //Global Varibles
    const long int MAX_ROWS = 10;
    const long int MAX_COLS = 10;

    //Begin Main
    void main(void)
    {
    //Variables
    long last[MAX_ROWS][MAX_COLS];
    long current[MAX_ROWS][MAX_COLS];
    int x;
    int y;
    int size;
    int find = 0;

    current[1][0] = 1;
    last[0][0] = 0;
    last[1][0] = 1;

    int a = 1;
    int b = 0;

    //Statements
    cout << "Enter in size of triangle : ";
    cin >> size;
    if(size == 1)
    {
    printf("1\n");
    }

    else
    {
    printf("1\n");

    while (1)
    {
    a++;
    b++;
    find = 0;
    for (x = 1; x < a; x++)
    {
    for(y = 0; y < b; y++)
    {
    current[x][y] = last[x - 1][y] + last[x - 1][y - ];
    }
    }

    for (x = 0; x < a; x++)
    {
    for(y = 0; y < b; y++)
    {
    last[x][y] = current[x][y];
    last[a][b] = 1;
    }
    }
    for (x = 1; x <= a; x++)
    {
    for(y = 1; y <= b; y++)
    {
    if(size < 4)
    {
    size = size - 1;
    }

    if (last[x][y] > size)
    {
    find = 1;
    }

    if (x > 1)
    printf(" %ld", last[x][y]);
    else
    printf("%ld", last[x][y]);
    }
    }
    printf("\n");

    if(find)
    {
    break;
    }
    }// end While

    }//End else

    getch();

    }
    //End Main, End Program

    -JMM

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    i didnt read it but large weird negative numbers mean you probably didnt ever chnage the data in the variable from its inital value.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    11
    I wish you could include more descriptions with your code. It would be easier to follow.

    you only need one 2-D array and 4 loops.

    triangle[0][0]=1;

    for(int a=1; a<size; a++)
    triangle[a][0]=1;
    for(int b=1; b<a; b++)//you need to finish this loop
    //find all values for triangle[MAX_ROWS][MAX_COLS]

    for(int a=1; a<size; a++)
    cout << triangle[a-1][0];
    for(int b=1; b<a; b++)
    cout << triangle[a-1][b];
    cout << endl;
    //print the triangle from one matrix

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive Triangle Function
    By w2look in forum C Programming
    Replies: 14
    Last Post: 11-13-2010, 02:31 PM
  2. Right Triangle Program
    By BSmith4740 in forum C# Programming
    Replies: 9
    Last Post: 02-27-2008, 12:24 AM
  3. Just in case: "Odd" Triangle Challenge (for me)
    By BB18 in forum C Programming
    Replies: 3
    Last Post: 10-09-2004, 12:02 AM
  4. pascal triangle
    By ndukaa1 in forum C++ Programming
    Replies: 15
    Last Post: 10-05-2004, 10:40 PM
  5. Pascal triangle numbers generation~
    By bljonk in forum C Programming
    Replies: 6
    Last Post: 03-22-2002, 02:25 PM