Find the highest vector value

Asked

Viewed 834 times

1

A program that asks to find the largest number of an array, typing the amount of elements and until which element it will verify. However, in this program that I did it is not considering the first element typed. If I type 5, 4, 3, 2, 1 it says the biggest is 4. I have no ideas how to solve

int i, qntd, n;
printf ("Digite a quantidade de elementos: ");
scanf ("%d", &qntd);
printf ("Ate onde considerar: ");
scanf ("%d", &n);

float vet[qntd], maior = 0;

for (i = 0; i < qntd; i++) {        //Preencher o vetor
    printf ("Digite um numero: ");
    scanf ("%f", &vet[i]);
}

for (i = 0; i < n; i++) {
    if (i == 0) {
        vet[i] = maior;
    }
    if (vet[i] > maior) {
        maior = vet[i];
    }
}
printf ("Maior = %.2f \n", maior);



system ("PAUSE");
  • Sure you need to read a number for quantity and another for the n? Taking advantage, your entry is incomplete: you did not say which qntd and which n you typed. And if said your input is with 2 numbers less than it should

2 answers

1


I believe you got confused on the first line that assigned the highest value.

for (i = 0; i < n; i++) {
    if (i == 0) {
        vet[i] = maior;
    }
    if (vet[i] > maior) {
        maior = vet[i];
    }
}

The right thing would be:

for (i = 0; i < n; i++) {
    if (i == 0) {
        maior = vet[i];
    }
    if (vet[i] > maior) {
        maior = vet[i];
    }
}

As the two if’s are equal, you wouldn’t even need that check.

Of course, in this case it wouldn’t work for negative numbers, since you initialize the "higher" variable with zero. You could then do this assignment immediately before the go:

maior = vet[0];
for (i = 0; i < n; i++) {
       if (vet[i] > maior)
           maior = vet[i];
    }
}

There are many ways to solve your problem, but the only mistake was in the first assignment.

  • Thank you very much. Something so simple that I couldn’t think of the time

1

There are errors there, but the code is too complex, it can be much simpler than this. And the statement is not good, what is the point of asking how far you should check? If you won’t check the rest or ask for the other data, but I’ll keep the algorithm doing something really meaningless:

#include <stdio.h>
#include <float.h>

int main(void) {
    int qntd, n;
    printf ("Digite a quantidade de elementos: ");
    scanf ("%d", &qntd);
    printf ("Ate onde considerar: ");
    scanf ("%d", &n);
    float vet[qntd], maior = FLT_MIN;
    for (int i = 0; i < qntd; i++) {
        printf ("Digite um numero: ");
        scanf ("%f", &vet[i]);
        if (i < n && vet[i] > maior) maior = vet[i];
    }
    printf ("Maior = %.2f \n", maior);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

You have to catch the smallest floatpossible, 0 is not the least, nothing prevents a negative value from being typed, unless it had to do this validation according to the statement, but it does not do this. And you can filter and find the biggest soon when entering the data you don’t need to make another loop for it. To tell the truth nor should create an array, I just did not want to change the code too much, but a simple variable solves, the statement does not ask to store in the vector.

Most errors of this type occur when it starts to complicate the code further. The best code is the one that doesn’t exist, so try to do it in the simplest way. Note that the error disappeared just by simplifying.

Browser other questions tagged

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