How to use abs() in an if function, Else if (c)

Asked

Viewed 1,875 times

0

Edit 2: What I was asked to do:

Your program must read the coordinates of a football over the field, and tell if it is inside the field, if it has gone out the side, if it has gone out the back 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 read first the x coordinate and then the y coordinate of the ball. Consider a diagonal line coming out of each corner of the field, to decide whether the ball went out sideways or down the bottom. Consider the goal as a rectangle of 7.32 x 2.44m coming out of each end of the field, in the middle of it. The only information printed by the program should be a line (terminated by n), with one of the messages: "inside", "side", "bottom" 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 night.

I did a post but got confused, and unfortunately I can’t delete.

I need to make the number assigned to X be in abs so it’s only positive, but I don’t know how to assign.

  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;
  }

Grateful.

  • convert the x positive, if negative? That’s it?

  • Yes, that would be it.

  • When I assign to the x -75 and to the y 50 (should give the result of FUND), it is result of lateral, as if not read some of the numbers "right"

  • 1

    Link to previous question: https://answall.com/q/326300/132

1 answer

0


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

  • Case xnegative, return positive.
  • Case x positive, return 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 the values absolutely, then you would have to do the operations in this way:

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

Code in relation to 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 boundary line between fundo and lateral, example:

  • (51-35)= 16, is in the bottom
  • (51-37)= 14, is in the lateral

It’s a little complicated to explain that magic number, the 15, but it is as if it were the equation of the straight line, with inclination 15

  • OK, I tried to apply them in the code but still when compiling it the results do not match. Could you check what’s wrong? I need to give two coordinates, and when they go around the corner I should tell if they went to the bottom or side. I managed using positive, but when I should use negative the results do not match.

  • @Vinicius is a mess of all size, I am not aware of the problem, I do not know what is the bottom or side

  • I was asked to read the coordinates of a football over the field, and to tell if it is inside the field, if it came out the side, if it came out the back or if it is inside the goal. The field is a rectangle of 100x70m. The program must read first the x coordinate and then the y coordinate of the ball. The x-coordinates of the field range from -50 to 50. Consider a diagonal line coming out of each corner of the field, to decide whether the ball went out sideways or down the bottom. And that’s what I’ve been trying to do ever since.

  • Or let’s simplify, there is how to put the abs function inside of Else if, since if I put x=abs(x) above Else if I can’t use Else if, and yes if.

  • Give 1 example of a coordinate that is considered background and 1 that is considered lateral. I made this example, it’s the last code I put, it has the abs within the if

  • A background coordinate (x) is one that is larger than (50), and is or is not accompanied by a y coordinate. Calculate the difference of the x and y coordinate to see if it is background or side like this: x= x-50 y= x-35 Ditei x as 100 and y like 60 x=100-50 = 50 y=60-35 = 25 x is larger than y, so it is background. If y-35>x-50 then it is lateral. The problem happens when I use negative numbers, because dai y= -60 x= -100 the x is > that y, because it is negative I should use <? x=-100-50 = 50 y=-60-35 = 25

  • I wanted an example, something like (50,0) that coordinate is on the line at the end of the field on the right side, (50,-35) is in the lower right corner of the field. I want to know what is considered fundo with an example coordinate. In the middle of this whole text it is impossible to try to help because it is super confused.

  • A background coordinate is x(70) y(50). A lateral coordinate is x(60) y(80).

  • That makes no sense, the center of the field has coordinated (0,0), these 2 coordinates you gave of example are both out of the field. (50, 35) is in the upper right corner of the field, 50, -35 is in the lower right corner. Edit the answer, target an image of a field and identify the region of the fundo and lateral, the way you’re explaining it is impossible for us to help

  • I posted an image response.

  • I’ll edit my answer, I think I have the solution

  • Thank you very much! It worked, now I will re-read what you wrote until I understand the logic behind it. Again, thank you very much.

  • @Vinicius If you have no doubts, select this answer as correct by clicking on the "right" ✅

Show 8 more comments

Browser other questions tagged

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