Program MC;

Uses
        Crt;

Const
        A = - 0.5;
        B = Pi;
        N = 1000;
Var
           I , J : Extended;
       Max , Min : Extended;
          X , dx : Extended;
    Nn , Counter : LongInt;
        Integral : Extended;
Function Ex ( X : Extended ) : Extended;
 Begin
  Ex := Sin ( X ) * Sin( X ) + Cos ( X )
 End;

Function Sign ( X : Extended ) : Byte;
 Begin
  Sign := 0;
   If X <> 0 then Sign :=  Round ( X / Abs ( X ) );
 End;

Procedure Make_dx;
 Begin
  Dx := Abs ( (  Ex ( B ) - Ex ( A )  ) / N )
 End;

Procedure GetExt;
var sx : extended;
 Begin
  WriteLn('Finding extremal values ...');
   X := A;
   Max := Ex ( B ); Min := Ex ( A );
    Repeat
      X := X + dx;
       If Ex ( X ) > Max then Max := Ex ( X );
       If Ex ( X ) < Min then begin sx:=x; Min := Ex ( X ); end;
    Until X >= B;
  Sound ( 1000 );
  WriteLn (' Extremal values is :');
  WriteLn( 'Max : ', Max );
  WriteLn(sx);
  WriteLn( 'Min : ', Min );
  Delay ( 200 );
  NoSound;
   End;

Procedure CMC;
 Begin
  J := Abs ( Max - Min );
  I := Abs ( B - A) ;
 End;

Procedure FNI;
  Var
    Rx , Ry : Extended;
 Begin
 Counter := 0;
 Randomize;
  For Nn := 1 to 10 * N do
   begin
    Write( #13 );
    Write( Nn );
       Rx := A + Random * I ;
       Ry := Min + Random * J ;

      If ( Rx > 0 ) and ( Ex ( Rx ) > 0 ) then                      { I }
       If ( Ry > 0 ) and ( Ry < Ex ( Rx ) ) then Inc ( Counter );

      If ( Rx > 0 ) and ( Ex ( Rx ) < 0 ) then                      { IV }
       If ( Ry < 0 ) and ( Ry > Ex ( Rx ) ) then Dec ( Counter );

      If ( Rx < 0 ) and ( Ex ( Rx ) < 0 ) then                      { III }
       If ( Ry < 0 ) and ( Ry > Ex ( Rx ) ) then Inc ( Counter );

      If ( Rx < 0 ) and ( Ex ( Rx ) > 0 ) then                      { II }
       If ( Ry > 0 ) and ( Ry < Ex ( Rx ) ) then Dec ( Counter );

   end;
  Integral := ( Counter / (10 * N) ) * ( I * J );

 End;


Begin
ClrScr;
Make_dx;
GetExt;
CMC;
WriteLn;
WriteLn( N * 10 );
FNI;
Sound ( 30 );
Delay ( 200 );
NoSound;
WriteLn;
WriteLn ( 'Integral = ',Integral );
ReadKey;
End.