r4lovets

Ищем локальные максимумы массива

Nov 21st, 2018
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.03 KB | None | 0 0
  1. program LocalMaximums;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8. const
  9.   N = 24;
  10.  
  11. type
  12.   TArray = array[1..N] of Integer;
  13.  
  14. procedure InputOfArray(var X: TArray);
  15.  
  16. var
  17.   i: Integer;
  18.  
  19. begin
  20.   for i := 1 to N do
  21.     X[i] := Random(100) - 50;
  22. end;
  23.  
  24. procedure OutputOfArray(var X: TArray);
  25.  
  26. var
  27.   i: Integer;
  28.  
  29. begin
  30.   Writeln('Initial array: ');
  31.   for i := 1 to N do
  32.     Write(X[i], ' ');
  33.   Writeln; Writeln;
  34. end;
  35.  
  36. function MaximumOfArray(X: TArray): Integer;
  37.  
  38. var
  39.   i, Max: Integer;
  40.  
  41. begin
  42.   Max := X[1];
  43.   for i := 2 to N do
  44.     if X[i] > Max then
  45.       Max := X[i];
  46.   MaximumOfArray := Max;
  47. end;
  48.  
  49. procedure OutputOfLocalMaximums(X: TArray);
  50.  
  51. var
  52.   i, Max: Integer;
  53.  
  54. begin
  55.   Writeln('Local maximum indexes: ');
  56.   Max := MaximumOfArray(X);
  57.   for i := 2 to N - 1 do
  58.     begin
  59.       if (X[i] <> Max) and ((X[i] > X[i - 1]) and (X[i] > X[i + 1])) then
  60.         write(i, ' ');
  61.     end;
  62. end;
  63.  
  64. var
  65.   D: TArray;
  66.  
  67. begin
  68.   InputOfArray(D);
  69.   OutputOfArray(D);
  70.   OutputOfLocalMaximums(D);
  71.  
  72.   ReadLn;
  73. end.
Add Comment
Please, Sign In to add comment