c++ query

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2007
Posts: 5
Reputation: raniajay is an unknown quantity at this point 
Solved Threads: 0
raniajay raniajay is offline Offline
Newbie Poster

c++ query

 
0
  #1
Dec 31st, 2007
i have tried to do the pascals law in the following method .....bt i m not getting a triangle shape.could someone help me out in this tight Spot?? here is the code what i have typed...
  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<iomanip.h>
  4. long TRIANGLE(int x,int y);
  5. int main()
  6. {
  7. clrscr();
  8. int lines=0,n;
  9. cout<<"enter how many lines";
  10. cin>>n;
  11. for(lines=0;lines<=n;lines++)
  12. {
  13. for(int i=0;i<lines;i++)
  14. for(int j=0;j<lines-n;j++)
  15. cout<<setw(2)<<" ";
  16. for(j=0;j<=i;j++)
  17. cout<<setw(4)<< TRIANGLE(i,j);
  18. cout<<endl;
  19. }
  20. getch();
  21. }
  22. long TRIANGLE(int x,int y)
  23. {if(x<0||y<0||y>x)
  24. return 0;
  25. long c=1;
  26. for (int i=1;i<=y;i++,x--)
  27. c=c*x/i;
  28. return c;
  29. }
Last edited by Ancient Dragon; Dec 31st, 2007 at 5:57 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: c++ query

 
0
  #2
Dec 31st, 2007
First, remember to post code surrounded by code tags to retain indentation to make the code easier to read.

Assuming you want the peak of the triangle centered over the base then I would would calculate the numbers and put them into a container printing the container once all calcualtions were done and not print the numbers to the screen on the fly. I'd consider using nested loops to control the display of the triangle using leading spaces followed by visible values followed by trailing spaces (though the trailing spaces are more conceptual than real).
How many spaces will be needed will depend on how many lines are displayed, how many characters will be needed in the display of the last line, and how many digits there are in the largest number contained within the triangle.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Solved Threads: 26
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: c++ query

 
0
  #3
Jan 1st, 2008
Hey raniajay, you'd be happy that C++ has provided standard libraries without the .h extension since long. You can simply write
  1. #include <iostream>
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC