GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Feb-2006, 13:54
worms707 worms707 is offline
New Member
 
Join Date: Jan 2006
Posts: 6
worms707 is on a distinguished road

Reversing Output, Pascals triangle


i have the following C++ code:
CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
using namespace std;

int pascalTri(int rows, int columns);

int main()
{
   int rows, i =1;

   cout<<"Enter number of rows you want displayed"<<endl;
   cin>>rows;

   if (rows <= 0)
       cout << "The number of rows must be a non-negative number" << endl;

   for (rows ; i <= rows; rows--)
   {
       for(int columns=1; columns <= rows; columns++)
           {
               for(rows; columns <= rows; columns++)
                   cout <<setw(6)<< pascalTri(rows, columns);
           }
       cout<<endl;
   }

   system ("pause");
   return 0 ;
}


int pascalTri(int rows, int columns)
{
   if ((columns == 1) || (columns == rows))
       return 1;

       return (pascalTri(rows-1, columns-1) + pascalTri(rows-1, columns));
}


the output is: (if the input is "4")
1 3 3 1
1 2 1
1 1
1

I want the output to be:
1
1 1
1 2 1
1 3 3 1.....

I used microsoft visual c++

There are no errors.

Can you help me please?

Thanks.
  #2  
Old 07-Feb-2006, 18:55
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Reversing Output, Pascals triangle


Hi worms,

Welcome to the GID Forums.

First, your loop should be changed. It is doing unnecessary things.
For example,
CPP / C++ / C Code:
   for (rows ; i <= rows; rows--)
   {
       for(int columns=1; columns <= rows; columns++)
           {
               for(rows; columns <= rows; columns++)
                   cout <<setw(6)<< pascalTri(rows, columns);
           }
       cout<<endl;
   }

Is equivalent to this:
CPP / C++ / C Code:
   for (; rows >= 0; rows--)
   {
       for(int columns=1; columns <= rows; columns++)
           {
                   cout <<setw(6)<< pascalTri(rows, columns);
           }
       cout<<endl;
   }


Do you know why?

Next, why does it print from the back?
Because you loop the 'rows' in descending order.
i.e Instead of looping from rows to 0, loop from 0 to rows.
Then it will print correctly.

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache / PHP problem, maybe output length? HaganeNoKokoro Apache Web Server Forum 3 07-Aug-2008 05:42
editbox output problem FireFox8173 MS Visual C++ / MFC Forum 1 01-Apr-2005 15:15
using vector or array oshiotse C++ Forum 4 16-Apr-2004 11:59
urgent: problem + output filters jack Apache Web Server Forum 0 04-Feb-2004 23:32
articles -> output Allowee Learning Journal by J de Silva 1 05-Jun-2003 20:47

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:31.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.