Recent

Author Topic: [SOLVED] Pascal's triangle- alignment of values  (Read 2264 times)

qwapp

  • Newbie
  • Posts: 6
[SOLVED] Pascal's triangle- alignment of values
« on: March 22, 2018, 01:00:36 pm »
(I've posted on "General" but just found out about this beginer board and  I will repost here aswell)

So I've come up with the code to the values of the triangle itself. What I'm currently strugling is how to aligne/center the values that are printed. I tried many things but, I could use some help now. If anyone has an idea how this can be done feel free to share! Thank you

Code: Pascal  [Select][+][-]
  1. Program Tri_pas;
  2. Uses Crt;
  3. Var
  4.     linha,ordem,a,b: byte;  
  5.  
  6.  
  7. Function fat(X: byte): real; // factorial
  8. Var fat1:  real;
  9. Begin
  10.  
  11.   fat1:=1;  
  12.   If X <= 1 Then  
  13.     fat:=1
  14.   Else
  15.   Begin
  16.     Repeat
  17.       fat1:=(fat1 * X);
  18.       X:=(X - 1);
  19.     Until X <= 1;
  20.     fat:=fat1;
  21.   End;
  22.  
  23. End;
  24.  
  25.  
  26.  
  27. Procedure nCp(n,p: byte);  //Combinations
  28. Var
  29.     i: byte;
  30.     nCp: real;
  31. Begin
  32.  
  33.   If n < 1 Then
  34.     n:=0  
  35.   Else
  36.     n:=(n-1);
  37.    
  38.   For i:=0 to n do    
  39.   Begin
  40.     writeln;
  41.     For p:=0 to i do  
  42.     Begin
  43.       nCp:= fat(i) / (fat(p) * fat(i - p));    //  mathematic formula for the combinations      
  44.       Write(nCp:1:0,' ');
  45.     End;
  46.   End;
  47.  
  48. End;
  49.  
  50.  
  51. { Main Programa  }
  52.  
  53. Begin
  54.  
  55.   Write('Insert a line(1 -> n) :  ');
  56.   Readln(linha);
  57.   nCp(linha,ordem);
  58.   readln;  
  59.  
  60. End.
  61.  
  62.  

I think I could convert the Real values of the triangle into String and try to position them where I want using some functions like "Lenght", so I tried "FloatToStr" function for this and other ones, but It doesn't seem to work..
« Last Edit: March 23, 2018, 01:36:12 pm by qwapp »

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Pascal's triangle- alignment of values
« Reply #1 on: March 22, 2018, 01:25:48 pm »
Please don't do double posting. The more you do it the more likely others will ignore you.

The original post is here:
https://forum.lazarus.freepascal.org/index.php/topic,40572

qwapp

  • Newbie
  • Posts: 6
Re: Pascal's triangle- alignment of values
« Reply #2 on: March 23, 2018, 10:54:50 am »
Ok, I will know now,  I just thought it would  be more appropriated to post this here (but I really got many helpfull responses on the original one, from you inclusively, thanks! ).

Is it possible to delete it once posted, or you already took care of it?
« Last Edit: March 23, 2018, 11:06:49 am by qwapp »

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Pascal's triangle- alignment of values
« Reply #3 on: March 23, 2018, 11:19:49 am »
If you post in wrong category, you can ask admin to help you to move it to the correct place. You can delete your own posts but you can't delete the first post you started in a new thread, only admin can delete that post. But I think it's okay, small mistakes won't cause any harm.

qwapp

  • Newbie
  • Posts: 6
Re: Pascal's triangle- alignment of values
« Reply #4 on: March 23, 2018, 01:35:51 pm »
I see, thank you.

 

TinyPortal © 2005-2018