Raggaed Array

This is a discussion on Raggaed Array within the C++ Programming forums, part of the General Programming Boards category; Creating a pascal triangle using a ragged array, or so i think... Problem is it works great until it prints ...

  1. #1
    Unregistered
    Guest

    Raggaed Array

    Creating a pascal triangle using a ragged array, or so i think... Problem is it works great until it prints the triangle out, it prints in the correct format, but the numbers are completely horrible negative numbers.

    Code:
    #include <iostream.h>
    
    int **BldCell(int size);
    void FillCell(int **cells,int size);
    void PrintCell(int **cells,int size);
    
    int main(void)
    {
    int **cells = 0;
    int size = 0;
    int row = 0, col = 0;
    
    	for (row = 0; row < size; row++)
    	{
    		for (col = 0; col < size; col++)
    		{
    			cells[row][col] = 0;
    		}
    	}
    
    	cout << "\nEnter the size of the Pascal Triangle: ";
    	cin >> size;
    
    	cells = BldCell(size);
    	FillCell(cells,size);
    	PrintCell(cells,size);
    
    return 0;
    }
    
    int **BldCell(int size)
    {
    
    	int **cells;
    	int a;
    
    
    	cells = new int * [size];
    
    	for (a = 0; a<size; a++)
    	{
    		cells[a] = new int [a + 1];
    	}
    
    return cells;
    }
    
    void FillCell (int **cells,int size)
    {
    	int row = 0;
    	int col = 0;
    
    	cells[0][0] = 1;
    
    	for (row = 1; row<size; row++)
    	{
    		for (col = 0; col<size; col++)
    		{
    			if (col == 0)
    				cells[row][col] = cells[row - 1][0];
    			else if (col <= row)
    				cells[row][col] = (cells[row - 1][col - 1] + 
    				cells[row - 1][col]);
    			else if (col > row)
    				cells[row][col] = 0;
    		}
    	}     
    
    return;
    }
    
    void PrintCell(int **cells,int size)
    {
    
    	int row = 0;
    	int col = 0;
    
    	for (row = 0; row<size; row++)
    	{
    		for (col = 0; col<=row; col++)
    		{
    			cout << cells[row][col];
    		}
    		cout << endl;
    	//delete **cells;
    	}
    
    return;
    }
    Thanx

    p.s. msvc++

    p.p.s sorry that i didnt put any comments in there

  2. #2
    Unregistered
    Guest

    fixed it

    well thx to those of u who looked at mah post, i figured it out.
    thx again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21