How do I return only the highest value within an array?

Asked

Viewed 1,230 times

0

It does not correctly check the largest. For each animal it shows the weight of the animal as being the largest.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define TAM_MIN 1
#define TAM_MAX 500

/*
Síntese
Objetivo: Classificar animais de um zoológico segundo seu peso.
Entrada: Números de animais do zoológico, e para cada animal,
         o número de identificação e o peso em gramas.
Saída: A identificação e peso do animal mais pesado, a identificação
       e o peso do animal mais leve, e a quantidade de animais de
       peso menor que o peso médio de todos os animais.
*/


void mostra_resultados(int id_animal[TAM_MAX], float peso_animal[TAM_MAX]);
int le_valida_codigo_animal(char titulo[]);
float le_valida_peso_animal(char titulo[]);
int le_valida_inteiro(int valor_min, int valor_max, char *texto);

int main() {
    int i=1;
    int cod_animal[TAM_MAX]={0};
    float peso_animal[TAM_MAX]={0};

    printf("#--- Cadastro de animais ---#");
    int num_animais = le_valida_inteiro(TAM_MIN, TAM_MAX, "\nDigite o numero de animais do zoologico:");

    for (i; i <= num_animais; i++) {
        cod_animal[i] = le_valida_codigo_animal("Digite o codigo de identificacao dos animais:");
        peso_animal[i] = le_valida_peso_animal("Digite o peso (em gramas) do animal:");
        system("cls");
    }


    mostra_resultados(cod_animal,peso_animal);

    return 0;
}

//Valida o numero de animais(entre 1 e 500)
int le_valida_inteiro(int valor_min, int valor_max, char texto[]) {
    int num_animais = 0;

    while (1) {
        printf("%s", texto);
        scanf("%d", &num_animais);

        if (num_animais >= valor_min && num_animais <= valor_max) {
            return num_animais;
        }
        printf("\nValor invalido.\nDigite um numero entre %d e %d!", valor_min, valor_max);    
    }
}

//Valida o codigo do animal
int le_valida_codigo_animal(char titulo[]) {
    int cod=0;
    do{

        printf("%s", titulo);
        scanf("%d", &cod);

        if(cod<0){
            printf("Codigo nao pode ser negativo");
        }
    }while(cod<0);
    return cod;
}

float le_valida_peso_animal(char titulo[]){
    float peso_animal=0;
    do{

        printf("%s", titulo);
        scanf("%f", &peso_animal);

        if(peso_animal<0){
            printf("O peso nao pode ser menor que zero!");
        }

    }while(peso_animal<0);
    return peso_animal;
}


//Exibe o id e o peso do animal mais pesado
void mostra_resultados(int id_animal[TAM_MAX], float peso_animal[TAM_MAX]){
    int i;
    float mais_pesado[0];
    for(i=0;i<TAM_MAX;i++)
        {

            if(peso_animal[i] > mais_pesado[i])
                {
                    mais_pesado[i] = peso_animal[i];
                    printf("Codigo de identificao: %d\n", id_animal[i]);
                    printf("Animal mais pesado: %3.f gramas\n", mais_pesado[i]);

                }
        }


}
  • I think I got it, guys!

  • I was wrong. I thought I solved it, but it’s still giving the same problem. I think I’ll do test each case separately: the heaviest, the least heavy, and the average of the weights.

2 answers

0


Its function shows results, is running, in for, up to 500, which is the declared value for TAM_MAX. So in the function call, pass the size of the vector, which in this case would be the num_animals present in its main function. You can also change the syntax in the function call and leave the vectors as follows: int vector[]. Or use int *vector.

void mostra_resultados(int id_animal[], float peso_animal[], int num_animais){

    int i, j = 0;

    for(i=1;i<num_animais;i++)
        if(peso_animal[i] > peso_animal[j])
            j = i;

     printf("Codigo de identificao: %d\n", id_animal[j]);
     printf("Animal mais pesado: %3.f gramas\n", peso_animal[j]);

}

I believe that would fix your problem. As for what I changed in the is, what’s happening is that, "I kicked" a possible position for the heavier animal, which in this case was the first position, inside the for it will check if there is any animal in the vector that is heavier than what is in this position "kicked"if you have, j will receive this position.

  • Thanks, Romulus! There’s only one problem: it keeps going wrong. The case is that if, for example, I register two animals, it seems to read only the first value in the vector and return it as the largest. I’m not getting around to it.

  • I don’t know if this is it, but take the code with the modification I sent and in its main function, instead of starting i with 1, starting with 0 and in the reading, put i < in animals. Because the way it’s in the main function, the first position is being skipped from reading.

  • Dude, thanks! It worked fine. Now just check if what the user typed as animal code does not exist in the vector. If it exists, it asks the user to type again. I will go back to the code here and finish.

  • Show, anything I’m available.

0

Friend, if(peso_animal[i] > mais_pesado[i]) notice that you are increasing the vector weight_more without having anything in the i position to compare, that is, I have the weight of the animal, but I have nothing in the i position to compare with it, I think to solve this problem would not need the weight_more vector[]

Browser other questions tagged

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