pascal triangel

please help me out...
i've make a pascal triangle, and its successfully run
but i want it to be a double sided triangle..
this is the code anyway
#include<iostream.h>

void main()
{
int i,x,row,column;
int a[100],b[100];
cout<<"Number:";
cin>>x;
for(i=0;i<=100;i++)
{
a[i]=0;
b[i]=0;
}
a[1]=1;
for(baris=0;row<=x;rows++)
{
for(i=1;i<=column;i++)
{
b[i]=a[i-1]+a[i];
cout<<b[i]<<"\t";
}
for(i=1;i<=column;i++)
{
a[i]=b[i];
}
cout<<endl;
column++;
}
}

thanks for any suggestion, and sorry cause my english is not good..
Last edited on
I have no idea what a "double-sided Pascal's triangle" is.
i mean its equilateral triangle hehehe
my program only make a right angled triangle pascal
so any suggest how to make an equilateral triangle pascal?
my program output is like this
1
1 1
1 2 1

what i wanted is i wanted the progam output is like this one
*****1
****1 1 1
***1 2 1 2 1
any suggest?
Last edited on
I'm sorry to inform you that Pascal's triangle is equilateral, and that what you're proposing not only is not a Pascal's triangle (and therefore can't be build using its set of rules) but it's also an isosceles triangle.
EDIT: By the way, we have [tt] tags:
1
111
12121
Last edited on
any idea what to change in my code?
cause its only shows
1
11
121
any idea?
Idea:
1
2
3
4
5
6
#include <iomanip>
//...
int padding( 3 );
cout << setw( padding++ ) << "1" << endl;
cout << setw( padding++ ) << "111" << endl;
cout << setw( padding++ ) << "12121" << endl;

Last edited on
if i use these code, i can't input...
thankyou, but i think i need another code..
thanks for your advice anyway...
sorry to tell you this but the out put is not a pascal triangle, numerically..

a pascal triangle would be:

.....1
...121
.12321
Last edited on
http://en.wikipedia.org/wiki/Pascal_triangle

You can also draw a pascal triangle like this:
1 1 1 1
1 2 3 4
1 3 6 10
1 4 10 20


Every number is the sum of the number above him and the number on the left of him. The numbers at the upper-row (/colum, i always confuse those two) and at the colum on the very left are all 1.

http://home.no.net/dubjai/win32cpptut/html/w32cpptut_01.html
>>intro to loops
This tutorial uses (besides other stuff) the pascal triangle to show how to use loops, you might want to read it.
Last edited on
It's interesting how all these people have no idea what the problem is, what it is about, or how to solve it, yet they suggest so-called solutions.
I specially like seymore's and hatrox's ideas. One missed the point completely, and the other doesn't know about the subject matter.

No, this isn't a problem of output formatting or an error in logic or a data representation issue. devaeron is trying to design some sort of Pascal's n triangle (I can't think of anything else to call it), where each number is the sum of the numbers right above it, above and to the left, and above and to the right. Although it seem he himself doesn't know what the algorithm is, because the result would never be

1
111
12121

It'd be

1
111
12321
1365631
14AEHEA41

This can be represented as a rectangular matrix of n,(n*2-1)

100000000
111000000
123210000
136563100
14AEHEA41

matrix[0][0]=1, and matrix[x][y]=matrix[x][y-1]+matrix[x-1][y-1]+matrix[x-2][y-1] (x should be checked to prevent negative indexation).

I hope we've all learnt something today, such as learning to understand a question before answering, and that a function needs to be defined before determining its output.


PS: by the way, Scipio, not only are you wasting half of the matrix (something that is impossible to prevent), but you're also wasting CPU by calculating unusable positions (notice that lines 5, 6, and 7 are incomplete).
Last edited on
First of all; i'm fiftheen years old and this above the niveau of math i have been teached on school so far. I dont know what a matrix is, and most of my information i get directly from wikipedia. So i dont pretend to know the answer, i only want to know what i'm doing wrong.

That being said: the pascal triangle as you show it above doesnt match with the one i know, or with the one that can be found on wikipedia (wich may not be the most trustable source, but wich normally is right about the most important matters). The difference: the way you calculate it, every number is the sum of the three numbers direct above it, while the way i know it should be the two numbers direct above it, wich would give someting like this:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1


This can also be presented as I do above, in a square.

PS: by the way, Scipio, not only are you wasting half of the matrix (something that is impossible to prevent), but you're also wasting CPU by calculating unusable positions (notice that lines 5, 6, and 7 are incomplete).


I dont understand what you mean by this. I havent presented any code, and if you would create a code with the algorithm i'm suggesting, i dont see wich unusable points it would calculate. Btw, what do you mean by the "AEHEA" in your post?
Last edited on
A = 0x0A (hexadecimal) = 10 decimal
E = 0x0E = 14 decimal
H = fictitious hex (if 0x0F is 15, then 0x0G = 16, and 0x0H = 17) referring to 17 decimal
Your answer is not exactly wrong. It's just wasteful. The most efficient was to represent a Pascal's triangle in a matrix (a matrix, by the way, is exactly what you wrote up there, even if yo didn't realize it) is this:
1000
1100
1210
1331
Notice that almost half of the space is not calculated, as opposed to
1111
1234
136A
14AK
Where the numbers in bold are unusable because they are only part of a line.
There even more efficient ways using more complex data structures, but this will do for low values of n.
Yes, I know that's not a Pascal's triangle. I said so in my first posts in this thread.
Sorry if I confused you. I thought you were talking about the memory representation of the triangle
Finally, the letters are numbers in base 36 (numerals 0-9 and letters A-Z)
A=10
B=11
...
I didn't want to mess up the formatting.
Last edited on
Thanks for the explanation jsmith and helios :).
I was wondering about the notation of numbers using chars already because of the names of memory locations. I get it now, thanks.

I however still dont see that my method is less efficient. Yes, it does calculate more in a shorter 'matrix' (right way to use that word?), but it also has more information in it.


1 1 1 1
1 2 3 4
1 3 6 A
1 4 A K

Two reach the 10's the way you're using you would need a bigger matrix:


1 0 0 0 0 0
1 1 0 0 0 0
1 2 1 0 0 0
1 3 3 1 0 0
1 4 6 4 1 0
1 5 A A 5 1


Where the numbers in bold are unusable because they are only part of a line.


Wy wouldnt i be able to use those values? I would think my method is more effecient, because every element presents an actuall value, while in your method half of the elements hold zero.

Sorry that i keep asking, but I really want to understand this. Again: I dont think my method actually is more efficient then yours, but i dont understand wy it is not.

EDIT: sorry, i see the problem. If you want to present the numbers as a triangle, you need to whole line, while i'm only calculating a part of it. So you cant use the numbers.
The reason i didnt saw this directly is because the first time I saw the pascal triangle it was presented in a square (wich can be really intresting to, eg if you draw a "*" everywhere the number is not even, and just " " if the number is even). I get it now, thanks for your explanations.
Last edited on
I ran the program posted (after fixing the typos on line 15 and making main return an int) and got this output:

Number:4
1
1 1
1 2 1
1 3 3 1

I'd say the question was probably about formatting.

After rereading the first posts, I think you might actually be right.
Well, this is what happens when one asks good questions stupidly.
Well, it can be fun to :)

Because of all this i got curious about the Pascal triangle, and i chanced some stuff in code i already got. Just to show the 'square-presentation' can be fun to: run this "full screen" (i mean clicking on full screen in the upper right of the console, not selecting "full screen" at the options), press enter after each drawing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>

using namespace std;

const int xsize=65;
const int ysize=65;
int test=2;

int main(){
    
    //int array to hold values
    int value[ysize][xsize];
    
    //upper and most left are all one
    for (int i=0;i<ysize;i++){
        value[i][0]=1;
        }
    for (int i=0;i<xsize;i++){
        value[0][i]=1;
        }
        
    //determinate all other values
    for (int i=1;i<ysize;i++){
        for (int j=1;j<xsize;j++){
            value[i][j]=value[i-1][j]+value[i][j-1];
            }
        }
    
    while (test<=64){

    system("cls");
    //draw triangle: "*" if true, " " if false
    for (int i=0;i<ysize;i++){
        for (int j=0;j<xsize;j++){
            if (!(value[i][j]%test==0)) cout<<"*";
            else cout<<" ";
            }

        cout<<endl;
        }
   
    //pause the program
    cin.ignore();
    test*=2;
    } 
    
    return 0;
}
Here's one that works for small triangles (up to 13 rows).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int i,x,row,column;
    int a[100],b[100];
    cout<<"Number:";
    cin>>x;
    for(i=0;i<=100;i++)
    {
        a[i]=0;
        b[i]=0;
    }
    a[1]=1;
    for(row=0;row<=x;row++)
    {
        for(i=x-row;i>0;--i) cout<<"  ";
        for(i=1;i<=column;i++)
        {
            b[i]=a[i-1]+a[i];
            cout<<setw(4);
            cout<<b[i];
        }
        for(i=1;i<=column;i++)
        {
            a[i]=b[i];
        }
        cout<<endl;
        column++;
    }
    return 0;
}
Thanks a lot, the codes are really worked..
Topic archived. No new replies allowed.