Since I haven't learned enough C++, and ain't intelligent enough either, I want you to have a look at this:
||||||||||||||||||1
|||||||||||||||||1 1
||||||||||||||||1 2 1
|||||||||||||||1 3 3 1
||||||||||||||1 4 6 4 1
|||||||||||||1 5 10 10 5 1
||||||||||||1 6 15 20 15 6 1
Get it? This is called Pascal's triangle. I want an algorithm in C++ which writes a specific number of lines of this triangle you enter.
This is what I have come up with this far:
int main()
{
int lines;
std::cout << "Type in the amount of lines of Pascal's triangle you want to be shown: ";
std::cin >> lines;
char space[] = " ";
for(int i=0; i=>lines/2; i++)
{
std::cout << space;
}
/*this loop creates the amount of space needed, so that the base of the triangle will get right. Else it might look something like:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
and so on.....*/
After that the space is created, I want the program to start writing the actual triangle, and this is why I'm asking you for help!
If you already haven't noticed, the syntax of the triangle is that a number in it, is the result of the two numbers above it:
1 2 1 - has:
1 3(1+2) 3(2+1) 1 - below. And they have:
1 4(3+1) 6(3+3) 4(3+1) 1 - below. And so on....... hope you've got it!
system("PAUSE");
return 0;
}



LinkBack URL
About LinkBacks


