Got for homework to write the following code, but I have no clue how to do so :/ ..
I need to make a program where the user writes 2 numbers for x and y, and it shows to him in which quadrant the point is in the coordinate system.
And I'm supposed to write it with ternary operators and the switch system..
I know some might say you can do it with if and else, and I already did, that's another exercise.
Ehm, to be honest I have no idea where to start, dont really understand how the switch statement will replace the whole ternary operators I was thinking something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
usingnamespace std;
int main() {
int x, y;
cin >> x;
cin >> y;
switch (x) {
case (x>0):
cout << "Smth" << endl;
}
}
But I cannot place that x>0 after case.. =/
Just help me with the start, and give me an example for 1 of the quadrants and I'll be able to finish the rest hopefully
Thanks for the answers!
But to be completely honest, I still have no clue, whuts going on.. =/
Anon's code seems easier, so I'll ask about it.
How does the switching works?
On this https://gyazo.com/f0a12db1d1f8e6616fe2201e7ffa3e09 example, what I've understand is whatever you input as an x, if its not 1 / 2 as the case ''names'' ( talking about case 1, case 2) it will do the default one?
And its confusing to me, since Anon's switch has the int q. And what exactly does it switch so it can get case 3 .-.
I'm sorry about those silly questions, i'm just completely noob in coding, trying to learn now =/.
As I interpreted your OP, x and y can be any points on a Cartesian grid.
q is essentially a bit map. If x is > 0, the low order bit of q is set. If y is > 0, the next lower bit of q is set. That gives us a binary number from 0-3.
Binary Quadrant
------- ----------
00 I
01 II
10 IV
11 III
The switch statement takes that binary value and outputs the corresponding quadrant name.
Note that we had to invert the labels for quadrants III and IV since the order of the binary values does not match the normal naming order of the quadrants.