0

I've written a programm to solve the integral X^2+2 by rectangles method

program fourrr;

var    a, b : real; { borders }
       h : real; { increment argument }
       s : real; { approximate value of the integral }
       n : integer; { number of intervals }
       x : real; { argument }
       y : real; { function value at the beginning of interval }
       i : integer;
begin

    write('lower border a: '); read(a);
    write('upper border b: '); read(b);
    write('increment argument h: '); read(h);

     n := (b - a) / (h + 1);
     x := a;
     s := 0;

    i := 1;
    while i <= n do
      begin
        y := x * x + 2;   // function value at the beginning of interval
        s := s + (y * h);
        x := x + h;
      end;

   writeln('Integral value: ', s); 
end.

but i cannot solve the problem with data types conversion when I run it.

So please help me it's very important. Thanks

Luchnik
  • 1,067
  • 4
  • 13
  • 31

1 Answers1

0

(Since the beginning, sorry for my bad english)

At the beginning, you need the input output: program fourrr(input,output);

I see that you done n := (b - a) / (h + 1); And that's not / , you should use " div " : n := (b - a) div (h + 1);

I don't know why did you use the loop ,and I tell you that I don't know how to solve that integral,but, if it's as you did, you don't need it. What you could change in the loop? Using the IF.

{ If i<=n THEN Begin y:=x * x + 2; // function value at the beginning of interval s := s + (y * h); x := x + h; end (*without the ; simbol*) Else writeln('ERROR'); (*The ELSE only read for him the writeln('error'), if you want to use more things , use the ELSE begin h:= ... ; alpha:=... If ...(<-for example) end;*)}

  • "At the beginning" is wrong; the program prompts the user for each input with `write`, and then reads the response from the console. You don't need any `program fourrr(input, output)`, because it's not getting the values as command line parameters. – Ken White Apr 10 '14 at 22:18
  • Your advice: _And that's not / , you should use " div " : n := (b - a) div (h + 1);_ is wrong (even worse) and will produce another error because you **cannot** use `div` with floating point data types. Correct would be `n := round((b - a) / (h + 1));` – gammatester Apr 11 '14 at 07:17