Type number again if repeated

Asked

Viewed 66 times

1

It is necessary to make a program in which the user type 5 numbers. If one of these numbers is repeated, he must ask the user to type another number again, but any number I type appears which is repeated. My code:

   program numero_repetido;
    var
       numero: array [1..5] of integer;
       i: integer;
         ii: integer;
       valoratual:integer;
    begin
    ii:= 1;
    i:=1;
    for i:= 1 to 5 do
        begin
            if i > 0 then
            begin
             Write('Digite um número inteiro: ');
             read(numero[i]);
             
             valoratual:= numero[i];
             
                for ii:=1 to 5 do 
                    begin
                        if valoratual = numero[ii] then
                            begin;
                                Write('Número já existe, digite outro: ');
                                i:=i-1;
                                                        
                            end;
                    end;
            end
            else
            begin
                Write('Digite um número inteirooo: ');
                read(numero[i]);
            end;            
        end;
     
    for i:= 1 to 5 do
        begin
            writeLN(numero[i]);
        end;
 
   END.

2 answers

3


Notice that you take the current number typed:

valoratual:= numero[i];

And in his for that checks if the number already exists, you compare it to the item that has just been typed, because its for scans the array completely:

for ii := 1 to 5 do 
begin
    if valoratual = numero[ii] then
    begin;
        Write('Número já existe, digite outro: ');
        i:=i-1;
    end;
end;

You can change your second for, so that he goes from 1 up to the i - 1, because it would be up to the position prior to which you are:

for ii := 1 to ( i - 1 ) do 
begin
    if valoratual = numero[ii] then
    begin;
        Write('Número já existe, digite outro: ');
        i := i-1;
    end;
end;

This will correct the question of the numbers always being equal... But your code does not compile in Free Pascal... because in your example, you change the variable i within the for and that is not allowed.

If you have this problem, you can change the code a little more, see the example:

program numero_repetido;
var
    numero: array [1..5] of integer;
    i: integer;
    ii: integer;
    valoratual: integer;
    repetido: boolean;
begin
    ii := 1;
    i := 1;

    for i := 1 to 5 do
    begin
        repetido := true;

        while repetido do
        begin
            writeLn('Digite um número inteiro: ');
            read(numero[i]);
            valoratual := numero[i];
            repetido := false;

            for ii := 1 to (i - 1) do 
            begin
                if valoratual = numero[ii] then
                begin;
                    writeLn('Número já existe, digite outro!');
                    repetido := true;
                end;
            end;
        end;
    end;

    writeLn('Números digitados:');

    for i:= 1 to 5 do
    begin
        writeLn(numero[i]);
    end;
end.

In this example I started using the while as a way to keep the user inside the loop until you enter a valid number.

See online: http://tpcg.io/1AUKL6zd

1

In the second block for you are comparing the value of the array with the equal indices (i = 1 and ii = 1), that is, it will always be equal. You’re comparing a value to itself.

You can correct by requesting the first value out of the test loop, requesting the remaining values with is i:=2.. 5 and then compare the following values with a loop for ii:=1.. i-1

Also, your Else block will never run as you have conditioned your loop for values from 1 to 5 (always greater than 0).

Browser other questions tagged

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