473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Spacing the output of Pascal's Triangle

Sorry for the inconvienience but i am expecting this pattern the pattern is in the attachment....i am just pasted my code and want alterations....please help
but i am ending up wid something like

1
1 1
1 2 1
1 3 3 1
with the following code
Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. void main()
  3. {
  4.   int n,y,x,c;
  5.   cin >> n;
  6.   for (y = 0; y < n; y++)
  7.     {
  8.      c = 1;
  9.      for (x = 0; x <= y; x++)
  10.      {
  11.         cout<<"  "<<c<< "   ";
  12.         c = c * (y - x) / (x + 1);
  13.      }
  14.      cout<<endl;
  15.   }
  16.   cout<<endl;
  17. }
  18.  
so please can someone help me out in spacing!
Attached Files
File Type: txt XYZ.txt (119 Bytes, 429 views)
Jul 23 '07 #1
8 4595
JosAH
11,448 Expert 8TB
Sorry for the inconvienience but i am expecting this pattern
1
1 1
1 2 1
1 3 3 1
but i am ending up wid something like

1
1 1
1 2 1
1 3 3 1
with the following code
which looks good to me (they're identical, so what's the problem?)

kind regards,

Jos
Jul 23 '07 #2
sicarie
4,677 Expert Mod 4TB
Please read these thoroughly.

Thanks

Update:: You have already been asked to read the Posting Guidelines throughly, so if you violate them again, I'm afraid I will have to issue you a temporary ban. If you want to post on the site, you have to follow the rules.
Jul 23 '07 #3
sicarie
4,677 Expert Mod 4TB
Check out the setw() command.
Jul 23 '07 #4
but where do i place the setw()
Jul 23 '07 #5
sicarie
4,677 Expert Mod 4TB
but where do i place the setw()
I'd start by putting it in front of your first 1. You're going to have to play around with it and figure it out for yourself. This might require a variable to keep track of what level of the triangle you are at, and probably figuring out a spacing in between each of the numbers on a line...
Jul 23 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
I'd start by putting it in front of your first 1. You're going to have to play around with it and figure it out for yourself. This might require a variable to keep track of what level of the triangle you are at, and probably figuring out a spacing in between each of the numbers on a line...
What sicarie says will work. I suggest you write your display down on paper so you can count the spaces you need for each line of Pascal's Triangle.
Jul 23 '07 #7
try this code:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. int main()
  5. {
  6.   double n;
  7.   cout << "Number: ";
  8.   cin >> n;
  9.   cin.ignore();
  10.   for (int y = 0; y < n; y++)
  11.   {
  12.     int c = 1;
  13.     cout << setw(n-y);
  14.  
  15.     for (int x = 0; x <= y; x++)
  16.     {
  17.       cout  << c << " ";
  18.       c = c * (y - x) / (x + 1);
  19.  
  20.     }
  21.     cout << "\n";
  22.   }
  23.   cin.get();
  24.   }
  25.  
Jul 28 '08 #8
Banfa
9,065 Expert Mod 8TB
Actually the problem is not quite as simple as it seems, the amount of spcing required is not only dependent on the current output line but the total number of lines that will be output.

For example it is fairly easy to see that the large the number of lines output the further over the 1 on the first line will have to be to leave room for the data on the following lines.
Jul 28 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Jerome Lyles | last post by:
I have a small training database: sql_tutorial. It works fine but the spacing between the output lines is too much. This is the way it looks when I copy and paste from the Konsole to this email:...
0
by: Statsstudent2006 | last post by:
Hello! I am a newbie, and have received my first VBA assignment. I have to write a subroutine that generates the Fibonacci sequence of numbers (the first n numbers, where n is an integer number...
25
by: GUPTAJI | last post by:
hi all, can u give me the code to create a Pascal's Triangle........... and yes, it should work thankx
5
by: singhm | last post by:
Hi guys so I have a trianlge program having hard time finishing this though, I have to develop a program which is the following: Write a program that will allow the user to enter the 3 lengths...
0
by: compiler | last post by:
hai, iam new to c,i need c-code for pascal triangle,could you please provide me the code. thanks and regards.
4
by: Aaron | last post by:
Hi, I have written a pascals triangle program. However, my program gets a floating point exception somewhere between 60 and 70 and any subsequent larger value. This is no doubt due to dividing...
19
by: lost1 | last post by:
Can someone point me in the right direction on how to get the triangle type to display. Below is a triangle class that is tested by another completely separate class. The main method of the test...
87
by: pereges | last post by:
I have a C program which I created on Windows machine. I have compiled and executed the program on windows machine and it gives me the consistent output every time i run it. for eg. input a = 2,...
2
by: J Ivan P Silvestre | last post by:
c #: Implementing the method Int TrianglePascal (int n) which returns the triangle to the level of Pascal N. public static int TrianglePascal(int n) { int arr = new int; for(int...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.