How to use abs () in an if, else if (c)

1

Edit 2: What I was asked to do:

  

Your program should read the coordinates of a soccer ball on the field, and say whether it is in the field, whether it was on the side, whether it was in the bottom or if it is inside the goal. The field is a rectangle of 100x70m. The center of the coordinates is in the center of the field. The x coordinates of the field range from -50 to 50. The program should first read the x-coordinate and then the y-coordinate of the ball. Consider a line diagonally coming out of each corner of the field to decide whether the ball came from the side or the bottom. Consider the goal as a rectangle of 7,32 x 2,44m leaving each end of field in the middle of it. The only information printed by the program should be a line (ending with \ n), with one of the messages: "inside", "lateral", "background" or "goal".

Edit: Full Code:

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
//Vinicius Rossato Piovesan, Code::Blocks 17.12

int main()
{
float x, y, a, b, c, d, e, f, g, h;
a=50;
b=-50;
c=35;
d=-35;
e=51.22;
f=-51.22;
g=3.66;
h=-3.66;
printf("Digite a coordenada de x (da bola): \n");
scanf("%f", &x);

printf("Digite a coordenada de y (da bola): \n");
scanf("%f", &y);

if(x<=a && x>=b &&  y<=c && y>=d)
printf("Dentro. \n");

else if (x>=a && x<=e && y>=h && y<=g || x<=b && x>=f && y<=g && y>=h)
printf("Gol. \n");

else if ((x>a || x<b) && (y<=c || y>=d))
printf("Fundo. \n");

else if (x-a >= y-c || x+b >= y+d)
printf("Fundo. \n");

else
printf("Lateral. \n");

return 0;
}

Good evening.

I made a post but it was confusing, and unfortunately I can not delete it.

I need to make the number assigned to X be in abs so that it is only positive, but I do not know how to assign it.

  else if ((x>a || x<b) && y<=c && y>=d)
  printf("Fundo. \n");

  else if (x-a >= y-c || x+b >= y+d)
  printf("Fundo. \n");

  else
  printf("Lateral. \n");

  return 0;
  }

Thank you.

    
asked by anonymous 31.08.2018 / 01:06

1 answer

0

abs (int x) this function returns the absolute value of x.

  • If x negative, returns positive.
  • If x positive, returns positive.
x=abs(x);   //basta esta linha para converter 'x' para positivo

 else if ((x>a || x<b) && y<=c && y>=d)
  printf("Fundo. \n");

  else if (x-a >= y-c || x+b >= y+d)
  printf("Fundo. \n");

  else
  printf("Lateral. \n");

  return 0;
  }

If you want to compare values absolutely, then you would have to do the following operations:

  else if (abs(x-a) >= abs(y-c) || abs(x+b) >= abs(y+d))

Code regarding the statement:

else if (x>=a && x<=e && y>=h && y<=g || x<=b && x>=f && y<=g && y>=h)
printf("Gol. \n");


else if(abs(x)-abs(y)>15)
printf("Fundo. \n");

else
printf("Lateral. \n");

That 15 is the limit line between fundo and lateral , eg:

  • (51-35)= 16 , is in background
  • (51-37)= 14 , is in the side

It's a bit complicated to explain that magic number, 15 , but it's like the equation of the line, with slope 15

    
31.08.2018 / 01:12