YellowAfterlife

Pascal: sum of array cells behind maximum index

Nov 12th, 2012
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 0.66 KB | None | 0 0
  1. { ... find the sum of all elements behind (after) first maximum element. Display a message if there are none. }
  2. const n = 6;
  3. var m: array[1 .. n] of Integer;
  4.     i, mxIndex, mxValue, sum: Integer;
  5. begin
  6.     { input }
  7.     for i := 1 to n do begin
  8.         write('M[', i, '] = ');
  9.         readLn(M[i]);
  10.     end;
  11.     { find max }
  12.     mxIndex := 0;
  13.     mxValue := 0;
  14.     for i := 1 to n do begin
  15.         if (mxIndex = 0) or (M[i] > mxValue) then begin
  16.             mxIndex := i;
  17.             mxValue := M[i];
  18.         end;
  19.     end;
  20.     { sum-related }
  21.     if (mxIndex < n) then begin
  22.         sum := 0;
  23.         for i := mxIndex + 1 to n do sum := sum + M[i];
  24.         writeLn('Sum: ', sum);
  25.     end else writeLn('There are no elements behind the maximum');
  26. end.
Advertisement
Add Comment
Please, Sign In to add comment