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;
}
|