Advertisement
LisunovaMaryna

delphi lab2_1

Oct 22nd, 2023
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.89 KB | None | 0 0
  1. Program Lab2_1;
  2.  
  3. Var
  4.     Arr: Array Of Real;
  5.     I, K, LengthArr, J: Integer;
  6.     NearestNum, SetValue, Buffer: Real;
  7.     IsCorrect: Boolean;
  8.  
  9. Begin
  10.     Writeln('The program finds the number of the nearest member of the sequence to a given number.');
  11.     Write('Enter length of subsequence: ');
  12.     Repeat
  13.         IsCorrect := True;
  14.         Try
  15.             Read(LengthArr);
  16.         Except
  17.             Write('Symbols have been entered or exceeding permissible limits. Enter a valid value: ');
  18.             IsCorrect := False;
  19.         End;
  20.         If (IsCorrect) And (LengthArr < 2) Then
  21.         Begin
  22.             IsCorrect := False;
  23.             Write('A number less than two was entered. Enter a valid value: ');
  24.         End;
  25.     Until IsCorrect;
  26.     SetLength(Arr, LengthArr);
  27.    
  28.     Write('Enter elements of subsequence: ');
  29.     For I := Low(Arr) To High(Arr) Do
  30.     Begin
  31.         Repeat
  32.             IsCorrect := True;
  33.             Try
  34.                 Read(Arr[I]);
  35.             Except
  36.                 Write('Symbols have been entered. Enter the number: ');
  37.                 IsCorrect := False;
  38.             End;
  39.             If (IsCorrect) And ((Arr[I] > 2000000000) Or (Arr[I] < -2000000000)) Then
  40.             Begin
  41.                 IsCorrect := False;
  42.                 Write('Exceeding permissible limits. Enter a valid value: ');
  43.             End;
  44.         Until IsCorrect;
  45.     End;
  46.    
  47.     For I := 0 To LengthArr - 2 Do
  48.         For J := I + 1 To LengthArr - 1 Do
  49.             If Arr[I] > Arr[J] Then
  50.             Begin
  51.                 Buffer := Arr[I];
  52.                 Arr[I] := Arr[J];
  53.                 Arr[J] := Buffer;
  54.             End;
  55.    
  56.     For I := Low(Arr) To High(Arr) Do
  57.         Write(Arr[I], ' ');
  58.    
  59.     Write(#13, 'Enter real value: ');
  60.     Repeat
  61.         IsCorrect := True;
  62.         Try
  63.             Read(SetValue);
  64.         Except
  65.             Write('Symbols have been entered. Enter the number: ');
  66.             IsCorrect := False;
  67.         End;
  68.         If (IsCorrect) And (SetValue < -2000000000) Or (SetValue > 2000000000) Then
  69.         Begin
  70.             IsCorrect := False;
  71.             Write('Exceeding permissible limits. Enter a valid value: ');
  72.         End;
  73.     Until IsCorrect;
  74.    
  75.     If SetValue < Arr[Low(Arr)] Then
  76.         Write('The entered value is less than the smallest term of the sequence.')
  77.     Else
  78.     Begin
  79.         NearestNum := Arr[Low(Arr)];
  80.         I := Low(Arr);
  81.         While SetValue > Arr[I] do
  82.         Begin
  83.             K := I;
  84.             Inc(I);
  85.         End;
  86.         NearestNum := Arr[K];
  87.        
  88.         Repeat
  89.             IsCorrect := True;
  90.             If NearestNum < Arr[K + 1] Then
  91.                 Write('The nearest element of the sequence numbered ', k + 1)
  92.             Else
  93.             Begin
  94.                 IsCorrect := False;
  95.                 Inc(K);
  96.             End;
  97.         Until IsCorrect;
  98.     End;
  99. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement