False return on a real number entry?

Asked

Viewed 53 times

-1

TamanhoDoVetor = int(input("Tamanho do vetor ? "))
print()
Vetor = [0] * TamanhoDoVetor

def CriaVetor(VetorNumeros,Tamanho):
    for i in range(Tamanho):
        VetorNumeros[i] = str(input("Elemento do vetor posição " + str (i+1) + " ? "))
def NumerosEstaoEmOrdemCrescente(VetorOriginal,Tamanho):
    Resultado = True
    for a in range(Tamanho):
        b = a + 1
        if a == Tamanho - 1:
            b = a
        if VetorOriginal[a] > VetorOriginal[b]:
            Resultado = False


    if Resultado == False:
         print("Os elementos do seu vetor não se encontra em ordem crescente")
    else:
        print("Os elementos do seu vetor encontra-se em ordem crescente")
CriaVetor(Vetor,TamanhoDoVetor)
NumerosEstaoEmOrdemCrescente(Vetor,TamanhoDoVetor)

in this code above they serve to know if the numbers of a vector are in ascending order, if the size of my vector is less than or equal to 9 and the entries are in ascending order type: 1, 2, 3, 4, 5, 6, 7, 8, 9 respectively the result is true, however if my vector is size 10 or larger, and my number input is 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 respectively the result and false, but how to see the result is true because they are in ascending order, but the code returns me false. I can’t identify the error!

1 answer

2


Basically the problem lies in this line :

VetorNumeros[i] = str(input("Elemento do vetor posição " + str (i+1) + " ? "))

Which makes your number vector a string vector and consequently that line:

if VetorOriginal[a] > VetorOriginal[b]: 

...you make comparison between string and their codes ASCII of its characters and not the comparison between integers you want to make.

ASCII table for numbers from 0 to 9:

Caractere   Dec  Oct    Hex
   0         48  0060   0x30
   1         49  0061   0x31
   2         50  0062   0x32
   3         51  0063   0x33
   4         52  0064   0x34
   5         53  0065   0x35
   6         54  0066   0x36
   7         55  0067   0x37
   8         56  0070   0x38
   9         57  0071   0x39

When you try to order the vector [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] in your program is actually trying to sort the vector ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"] and in the last comparison:

"9" > "10" 

It is a comparison of strings and is done in python character by character, in case the ascii code of 9 and the ascii code of 1 :

0x39 > 0x31

Other examples with the same situation:

>>> '9' > '1'
True
>>> '9' > '10'
True
>>> '9' > '100'
True
>>> '9' > '1000'
True
>>> '9' > '10000'
True

To correct the function just change the line :

VetorNumeros[i] = str(input("Elemento do vetor posição " + str (i+1) + " ? "))

for:

VetorNumeros[i] = int(input("Elemento do vetor posição " + str (i+1) + " ? "))

Code working:

TamanhoDoVetor = int(input("Tamanho do vetor ? "))
print()
Vetor = [0] * TamanhoDoVetor

def CriaVetor(VetorNumeros,Tamanho):
    for i in range(Tamanho):
        VetorNumeros[i] = int(input("Elemento do vetor posição " + str (i+1) + " ? "))
def NumerosEstaoEmOrdemCrescente(VetorOriginal,Tamanho):
    Resultado = True
    for a in range(Tamanho):
        b = a + 1
        if a == Tamanho - 1:
            b = a      
        if VetorOriginal[a] > VetorOriginal[b]:
            Resultado = False


    if Resultado == False:
         print("Os elementos do seu vetor não se encontra em ordem crescente")
    else:
        print("Os elementos do seu vetor encontra-se em ordem crescente")
CriaVetor(Vetor,TamanhoDoVetor)
NumerosEstaoEmOrdemCrescente(Vetor,TamanhoDoVetor)

Code in the Repl.it

  • 1

    Thanks man, I spent all day trying to solve this problem, but without success so far, I copied this part of the code from another issue, this part that found the error, and I was trying to look for the error within the other function!

Browser other questions tagged

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