Most odd number

Asked

Viewed 1,061 times

2

I spent hours trying to solve, I started to think that the problem might be in the compiler, since it returns me a different (and wrong) number at each run

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");

}
  • boot int qtde_impar, int qtde_impar = 0 not to pick up memory junk.

  • Do you have the exercise statement? I’ve seen the error, but this code can be greatly improved.

  • If this is the statement, it is bad, it does not seem to be complete or clear. Your question implies that you want something else. The first thing to do a correct code is to interpret the text correctly. You have to pass the vector as well?

  • Thanks, Lucas. That’s all that’s solved my problem :)

  • else continue is unnecessary.

  • @Victorsouza actually has much more serious problems. That would be one of the problems if the exercise were to count how many odd.

  • Another way of writing vetor[cont]%2 == 1, which I believe is more understandable and simple is vetor[cont] & 1 == 1.

  • @Victorsouza Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already done so. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

Show 3 more comments

2 answers

4

The algorithm does not do what the statement asks. To take the largest odd you must store the largest in variable and update it whenever you find an odd larger than the one already stored.

I also made the function receive the vector since it does not make sense to receive the size as parameter and the vector to be 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));
}

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

If you can accept negatives, and are currently accepting, then you should start with the biggest MIN_INT to ensure that it starts as small as possible.

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

In this case, if the problem was to take the largest number then the cause is that the variable was not initialized and picked up a garbage in memory as the 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 going well.

2

Counting the amount 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 */

Browser other questions tagged

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