This program generates lines of pascal's triangle, numbers after 23 generate weird output.
Pascal Triangle Generator
#include <iostream>
#include <stdlib.h>
using namespace std;
string Separator(" ");
float factorial (float n)
{
if (n == 0.0f)
return 1.0f;
else if (n != 1.0f)
return n*factorial (n-1.0f);
else
return n;
}
float combination (float n,float r)
{
return factorial(r)/((factorial(r-n)*factorial(n)));
}
void printRow(float r)
{
for (float i=0.0f; i<=r; i+=1.0f){
cout << combination (i,r) << Separator;
}
}
void printRows(float r)
{
for (float j=0.0f; j<r; j+=1.0f){
printRow(j);
cout << endl;
}
}
int main(int argc, char** argv)
{
float nRows = 10.0f;
cout << "Hello, This is me Pascal. I'll give you ";
if (argv [1] == 0)
cout << "10 Lines of my famous triangle";
else if (atof (argv [1]) <= 0.0f)
cout << "10 Lines of my famous triangle";
else {
nRows = atof(argv [1]);
cout << argv [1] << (nRows > 1? " Lines " : " Line ") << "of my famous triangle.\n";
}
printRows(nRows);
}
technogeek_42 -20 Posting Whiz in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.