M
M
Mehelborn2020-06-13 11:34:11
Pascal
Mehelborn, 2020-06-13 11:34:11

Two-dimensional array. Pascal. Where is the mistake?

Hello. It is necessary to find the maximum numbers in each column in a 2x6 matrix and find the minimum number among the found numbers. I found the maximum numbers, but I can't write them into an array. The minimum must be found in the main body of the program. Can't see the error. I am completely unfamiliar with pascal.

uses crt;
const X = 2;
const Y = 6;
Type matrix = array[1..X, 1..Y] of integer;
Type arr = array[1..Y] of integer;
var D: matrix; p, t: integer;
var g: arr; k, min: integer;

procedure print_mtx(const a: matrix);
var i, j: integer; begin
    for i:= 1 to X do begin
        for j:= 1 to Y do write(a[i, j]:5);
        writeln;
    end;
end;

procedure print_arr(const a: arr);
var i: integer; begin
    for i:= 1 to Y do write(a[i]:5);
end;

function trans(var a: matrix; b: arr): integer;
var i, j: integer; begin
    i:= 1;
    for j:= 1 to Y do begin
        if(a[i,j] >= a[i+1,j]) then begin
             b[j]:= a[i,j];
             write(a[i,j]:5);
        end
        else begin
             b[j]:= a[i+1,j];
             write(a[i+1,j]:5);
        end;
    end;
end;

begin
     clrscr;
     randomize;
     for p:= 1 to X do
         for t:= 1 to Y do D[p,t]:= random(50);
     print_mtx(D);
     writeln;
     trans(D, g);
     writeln;
     writeln;
     print_arr(g);
     min:= g[1];
     for k:= 1 to Y do if(g[k] <= min) then min:= g[k];
     writeln;
     writeln;
     write(min:5);
end.

5ee48fd708197734574583.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pashenka, 2020-06-13
@Mehelborn

I also absolutely do not know Pascal, the last experience I had with him was 13 years ago. But your problem was that you didn't modify the array g, that is, you didn't write anything to it. Therefore, there were only zeros.
I fixed it, it seems to work correctly now:

uses crt;
const X = 2;
const Y = 6;
Type matrix = array[1..X, 1..Y] of integer;
Type arr = array[1..Y] of integer;
var D: matrix; p, t: integer;
var g: arr; k, min: integer;

procedure print_mtx(const a: matrix);
var i, j: integer; begin
    for i:= 1 to X do begin
        for j:= 1 to Y do write(a[i, j]:5);
        writeln;
    end;
end;

procedure print_arr(const a: arr);
var i: integer;
begin
    for i:= 1 to Y do
        write(a[i]:5);
end;

function trans(var a: matrix): integer;
var i, j: integer; begin
    i:= 1;
    for j:= 1 to Y do begin
        if(a[i,j] >= a[i+1,j]) then begin
             g[j]:= a[i,j];
             write(a[i,j]:5);
        end
        else begin
             g[j]:= a[i+1,j];
             write(a[i+1,j]:5);
        end;
    end;
end;

begin
     clrscr;
     randomize;
     for p:= 1 to X do
         for t:= 1 to Y do D[p,t]:= random(50);
     print_mtx(D);
     writeln;
     trans(D);
     writeln;
     writeln;
     print_arr(g);
     min:= g[1];
     for k:= 1 to Y do if(g[k] < min) then min:= g[k];
     writeln;
     writeln;
     write(min:5);
end.

The call print_arr(g);can be removed so as not to output the same string as the function outputstrans

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question