Highest odd number

2

I spent hours trying to solve it, I started to think that the problem might be in the compiler, since it returns a different (and wrong) number with every execution

What could be the mistake?

#include <stdio.h>
#include <stdlib.h>
int vetor[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int qtde_impar(int tamanho)
{  int cont;
 int qtde_impar ;

 for (cont = 0 ; cont < 10 ; cont++)
{if (vetor[cont]%2 == 1)
qtde_impar++;
else
continue;}
  return qtde_impar;}



int main()
{
int a ;
a = qtde_impar(10);

printf("%i\n",a);

system("PAUSE");

}
    
asked by anonymous 13.06.2017 / 21:04

2 answers

4

The algorithm does not do what the statement calls for. To get the highest odd you should save the largest variable and update it whenever you find an odd greater than what was already stored.

I also made the function receive the vector since it does not make sense to receive the size as a parameter and the vector is accessed globally.

#include <stdio.h>

int qtde_impar(int vetor[], int tamanho) {
    int maior = 1; //precisaria ver se pode ter valores negativos
    for (int cont = 0; cont < 10 ; cont++) if (vetor[cont] % 2 == 1 && vetor[cont] > maior) maior = vetor[cont];
    return maior;
}

int main() {
    int vetor[10] = { 8, 2, 1, 4, 5, 3, 7, 2, 9, 0 };
    printf("%i\n", qtde_impar(vetor, 10));
}

See running on ideone . And no Coding Ground . Also put it in GitHub for future reference .

If you can accept negatives, and are currently accepting, then you should start with the highest MIN_INT to make sure it starts as little as possible.

The problem "never" is in the compiler, especially someone who is starting up can not find a bug in the compiler. The first step in getting code right is to understand the problem and create a correct algorithm. Then understand every aspect of the language to know all the requirements of the code.

In case, if the problem were to get the largest number then the cause is that the variable was not initialized and took garbage into memory as initial value. C does not guarantee memory security, so it achieves the best possible performance, leaving the programmer responsible for taking care that everything is working.

    
13.06.2017 / 21:19
1

Counting number of odd numbers in an integer vector:

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

/* Macro para calcular quantidade de elementos dentro de um vetor */
#define sizeof_vector(_vec) (sizeof(_vec) / sizeof(_vec[0]))

/* Vetores para teste */
int g_vetor_a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int g_vetor_b[] = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
int g_vetor_c[] = { 2, 4, 6, 8, 10, 12, 14, 16, 18 };


int contarImpares( int * vet, int qtd )
{
    int n = 0;
    int i = 0;

    for( i = 0 ; i < qtd ; i++)
        if( vet[i] % 2 )
            n++;

    return n;
}


int main( int argc, char * argv[] )
{
    /* Testando */
    printf( "Vetor A: %d\n", contarImpares( g_vetor_a, sizeof_vector(g_vetor_a) ) );
    printf( "Vetor B: %d\n", contarImpares( g_vetor_b, sizeof_vector(g_vetor_b) ) );
    printf( "Vetor C: %d\n", contarImpares( g_vetor_c, sizeof_vector(g_vetor_c) ) );

    return 0;
}


/* fim */

Calculating the largest odd number contained in a vector:

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

/* Macro para calcular quantidade de elementos dentro de um vetor */
#define sizeof_vector(_vec) (sizeof(_vec) / sizeof(_vec[0]))

/* Vetores para teste */
int g_vetor_a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int g_vetor_b[] = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
int g_vetor_c[] = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };


int maiorImpar( int * vet, int qtd )
{
    int i = 0;
    int max = -1;

    for( i = 0 ; i < qtd ; i++)
        if( vet[i] % 2 )
            if( vet[i] > max )
                max = vet[i];

    return max;
}


int main( int argc, char * argv[] )
{
    /* Testando */
    printf( "Vetor A: %d\n", maiorImpar( g_vetor_a, sizeof_vector(g_vetor_a) ) );
    printf( "Vetor B: %d\n", maiorImpar( g_vetor_b, sizeof_vector(g_vetor_b) ) );
    printf( "Vetor C: %d\n", maiorImpar( g_vetor_c, sizeof_vector(g_vetor_c) ) );

    return 0;
}

/* fim */
    
13.06.2017 / 22:32