Help with homework =/

I really need help,

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.

Please help me out =/
closed account (48T7M4Gy)
Ok where do you want to start?
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>
using namespace 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

int main() {

    int x, y;
    std::cin >> x >> y ;

    switch( (x<0) + (y<0) ) { // switch!!

        case 0: // x >= 0 && y >= 0
            std::cout << "first quadrant\n";
            break ;

        case 2: // x < 0 && y < 0
            std::cout << "third quadrant\n";
            break ;

        default: case 1 :  // x < 0 && y >= 0 or x >= 0 && y < 0
        std::cout << ( x < 0 ? "second" : "fourth" ) << " quadrant\n" ; // ternary operator!!

        // note: TODO: handle origin/points on the axis (0,0) (0,y) (-x,0) etc. (if required)
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main() 
{   int x, y, q = 0;

	cin >> x;
	cin >> y;
    q |= x > 0 ? 0 : 1;
    q |= y > 0 ? 0 : 2; 
    cout << "Quadrant: ";
    switch (q)
    {
    case 0: cout << "I"; break;
    case 1: cout << "II"; break; 
    case 2: cout << "IV"; break;
    case 3: cout << "III"; break;
    }    
    cout << endl;
	system ("pause");
}
Last edited on
>>> Not sure why a switch statement is required.

> And I'm supposed to write it with ternary operators and the switch system.. statement

That's why a switch statement is required.


For the record, but for that, the ternary operator is also not "required".

> I know some might say you can do it with if and else, and I already did, that's another exercise.
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 =/.
Last edited on
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.
Topic archived. No new replies allowed.