Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- 1. Calculate sums of non-negative columns
- 2. Find the smallest sum of absolute values in diagonals, parallel to topright-bottomleft diag.
- }
- program duckSum;
- uses crt{, ducks};
- type duck = integer;
- const duckLimit = 10;
- noDucks = 0;
- duckParadox = -1;
- var ducks: array[1..duckLimit, 1..duckLimit] of duck;
- i, j: integer;
- ducksFound, ducksMin, duckCount: duck;
- begin
- clrScr;
- { let user input matrix: }
- write('What would be your duckrix size?: '); readLn(duckCount);
- writeLn('Your people demand ducks.');
- writeLn('Do not disappoint them.');
- writeLn('Input starts now:');
- for j := 1 to duckCount do
- for i := 1 to duckCount do begin
- { +4 is because of text above }
- gotoXY((i - 1) * 8 + 1, j + 4);
- readLn(ducks[i, j]);
- end;
- { calculate sum of elements in non-negative columns: }
- for i := 1 to duckCount do begin
- ducksFound := noDucks;
- for j := 1 to duckCount do begin
- if ducks[i, j] < noDucks then begin
- ducksFound := duckParadox;
- break;
- end else ducksFound := ducksFound + ducks[i, j];
- end;
- if ducksFound <> duckParadox then
- writeLn('Ducks in column ', i, ': ', ducksFound);
- end;
- { find minimum sum of elements in diagonals, parallel to main '\' diagonal }
- ducksMin := duckParadox;
- { above the main diag: }
- for i := 2 to duckCount do begin
- { find sum of elements in diag: }
- ducksFound := noDucks;
- for j := 0 to duckCount - i do
- ducksFound := abs(ducksFound + ducks[i + j, 1 + j]);
- { minimum? }
- if (ducksFound < ducksMin) or (ducksMin = duckParadox) then ducksMin := ducksFound;
- end;
- { below the main diag: }
- for i := 2 to duckCount do begin
- { find sum of elements in diag: }
- ducksFound := noDucks;
- for j := 0 to duckCount - i do
- ducksFound := abs(ducksFound + ducks[1 + j, i + j]);
- { minimum? }
- if (ducksFound < ducksMin) or (ducksMin = duckParadox) then ducksMin := ducksFound;
- end;
- writeLn('Minimum of ducks in diagonal, parallel to "\" ', ducksMin);
- readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement