Smallest value typed in a vector

Asked

Viewed 1,242 times

-1

I wonder why my code is returning the wrong value of the smallest variable :

int i, numero[10], menor;

menor = numero[0];

for (i = 1; i <= 5; i++){
    printf("Entre com o %d numero : \n",i);
    scanf("%d",&numero[i]);
}

for (i = 1; i<=5; i++){
    if (numero[i] < menor)
        menor = numero[i];
}


printf("Menor : %d \n",menor);

1 answer

1


You got a lot of problems here.

When you declare a variable in C and do not assign a value, it may contain a memory junk, i.e., its array numero[10] can contain dozens of values you have no idea, read more about it here:

Why a variable with default value is usually declared?

You can initialize your array in the declaration, as follows:

int numero[10] = {0,0,0,0,0,0,0,0,0,0};

With this, you will no longer have the problem of unwanted values, but notice the following, you have an array of 10 positions, but use only 5... Right after you initialize the array, if the user does not type any negative number, the lowest value will always be 0, as there are 5 positions with this value in the array.

We can then decrease the array:

int numero[5] = {0,0,0,0,0};

And also correct the for to iterate from position 0 of the array, if you don’t know, is a very important piece of information, array C starts at 0, so an array of 5 positions, starts at 0 and ends at 4:

for (i = 0; i < 5; i++){
    printf("Entre com o %d numero : \n",i+1);
    scanf("%d",&numero[i]);
}

Note that I start at 0, to display the correct number to the user, we are 1 in the index of the array at printf.

We also started the variable menor after the for, so that it already has a value present in the array:

menor = numero[0];

And finally, we fixed the for which it looks for the smallest value in the array, as we already have the value of the first position of the array, we can start this in 1:

for (i = 1; i < 5; i++){
    if (numero[i] < menor)
        menor = numero[i];
}

Obs.: How we now work with all array positions and initialize the variable menor after for, the initialization values of the array numero can be removed and you will have the same result, because you did not use the array before overwriting all its values, but I thought it valid to cite, see this online example to see and understand better the values I mentioned: https://repl.it/repls/FineOffshoreDatabases


Your final code will look like this:

#include <stdio.h>

int main(void) {
    int i, menor;
    int numero[5] = {0,0,0,0,0};

    for (i = 0; i < 5; i++){
        printf("Entre com o %d numero : \n",i+1);
        scanf("%d",&numero[i]);
    }

    menor = numero[0];

    for (i = 1; i < 5; i++){
        if (numero[i] < menor)
            menor = numero[i];
    }

    printf("Menor : %d \n",menor);

    return 0;
}

See online: https://repl.it/repls/OblongAnguishedMicrokernel


If you want to know more about why the array starts at 0, read here:

Why the array index and other sequences start at zero?

  • Thank you so much for the explanation!! I understood everything you explained and learned some interesting things with these links.

  • How cool, thank you! D

Browser other questions tagged

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