Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program Lab2_1;
- Var
- Arr: Array Of Real;
- I, K, LengthArr, J: Integer;
- NearestNum, SetValue, Buffer: Real;
- IsCorrect: Boolean;
- Begin
- Writeln('The program finds the number of the nearest member of the sequence to a given number.');
- Write('Enter length of subsequence: ');
- Repeat
- IsCorrect := True;
- Try
- Read(LengthArr);
- Except
- Write('Symbols have been entered or exceeding permissible limits. Enter a valid value: ');
- IsCorrect := False;
- End;
- If (IsCorrect) And (LengthArr < 2) Then
- Begin
- IsCorrect := False;
- Write('A number less than two was entered. Enter a valid value: ');
- End;
- Until IsCorrect;
- SetLength(Arr, LengthArr);
- Write('Enter elements of subsequence: ');
- For I := Low(Arr) To High(Arr) Do
- Begin
- Repeat
- IsCorrect := True;
- Try
- Read(Arr[I]);
- Except
- Write('Symbols have been entered. Enter the number: ');
- IsCorrect := False;
- End;
- If (IsCorrect) And ((Arr[I] > 2000000000) Or (Arr[I] < -2000000000)) Then
- Begin
- IsCorrect := False;
- Write('Exceeding permissible limits. Enter a valid value: ');
- End;
- Until IsCorrect;
- End;
- For I := 0 To LengthArr - 2 Do
- For J := I + 1 To LengthArr - 1 Do
- If Arr[I] > Arr[J] Then
- Begin
- Buffer := Arr[I];
- Arr[I] := Arr[J];
- Arr[J] := Buffer;
- End;
- For I := Low(Arr) To High(Arr) Do
- Write(Arr[I], ' ');
- Write(#13, 'Enter real value: ');
- Repeat
- IsCorrect := True;
- Try
- Read(SetValue);
- Except
- Write('Symbols have been entered. Enter the number: ');
- IsCorrect := False;
- End;
- If (IsCorrect) And (SetValue < -2000000000) Or (SetValue > 2000000000) Then
- Begin
- IsCorrect := False;
- Write('Exceeding permissible limits. Enter a valid value: ');
- End;
- Until IsCorrect;
- If SetValue < Arr[Low(Arr)] Then
- Write('The entered value is less than the smallest term of the sequence.')
- Else
- Begin
- NearestNum := Arr[Low(Arr)];
- I := Low(Arr);
- While SetValue > Arr[I] do
- Begin
- K := I;
- Inc(I);
- End;
- NearestNum := Arr[K];
- Repeat
- IsCorrect := True;
- If NearestNum < Arr[K + 1] Then
- Write('The nearest element of the sequence numbered ', k + 1)
- Else
- Begin
- IsCorrect := False;
- Inc(K);
- End;
- Until IsCorrect;
- End;
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement