Recent

Author Topic: [ASK]  (Read 1302 times)

jeffsamuel

  • Newbie
  • Posts: 1
[ASK]
« on: October 14, 2018, 05:06:32 pm »
This is my code.

program Soal03;


var
output:real;
a,b,n:real;
c,d,e,f,g:real;

function lebar(j,i,k: real): real;
begin
   lebar:=(i-j)/k;
end;

function fungsi(x: real): real;
begin
   fungsi:= (x)/sqrt((x*x)- 2*x + 5);
end;



begin
   write('Masukkan a: ');
   readln(a);
   write('Masukkan b: ');
   readln(b);
   write('Masukkan N: ');
   readln(n);
c:=lebar(a,b,n);

d:=0;
   f:=0;
   g:=0;
   while (d<>b) do
   begin
      d:=d+c;
      e:=d*fungsi(f);
      f:=f+c;
      g:=g+e;
   end;   
output:=g;
write('luas kurva adalah ',output:0:4);
end.

it should have an output but when compiled, it doesnt give any output. What did i do wrong?

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: [ASK]
« Reply #1 on: October 14, 2018, 05:17:01 pm »
Here your program falls to endless loop:
Code: Pascal  [Select][+][-]
  1. ...
  2.   while (d<>b) do
  3.    begin
  4.       d:=d+c;
  5.       e:=d*fungsi(f);
  6.       f:=f+c;
  7.       g:=g+e;
  8.    end;
  9. ...


EDIT: And because both b and d are real, it is in general bad idea to compare them (it will fail in cases like b=0.9999999999 and d=1.00000001).
« Last Edit: October 14, 2018, 05:25:00 pm by Blaazen »
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Thaddy

  • Hero Member
  • *****
  • Posts: 14159
  • Probably until I exterminate Putin.
Re: [ASK]
« Reply #2 on: October 14, 2018, 05:30:53 pm »
Apart from the fact that real is deprecated, we have this in math: https://www.freepascal.org/docs-html/rtl/math/samevalue.html
In your code it will pick the correct overload too.
Note we also have a real48 support unit for legacy code (or stubborn people).
Specialize a type, not a var.

 

TinyPortal © 2005-2018