Advertisement
YellowAfterlife

A Pascal matrix (2d array) program. Now with ducks.

Nov 27th, 2012
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.14 KB | None | 0 0
  1. {
  2. 1. Calculate sums of non-negative columns
  3. 2. Find the smallest sum of absolute values in diagonals, parallel to topright-bottomleft diag.
  4. }
  5. program duckSum;
  6. uses crt{, ducks};
  7. type duck = integer;
  8. const duckLimit = 10;
  9.       noDucks = 0;
  10.       duckParadox = -1;
  11. var ducks: array[1..duckLimit, 1..duckLimit] of duck;
  12.     i, j: integer;
  13.     ducksFound, ducksMin, duckCount: duck;
  14. begin
  15.     clrScr;
  16.     { let user input matrix: }
  17.     write('What would be your duckrix size?: '); readLn(duckCount);
  18.     writeLn('Your people demand ducks.');
  19.     writeLn('Do not disappoint them.');
  20.     writeLn('Input starts now:');
  21.     for j := 1 to duckCount do
  22.     for i := 1 to duckCount do begin
  23.         { +4 is because of text above }
  24.         gotoXY((i - 1) * 8 + 1, j + 4);
  25.         readLn(ducks[i, j]);
  26.     end;
  27.     { calculate sum of elements in non-negative columns: }
  28.     for i := 1 to duckCount do begin
  29.         ducksFound := noDucks;
  30.         for j := 1 to duckCount do begin
  31.             if ducks[i, j] < noDucks then begin
  32.                 ducksFound := duckParadox;
  33.                 break;
  34.             end else ducksFound := ducksFound + ducks[i, j];
  35.         end;
  36.         if ducksFound <> duckParadox then
  37.         writeLn('Ducks in column ', i, ': ', ducksFound);
  38.     end;
  39.     { find minimum sum of elements in diagonals, parallel to main '\' diagonal }
  40.     ducksMin := duckParadox;
  41.     { above the main diag: }
  42.     for i := 2 to duckCount do begin
  43.         { find sum of elements in diag: }
  44.         ducksFound := noDucks;
  45.         for j := 0 to duckCount - i do
  46.         ducksFound := abs(ducksFound + ducks[i + j, 1 + j]);
  47.         { minimum? }
  48.         if (ducksFound < ducksMin) or (ducksMin = duckParadox) then ducksMin := ducksFound;
  49.     end;
  50.     { below the main diag: }
  51.     for i := 2 to duckCount do begin
  52.         { find sum of elements in diag: }
  53.         ducksFound := noDucks;
  54.         for j := 0 to duckCount - i do
  55.         ducksFound := abs(ducksFound + ducks[1 + j, i + j]);
  56.         { minimum? }
  57.         if (ducksFound < ducksMin) or (ducksMin = duckParadox) then ducksMin := ducksFound;
  58.     end;
  59.     writeLn('Minimum of ducks in diagonal, parallel to "\" ', ducksMin);
  60.     readln;
  61. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement