I just write a little prog to print YangHui triangle like this:
This is my prog code:Code:1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
This little prog spent me one night and this morning time,Code:#include <iostream> #include <stdio.h> using namespace std; // define function prototype void yhtriangle(int& x); int main() { int lines; cout<<"This is a little prog to print YangHui triangle ."<<endl; cout<<"Please enter the lines what you want to print: "<<endl; cin>>lines; yhtriangle(lines); cout<<endl<<endl<<endl; system("pause"); return 0; } void yhtriangle(int& x) { int *yanghui; int *temp; //to save operation value. int n; int blank; //counter the space numbers int counter; //cin>>x; // I decide to input x from main() //make two dynamic arrays yanghui = new int[x]; temp = new int[x]; //initialize arrayes for(n = 0; n < x; n++) { yanghui[n] = 0; temp[n] = 0; } yanghui[0] = 1; temp[0] = 1; blank = x; //circulate print triangle for(counter = 0; counter < x; counter++) { for(n = 0; n < blank; n++) cout<<" "; for(n = 0; n < counter; n++) { yanghui[n+1]=temp[n+1]+temp[n]; cout<<yanghui[n]<<" "<<flush; temp[n] = yanghui[n]; } cout<<endl; blank--; } delete new int[x]; //release dynamic array's memory. }
I was confused by defining dynamic array,
althoght it's running , but print graph is ugly,
please give some suggestion, how to change this statement:
Thanks!cout<<yanghui[n]<<" "<<flush;