S
S
Sliznyak2020-11-28 22:55:56
Pascal
Sliznyak, 2020-11-28 22:55:56

The program should swap the minimum and maximum elements of the matrix, but for some reason, most often, it changes the wrong elements. What is the reason for this?

Program Podprogrammi;
uses crt;
type masiv = array [1..20, 1..20] of integer;

var A,B,C:masiv;
    n,k:integer;
    irmax,icmax,irmin,icmin:integer;

procedure init(m: string; var mas:masiv);
begin
  writeln('Введите элементы массива ', m);
  for var i:=1 to n do
    for var j:=1 to k do begin
      write('[', i, ', ', j, '] = ');
      readln(mas[i, j]);
    end;
  writeln;
end;

procedure output(var mas:masiv);
begin
  for var i:=1 to n do begin
    for var j:=1 to k do
      write(mas[i, j]:5);
    writeln;
  end;
end;

procedure castling(var mas:masiv);
var max,min,g:integer;
    i,j:integer;
begin
  i:=1;
  j:=1;
  max:=0;
  min:=99999;
  for i:=1 to n do begin
    for j:=1 to k do
      if (mas[i, j]) > max then begin
        max:=(mas[i, j]);
        irmax:=i;
        icmax:=j;
      end;
      
      if (mas[i, j]) < min then begin
        min:=(mas[i, j]);
        irmin:=i;
        icmin:=j;
      end;
  end;
  g:=(mas[irmax, icmax]);
  mas[irmax,icmax]:=(mas[irmin, icmin]);
  mas[irmin,icmin]:=g;
  output(mas);
end;

begin
  repeat
    writeln('Введите кол-во строк в массивах (не больше 20)');
    read(n);
  until (n>0) and (n<21);

  repeat
    writeln('Введите кол-во столбцов в массивах (не больше 20)');
    read(k);
  until (k>0) and (k<21);

  init('A', A);
  init('B', B);
  init('C', C);
  clrscr;
  writeln('Массив A:');
  output(A);
  writeln('Массив B:');
  output(B);
  writeln('Массив C:');
  output(C);
  
  writeln('Новый массив A');
  castling(A);
  writeln('Новый массив B');
  castling(B);
  writeln('Новый массив C');
  castling(C);
  
end.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question