Repeated element within matrix - Pascal

Asked

Viewed 194 times

0

I have been trying for a long time to produce a code that identifies and reports to the user if there are repeated elements in a 5x5 matrix, composed of random numbers, in the pascal. I’ve tried searching the matrix using one or two variables and also with an entire matrix for research, but it didn’t work. I have heard, from some people, that it is not necessary a very large code, nor too complex for the task. Thus my despair increases.

I ask if anyone has any solution to the problem

Pastebin link to unsuccessful attempt (and most traveled)- https://pastebin.com/uzPLACXe

Code:

program Exercicio5;
var
  Matr, same: array [1..5, 1..5] of real;
  lin, col, linPesq, ColPesq:integer;
  repete: boolean;

begin
  randomize;
  for lin:= 1 to 5 do
    begin
    writeln;
    for col:= 1 to 5 do
      begin
        Matr[lin, col]:= random (1000);
        same[lin,col]:= Matr[lin,col];
        write(Matr[lin, col]:0:2, ' | ');
        end;
    end;
  for LinPesq:= 1 to 5 do
      begin
        for ColPesq:= 1 to 5 do
          begin
            for lin:= 1 to 5 do
              begin
                for col:= 1 to 5 do
                  begin

                    if same[LinPesq,ColPesq] = Matr[lin, col] then
                        begin
                          if lin and col and LinPesq and ColPesq <> 1 then
                            begin
                        repete := true;
                        break;
                              end;
                          end;
                    end;
                  end;
                    end;
                end;
  writeln;
  writeln;
  if repete = true then
    writeln('Ha elementos repetidos na matriz')
      else
        writeln('Nao ha elementos repetidos na matriz');
  readln;

end.
  • 1

    hello, put your code here to enhance the view

1 answer

0


Hello

Your path is correct in analysis. I believe the only carelessness was in relation to the comparison you were making through a second matrix, when in fact what matters is an element-to-element comparison in the case.

Therefore, in the code below what I do is walk through the elements of the matrix, store the value of the element in a variable (nVal) that will then compare with all other elements of the matrix (rows 1 to 5) and (columns 1 to 5), looking for the same value, but, still analyzing whether row or column is different from the one being consulted.

In this case, this piece of code serves to verify if it is the line and column that is under analysis or if in fact it is another element:

( ( lin <> LinPesq ) or ( col <> ColPesq ) )

I put in a looping only to facilitate when analyzing the code

nTestes:= 10;
for i:= 1 to nTestes do
   (...)
end;

Anyway, the final code was this way:

program elemento_repetido;
var
    Matr: array [1..5, 1..5] of real;
    i, nTestes, lin, col, linPesq, ColPesq:integer;
    repete: boolean;
    nRepetido, nVal: real;
begin
    nTestes:= 10;
    for i:= 1 to nTestes do
    begin
        writeln('Teste #', i );
        repete:= false;
        randomize;
        for lin:= 1 to 5 do
        begin
            writeln;
            for col:= 1 to 5 do
            begin
                Matr[lin, col]:= random (1000);
                write(Matr[lin, col]:0:2, ' | ');
            end;
        end;
        for LinPesq:= 1 to 5 do
        begin
            for ColPesq:= 1 to 5 do
            begin
                nVal:= Matr[LinPesq,ColPesq];
                for lin:= 1 to 5 do
                begin
                    for col:= 1 to 5 do
                    begin
                        if ( nVal = Matr[lin,col] ) and 
                           ( ( lin <> LinPesq ) or ( col <> ColPesq ) ) then
                        begin
                            nRepetido:= nVal;
                            repete:= true;
                            break;
                        end;
                    end;
                end;
            end;
        end;
        writeln;
        writeln;
        if repete then
            writeln('Há elementos repetidos na matriz: ', nRepetido:6:2 )
        else
            writeln('Nao há elementos repetidos na matriz');
        readln;
    end;
end.
  • Thank you so much for answering. Code very well made :)

  • You’re welcome to have!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.