Separate even and odd numbers with two vectors in C

Asked

Viewed 2,698 times

0

I have the following problem: I need to read several numbers until the number typed is 0 or one of the vectors is fully filled, in case the vector must have a maximum of 10 indexes, and for each typed number must-separate the odd pairs on each vector respectively. But when one of the vectors is filled the program does not finish, it repeats the question of 'for' successively ending only when the value 0 is entered.

#include <stdio.h>

main(){
int par[10], impar[10], i, num, pares = 0, impares = 0;

do{
    printf("\n Digite um numero: ");
    scanf("%d", &num);
    for ( i=0; i < 20; i++){
        if (num % 2 == 0){
            par[i] = num;
            pares ++;
        }
        else if (num % 2 != 0){
            impar[i] = num;
            impares++;
        }
    }
}while((num != 0) && (pares < 10 ) || (impares < 10));
for (i=0; i<10; i++){
    printf("\n Pares: %d", par[i]);
    printf("\n Impares: %d", impar[i]);
}
}
  • Change (pares < 10 ) || (impares < 10) for (pares < 10) && (impares < 10). Problem solved?

  • 1

    To avoid confusion with the condition while I would make an infinite cycle with break inside associated with very simple conditions: if (num == 0) break;, if (pares == 10) break;

  • in-house?

3 answers

0

You have to use different variables to control the positions in each of the vectors, one for the pairs and the other for the odd ones, you are using a single variable.

#include <stdio.h>

int main(){
    int par[10], impar[10], i, num, pares = 0, impares = 0;
    printf("\n Digite um numero: ");
    scanf("%d", &num);
    while ((num != 0) && ((pares < 10 ) && (impares < 10))) {
        if (num % 2 == 0)
            par[pares++] = num;
        else
            impar[impares++] = num;
        printf("\n Digite um numero: ");
        scanf("%d", &num);
    }
    printf("%d pares\n", pares);
    for (i=0; i<pares; i++)
        printf("\t%d", par[i]);
    printf("%d ímpares\n", impares);
    for (i=0; i<impares; i++)
        printf("\t%d", impar[i]);
    return 0;
}

0


I reread the program because the logic was very confusing.
Breaking improved data entry and put some comments, thing that unfortunately is neglected in the responses.

#include <stdio.h>

int main(void)
{
  int par[10], impar[10], num, pares = 0, impares = 0;
  int i, n_read;

  for (;;)
  {
    printf("*\n");
    printf("* digite um numero: ");

    n_read = scanf(" %d", &num);
    if (n_read < 1)
    {
      printf("* digitacao invalida, ignorando linha digitada\n");
      scanf("%*[^\n]");
      continue;
    }

    if (num == 0)
    {
      // terminando entrada de dados porque leu numero 0
      break;
    }

    if (num & 1)
    {
      impar[impares++] = num;
      if (impares == 10)
      {
        // terminando entrada de dados porque ja' leu 10 impares
        break;
      }
    }
    else
    {
      par[pares++] = num;
      if (pares == 10)
      {
        // terminando entrada de dados porque ja' leu 10 pares
        break;
      }
    }
  } // for

  printf("*\n");

  printf("* total de numeros pares:   %d\n", pares);
  for (i = 0; i < pares; i++)
    printf("* par[%d]=%d\n", i, par[i]);

  printf("* \n");

  printf("* total de numeros impares:   %d\n", impares);
  for (i = 0; i < impares; i++)
    printf("* impar[%d]=%d\n", i, impar[i]);

  printf("*\n");
}
  • Just one question, what is the n_read variable for?

  • the return of the function "scanf" is the number of variables that was successfully read, in case 1 because we are reading only 1 variable: in a...if you type "p", for example, scanf will return 0 in n_read, and the program will show the message "invalid typing etc", and will ask for new typing

0

Hello,

I changed the code, I think I made the simplest way to this problem, take a look:

#include <stdio.h>

main(){
int par[10], impar[10], num, i, pares = 0, impares = 0;

do{
    printf("\n Digite um numero: ");
    scanf("%d", &num);

        //Verifica se o numero é zero, se já for sai do loop
        if(num == 0) {
            break;  
        }else{
            if (num % 2 == 0){
                par[pares] = num;
                pares++;
            } else {
                impar[impares] = num;
                impares++;
            }
        }
 //Continua a repetir o do enquanto os arrays nao estão preenchidos       
 }while(pares < 10  && impares < 10);

 printf("\n Pares: ");
 for (i=0; i< pares; i++){
    printf("%d ", par[i]); 
 }

 printf("\n Impares: ");
 for (i=0; i<impares; i++){
    printf("%d", impar[i]);
 }
}

Browser other questions tagged

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