Pascal's Triangle

Hi all,

I have a question. I have created a code for printing Pascal's Triangle.
There is a pointer to an array of pointer to arrays of integers.

The arrays of integers should be filled with the correct numbers, but I don't seem to get it right.

Furthermore I can't seem to print the arrays on the screen.

Can somebody help me out? Thanks a lot....here's the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

#include <iostream>
#include <iomanip>
using namespace std;

int *createNew (int);
void fillFirstArray (int *, int);
void fillArray (int *, int *, int);
void printTriangle (int *, int);

int main(int argc, char* argv[])
{
   int   size;          //size of Pascal's Triangle, user input.
   int*  *arrayPtr;     //array of pointers, elements equal to size input.
   int   *tempPtr;      //temporarely pointer as input for fillArray function.

   cout << "What size does the triangle need to be? ";
   cin >> size;

   arrayPtr = new int*[size];

   for (int count = size; count > 0; count--)
   {
      *arrayPtr = createNew(count);
      arrayPtr++;
   }
   arrayPtr -= size;
   tempPtr = *(arrayPtr + 1);

   //fill the first array with value 1.
   fillFirstArray(*arrayPtr, size);

   while (arrayPtr < arrayPtr + (size - 1))
   {
      fillArray(*arrayPtr, *(&tempPtr), size);
      arrayPtr++;
      tempPtr = *(arrayPtr + 1);
      size--;
   }
   cout << **arrayPtr << endl;
   delete [] *arrayPtr, arrayPtr;
   arrayPtr = NULL;
   system ("pause");
   return 0;
}

int *createNew(int size)
{
   int   *linePtr;    //pointer to an array of integers, size = number of elements.

   linePtr = new int[size];
   return(linePtr);
}

void fillFirstArray(int *firstArray, int num)
{
   for(int c = 0; c < num; c++)
   {
      firstArray[c] = 1;
   }
}


void fillArray(int *array, int *array2, int inputSize)
{
   for (int teller = 0; teller < inputSize; teller++)
   {
      if (teller == 0)
      {
         array2[teller] = 1;
      }
      else
      {
         array2[teller] = array[teller] + array2[teller - 1];
      }

   }
   printTriangle(array, inputSize);                                                    
}

void printTriangle (int *printArray, int printSize)
{
   for (int count = 0; count < printSize; count++)
   {
      cout << setw(5) << printArray[count] << setw(5);
   }
   cout << endl;
}
Last edited on
I see where I go wrong....

array++; //pointing to the second array.

The thing is, I don't know how to make the pointer point to the second piece of allocated memory in line 34.

Thanks!

Cheers
pinoynl
It's because you created the pointer locally, then returned it. At the end of the function the memory is destroyed, leaving the address returned to point to nothing. You have two options:
Pass in the pointer as an argument. Then edit it. (Pass by reference of course) Don't return anything.
Set the pointer to static. (I don't know if this will actually work. But it should afaik.)

Nvm. That's new-d memory, it shouldn't deallocate automatically.
Honestly, the easiest way to work around this IMO is to use vectors. Vectors would really save you some effort here.
Last edited on
Is it possible to dynamically create vectors?
I have already got the desired result, the code above has been changed to the correct one. I will still try to do it with vectors in stead, but not now, coz I still have a lot of homework to do :)

Greetings
pinoynl
Last edited on
Topic archived. No new replies allowed.