How to add counter to every if of my simple code

Asked

Viewed 665 times

1

            #include <stdio.h>
            #include <stdlib.h>
            int main()
            {
                float x,y;
                char k;
                do{
                printf("Este Programa indica o quadrante no plano cartesiano\n");
                printf("Entre com o valor de x:");
                scanf("%f",&x);
                printf("Entre com o valor de y:");
                scanf("%f",&y);
                if((x>0)&&(y>0))
                printf("O Ponto P(%.2f,%.2f) pertence ao primeiro Quadrante\n",x,y);
                else
                {
                  if((x<0)&&(y>0))
                  printf("O Ponto P(%.2f.%.2f) pertence ao segundo Quadrante\n",x,y);
                  else
                  {
                    if((x<0)&&(y<0))
                    printf("O Ponto P(%.2f,%.2f) pertence ao terceiro Quadrante\n",x,y);
                    else
                    {
                      if((x>0)&&(y<0))
                      printf("O Ponto P(%.2f,%.2f) pertence ao quarto Quadrante\n",x,y);
                      else
                      {
                        if((x==0)&&((y>0||y<0)))
                        printf("O Ponto P(%.2f,%.2f) esta no eixo y\n",x,y);
                        else
                        {
                          if((y==0)&&((x>0||x<0)))
                          printf("O Ponto P(%.2f,%.2f) esta no eixo x\n",x,y);
                          else
                          {
                            if((x==0)&&(y==0))
                            printf("O Ponto P(%.2f,%.2f) esta na origem\n\n",x,y);

                fflush(stdin);
                printf("\nDeseja digitar outro ponto. (S)/(N):\n");
                scanf("%c",&k);
                }while((k=='S')||(k=='s'));
            system("Pause");

I would like to implement in this code a counter and then write the points totals in each of the situations. For example, "Total points on the x-axis: ". However, I’m not getting it for some reason. If anyone can give a hint, thank you.

  • float vs double: prefers at all times double when you have to work with floating point numbers. (Exceptions to this rule are very few.)

  • How do you use system("Pause") Knowing that you are using Windows. On all other current Operating Systems the function fflush(stdin) has Undefined Behavior. If you can write your code using another way to get those features (system("Pause") and fflush(stdin)), make that same code more portable.

1 answer

1


You haven’t explained exactly why you’re not succeeding, so I’ll tell you a little bit about the problems I’ve encountered and come up with a simple solution.

The first thing is indentation. When you have a lot of if/elseSo put it this way:

if (condicao1) {
    // código
} else if (condicao2) {
    // código
} else if (condicao3) {
   // código
}

So you avoid nesting randomly blocks that are not logically inside each other and still makes it easy to read.

With the indentation fixed and some adjustments that I comment next, it was like this:

#include <stdio.h>

int main() {
    char k;

    int qtdDePontosNoEixoX = 0;
    int qtdDePontosNoEixoY = 0;

    do {
        printf("Este Programa indica o quadrante no plano cartesiano\n");
        printf("Entre com o valor de x:");
        float x;
        scanf("%f",&x);
        printf("Entre com o valor de y:");
        float y;
        scanf("%f",&y);

        if((x>0)&&(y>0)) {
            printf("O Ponto P(%.2f,%.2f) pertence ao primeiro Quadrante\n",x,y);
        } else if((x<0)&&(y>0)) {                    
            printf("O Ponto P(%.2f.%.2f) pertence ao segundo Quadrante\n",x,y);
        } else if((x<0)&&(y<0)) {
            printf("O Ponto P(%.2f,%.2f) pertence ao terceiro Quadrante\n",x,y);
        } else if((x>0)&&(y<0)) {
            printf("O Ponto P(%.2f,%.2f) pertence ao quarto Quadrante\n",x,y);
        } else if((x==0)&&((y>0||y<0))) {
            printf("O Ponto P(%.2f,%.2f) esta no eixo y\n",x,y);
            qtdDePontosNoEixoY++;
        } else if((y==0)&&((x>0||x<0))) {
            printf("O Ponto P(%.2f,%.2f) esta no eixo x\n",x,y);
            qtdDePontosNoEixoX++;
        } else if((x==0)&&(y==0)) {
            qtdDePontosNoEixoX++;
            qtdDePontosNoEixoY++;
            printf("O Ponto P(%.2f,%.2f) esta na origem\n\n",x,y);
        }

        printf("\nDeseja digitar outro ponto. (S)/(N):\n");
        getchar(); // lendo e jogando fora a quebra de linha que fica na entrada por causa do Enter
        k = getchar();
    } while(k=='S' || k=='s');

    printf("%d dos pontos que você digitou estavam no eixo X e %d estavam no eixo Y\n", 
              qtdDePontosNoEixoX, qtdDePontosNoEixoY);

}

I removed the #include <stdlib.h>, that is not being used. It is interesting to know what you include and why, not to add something to your executable.

I moved the variables declaration x and y close to their respective scanf. Try to declare your variables always near where you are using them. So you avoid polluting a namespace larger than you need and having to keep going up and down the code to find out the type of variables or their initial value.

I removed the fflush(stdin). The function fflush only serves for flows of exit. For input flows she has the undefined behavior. I put a simple getchar() to read this Enter and throw it away and that’s enough for this simple case (if we’re going to start making production-level error checks, we’d have to tinker with a lot more stuff).

As for the solution, after organizing the code, it is very easy to see where you have to move: only create two variables, one for each axis, initialize with 0 and increment in 1 in the appropriate cases.

Browser other questions tagged

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