Hi there. I had to make a code to print out a Pascal Triangle. But I'm not getting it to display properly.
For example, if the user types in 4, then it will print:
1
1
1 2
1 3 3
But it should print
1
1 1
1 2 1
1 3 3 1
And the last thing I'm trying to figure out is how to make it show a sum of the numbers.
So it should add:
.. 1
.. 1 + 1
.. 1 + 2 + 1
+ 1 + 3 + 3 + 1
.. 15 <-----------and display this as the sum.
Any help is appreciated. Thank you!
#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <ctype.h> // I know some of these may be unnecessary
#include <string> // but our teacher wants us to include
#include <iomanip> // these in all our code.
#include <math.h>
using namespace std;
int sum, i, size;
void inputSize();
void sumNumbers();
void main(int size)
{
int sum=0;
cout<<"Pascal Triangle:\n";
inputSize();
}
void inputSize()
{
cout<< "Enter the amount of rows (between 1 and 9) you would like in the triangle."<<endl;
cin >>size;
cout<<"\n";
if (size > 0 && size <10)
{
int *rowA = new int[1];
rowA[0] = 1;
cout <<setw(6)<<rowA[0] <<endl;
for (int x = 1; x < size; x++)
{
int *rowB = new int[x+1];
rowB[0] = rowA[0];
for (int y = 1; y < x; y++)
rowB[y] = rowA[y-1] + rowA[y];
rowB[x] = rowA[x-1];
delete [] rowA;
rowA = rowB;
for (int y = 0; y < x; y++)
cout <<setw(6)<< rowB[y];
cout <<"\n";
}
delete [] rowA;
sumNumbers();
}
else
{ cout<< "Number is not between 1-9.\n"<<endl; }
}
void sumNumbers()
{
for (int x=0; x<size; x++)
{
for (int y=0; y<size; y++)
{
sum +=y; // This is totally wrong,
} // I can't figure out how to
cout<<sum<<"\n"<<endl; //make a sum of the numbers
}
}