diff options
Solved exercise 0 of homework1.cpp
1 files changed, 60 insertions, 2 deletions
| -rw-r--r-- | Year 1/Programming introduction/Homeworks/homework1.cpp | 62 |
diff --git a/Year 1/Programming introduction/Homeworks/homework1.cpp b/Year 1/Programming introduction/Homeworks/homework1.cpp index f76c172..f443cb9 100644 --- a/Year 1/Programming introduction/Homeworks/homework1.cpp +++ b/Year 1/Programming introduction/Homeworks/homework1.cpp @@ -5,6 +5,65 @@ using namespace std; /* + * Exercise 0 + */ + +double distanceBetweenPoints(int x1, int y1, int x2, int y2) +{ + int sideA = x1 - x2, sideB = y1 - y2; + return sqrt(sideA * sideA + sideB * sideB); // Pythagorean theorem +} + +void ex0() { + // These should be precalculated, but this shows how they're found out + float const SLOPE_B = 2.828 / 2.828, + SLOPE_C = -2.828 / -2.828, + SLOPE_D = -2.828 / 2.828, + SLOPE_E = 2.828 / -2.828; + + double x, y; + cin >> x >> y; + + if (x == 0) // if it's on the Y ordinate + cout << "On a line" << endl; + else if (distanceBetweenPoints(0, 0, x, y) < 4.0) { + float pointSlope = y / x; + + if (x == -2.828 || x == 2.828 || pointSlope == SLOPE_B || pointSlope == SLOPE_C || pointSlope == SLOPE_D || pointSlope == SLOPE_E) + cout << "On a line" << endl; + else if (y >= 0 && x > 0) { // Check 1st quadrant + if (pointSlope > SLOPE_B) + cout << "Inside green area!" << endl; + else if (x > 2.828) + cout << "Inside purple area!" << endl; + else + cout << "Inside yellow area!" << endl; + } + else if (y >= 0 && x < 0) { // Check 2nd quadrant + if (pointSlope < SLOPE_D) + cout << "Inside red area!" << endl; + else if (x > -2.828) + cout << "Inside pink area!" << endl; + } + else if (y < 0 && x < 0) { // Check 3rd quadrant + if (pointSlope > SLOPE_C) + cout << "Inside blue area!" << endl; + else if (x > -2.828) + cout << "Inside pink area!" << endl; + } + else { // Check 4th quadrant + if (pointSlope > SLOPE_E && x > 2.828) + cout << "Inside purple area!" << endl; + else + cout << "Inside yellow area!" << endl; + } + } + else if ((-2.0 < x && x < 2.0) && (-6.0 < y && y < -7.0)) // outside circle and in rectangle + cout << "Inside grey area" << endl; + +} + +/* * Exercise 1 */ @@ -177,7 +236,6 @@ void countBullsAndCows(int secretNum, int guess, int *bulls, int *cows) { void playBullsAndCows() { int secNum = genSecretNum(), guess = 0, bulls = 0, cows = 0; - cout << secNum << endl; for (int i = 0; i < N; i++) { cin >> guess; @@ -199,5 +257,5 @@ void ex5() { } int main() { - ex5(); + ex0(); } |
