Exercise with odd pairs in C

Asked

Viewed 7,439 times

6

Statement - Make an algorithm that reads a set of numbers (X) and indicate the amount of even numbers (Qpares) and the amount of odd numbers (Qimpares) read.

Note: Entering values ends when the user inserts 0.

      #include <stdio.h>
      main(){
      int num,res,qpar=0,qimp=0;
      while(num!=0){
      printf("Introduza");
      scanf("%d",&num);
        res=num%2;
       if(res==0){
        qpar++;
    } else {
        qimp++;
    }
   }
  printf("Pares -> %d\n",&qpar);
  printf("Impares -> %d\n",&qimp);
}

I don’t know what I’m doing wrong here, it compiles well but the problem is it doesn’t give me the right values of even and odd numbers. Otherwise it is also making the cycle correctly.

5 answers

5


There are errors, the & is not used for printing. One more condition to validate before zero so that it does not enter as a pair.

Code

  #include <stdio.h>
  int main(){
      int num,qpar=0,qimp=0;
      float res;
      do{
          printf("Introduza");
          scanf("%d",&num);
          if(num!=0){ //Condicional para que  o 0 não conte como par    
              res=num%2;        

              if(res==0){
                  qpar++;
              } else {
                  qimp++;
              }
          }
      }while((num!=0));
      printf("Pares -> %d\n", qpar); //Retirado o &
      printf("Impares -> %d\n", qimp); //Retirado o &
  }
  • 1

    I removed & from my code and stopped cycling... but your code is working perfectly.

3

Your mistake is here:

    printf("Pares -> %d\n", &qpar);
    printf("Impares -> %d\n", &qimp);

Take these off &. You do not want to show the addresses of the variables, but the values. What you want is this:

    printf("Pares -> %d\n", qpar);
    printf("Impares -> %d\n", qimp);

2

Organizing the code helps to identify the problem. In the specific case it is having an integer pointer printed instead of an integer. If it were compiled as it should, it would not compile. and an error would already be presented.

The scanf() needs to pass a pointer, so uses the operator &(address of) because it needs to tell where the read value of the keyboard will be stored. no printf() should not do this, it is the very value you want to print and not the address where it is.

#include <stdio.h>

int main() {
    int num = 1, qpar = 0, qimp = 0;
    while (num != 0) {
        printf("Introduza\n");
        scanf("%d", &num);
        if (num % 2 == 0) {
            qpar++;
        } else {
            qimp++;
        }
    }
    printf("Pares -> %d\n", qpar);
    printf("Impares -> %d\n", qimp);
}

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

  • Thanks for the help, I removed the & as I said but that way the program stopped doing the cycle I do not know why... Presents me pairs and odd like 0 right after running...

  • @Nopenope is not to happen that, saw my running normal? Must have changed something else that should not.

  • You’re having the same problem as me

  • It’s not, mine’s perfect, you’ve entered the link? it shows the result perfectly. I’ll put it back here: http://ideone.com/ZsnkYa

  • Yes but I don’t know why, when I step into the dev c he won’t let me enter values.

  • This IDE (I think you are talking about DEVC++) is very bad. But there may be another problem you have introduced into the code. Copy the one that is here paste there and see if it works. I repeat if not mater the organized code is easy to get lost.

  • @bigown the problem is not the IDE, I did it by Devc++, the problem is the logical order, more precisely the while.

  • @Felipepaetzold may be, but mine is working.

  • @bigown works, but this way is counting wrong pairs and odd, stressing that 0 is a key to get out of the looping and not a pair.

  • @Felipepaetzold may be, but this is not specified in the question. It would be right to close the question because it is not clear. You "guessed" what he wanted. You might as well want to count and his current algorithm indicates this.

  • @bigown "entering values ends when the user inserts 0.". Specified.

  • But it doesn’t say whether he should be included in the count or not, and his code did. That is, you were right because you added by luck a requirement that you did not have in the question.

Show 7 more comments

0

I can identify a new error in your code, you do not start the variable num before starting to check the while. This can be a problem because the memory region referenced by the variable can contain 0, if applicable, your while will be ignored.

I see two solutions:

  • Swap your repetition structure for a do while.

  • Simply perform a num scanf before entering the while. The printf error has already been identified, but remember that when you put & in front of a variable you are asking for its memory address.

I don’t know if you did it to post here, but it’s always good to separate some lines with white spaces, where it makes sense. Separating all the code can leave it as messy as not separating

Good luck, Keep coding.

0

#include <stdio.h>

void ex_3()
{
    int n,c=1;
    do
    {
        printf("DIGITE UM NUMERO INTEIRO: ");
        scanf("%d", &n);
        if(n<=0)
        {
            c=0;
        }
        else if(n%2==0)
        {
            printf("O NUMERO %d E PAR\n", n);
        }
        else
            printf("O NUMERO %d E IMPAR\n", n);
    }
    while(c!=0);
}

Browser other questions tagged

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