YangHui triangle print test prog?

This is a discussion on YangHui triangle print test prog? within the C++ Programming forums, part of the General Programming Boards category; I just write a little prog to print YangHui triangle like this: Code: 1 1 1 1 2 1 1 ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    98

    YangHui triangle print test prog?

    I just write a little prog to print YangHui triangle like this:
    Code:
                         1
                       1   1
                     1   2   1
                  1   3    3   1
                1   4   6   4   1
    This is my prog code:
    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.
    
    }
    This little prog spent me one night and this morning time,
    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:
    cout<<yanghui[n]<<" "<<flush;
    Thanks!
    Last edited by toysoldier; 08-19-2004 at 12:08 AM.

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    Something wrong I have fond, this is edited code:
    Code:
    #include <iostream>
    #include <stdio.h>
    #include <iomanip.h>    //add this statement;
    
    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;
      cout<<"lines = "<<flush;
      cin>>lines;
      cout<<endl<<endl<<endl<<endl;
      
      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++)     //should be '<=';
        {
            yanghui[n+1]=temp[n+1]+temp[n];  
            cout<<setw(4)<<yanghui[n]<<" "<<flush;   //insert setw(4);
            temp[n] = yanghui[n];
        }
                    
        cout<<endl;
        blank--;
      }
      delete new int[x];   //release dynamic array's memory.
    
    }
    Last edited by toysoldier; 08-20-2004 at 07:58 AM.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    I using Thantos's suggestion to print result like this:
    Code:
                                     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 
                       1    7   21   35   35   21    7    1 
                     1    8   28   56   70   56   28    8    1 
                   1    9   36   84  126  126   84   36    9    1 
                 1   10   45  120  210  252  210  120   45   10    1 
               1   11   55  165  330  462  462  330  165   55   11    1 
             1   12   66  220  495  792  924  792  495  220   66   12    1 
           1   13   78  286  715 1287 1716 1716 1287  715  286   78   13    1 
         1   14   91  364 1001 2002 3003 3432 3003 2002 1001  364   91   14    1

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    It seems like there's a lesser slope on the right - why is that?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    > delete new int[x]; //release dynamic array's memory.
    Heh, this does nothing - except allocate and immediately free the memory.
    Meanwhile, your other allocations leak away.

    > #include <iomanip.h> //add this statement;
    Why the old-style?
    You're using namespaces.

    Oh, and code tags please...

    > It seems like there's a lesser slope on the right - why is that?
    Check the number of spaces used for indentation
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Useless Apprentice ryan_germain's Avatar
    Join Date
    Jun 2004
    Posts
    76
    Quote Originally Posted by sean_mackrory
    It seems like there's a lesser slope on the right - why is that?
    on the left...in between the 5 and the 10, the 6 and 15, 7 and 21, etc... there is a blank (space...whatever) missing

    edit: beaten
    There is not the slightest indication that [nuclear energy] will ever be obtainable. It would mean that the atom would have to be shattered at will.

    -Albert Einstein, 1932

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    delete new int[x];
    @ Salem :

    Please give some tip, how to correct it?
    Thanks.

  8. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    replace it with

    Code:
    delete temp;
    delete yanghui;

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    Or even

    Code:
    delete[] temp;
    delete[] yanghui;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why is my program freezing?
    By ShadowMetis in forum Windows Programming
    Replies: 8
    Last Post: 08-20-2004, 04:20 PM
  2. Question About Linker Errors In Dev-C++
    By ShadowMetis in forum C++ Programming
    Replies: 9
    Last Post: 08-18-2004, 09:42 PM
  3. Please test my calculator prog
    By Quantrizi in forum Game Programming
    Replies: 69
    Last Post: 10-08-2002, 06:00 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21