Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Programming introduction / Homeworks / homework1.cpp
diff options
context:
space:
mode:
1 files changed, 99 insertions, 51 deletions
-rw-r--r--Year 1/Programming introduction/Homeworks/homework1.cpp150
diff --git a/Year 1/Programming introduction/Homeworks/homework1.cpp b/Year 1/Programming introduction/Homeworks/homework1.cpp
index 0ae657c..29bb23d 100644
--- a/Year 1/Programming introduction/Homeworks/homework1.cpp
+++ b/Year 1/Programming introduction/Homeworks/homework1.cpp
@@ -12,15 +12,18 @@ using namespace std;
double const FLOAT_EPSILON_FLOOR = 0.0001, FLOAT_EPSILON_CEIL = FLOAT_EPSILON_FLOOR * 9;
-bool eq(double a, double b) {
+bool eq(double a, double b)
+{
return ((a - b) < 0.0 ? (b - a) : (a - b)) <= FLOAT_EPSILON_FLOOR;
}
-bool lt(double a, double b) {
+bool lt(double a, double b)
+{
return !eq(a, b) && (a - b) < FLOAT_EPSILON_CEIL;
}
-bool mt(double a, double b) {
+bool mt(double a, double b)
+{
return lt(b, a);
}
@@ -34,7 +37,8 @@ double distanceBetweenPoints(int x1, int y1, int x2, int y2)
/* Exercise calculations */
-void ex0() {
+void ex0()
+{
// These should be precalculated, but this shows how they're found out
// The coordinates of points B, C, D and E are some combination of positive and/or negative values of 2.828
float const CROSS_PNT = 2.828,
@@ -47,13 +51,20 @@ void ex0() {
cin >> x >> y;
if (eq(x, 0)) // if it's on the Y ordinate
+ {
cout << "On a line" << endl;
- else if (lt(distanceBetweenPoints(0, 0, x, y), 4.0)) {
+ }
+ else if (lt(distanceBetweenPoints(0, 0, x, y), 4.0))
+ {
double pointSlope = y / x;
- if (eq(x, -CROSS_PNT) || eq(x, CROSS_PNT) || eq(pointSlope, SLOPE_B) || eq(pointSlope, SLOPE_C) || eq(pointSlope, SLOPE_D) || eq(pointSlope, SLOPE_E))
+ if (eq(x, -CROSS_PNT) || eq(x, CROSS_PNT)
+ || eq(pointSlope, SLOPE_B) || eq(pointSlope, SLOPE_C) || eq(pointSlope, SLOPE_D) || eq(pointSlope, SLOPE_E))
+ {
cout << "On a line" << endl;
- else if ((mt(y, 0) || eq(y, 0)) && mt(x, 0)) { // Check 1st quadrant
+ }
+ else if ((mt(y, 0) || eq(y, 0)) && mt(x, 0)) // Check 1st quadrant
+ {
if (mt(pointSlope, SLOPE_B))
cout << "Inside green area!" << endl;
else if (mt(x, CROSS_PNT))
@@ -61,39 +72,45 @@ void ex0() {
else
cout << "Inside yellow area!" << endl;
}
- else if ((mt(y, 0) || eq(y, 0)) && lt(x, 0)) { // Check 2nd quadrant
+ else if ((mt(y, 0) || eq(y, 0)) && lt(x, 0)) // Check 2nd quadrant
+ {
if (lt(pointSlope, SLOPE_D))
cout << "Inside red area!" << endl;
else if (x > -CROSS_PNT)
cout << "Inside pink area!" << endl;
}
- else if (lt(y, 0) && lt(x, 0)) { // Check 3rd quadrant
+ else if (lt(y, 0) && lt(x, 0)) // Check 3rd quadrant
+ {
if (mt(pointSlope, SLOPE_C))
cout << "Inside blue area!" << endl;
else if (mt(x, -CROSS_PNT))
cout << "Inside pink area!" << endl;
}
- else { // Check 4th quadrant
+ else // Check 4th quadrant
+ {
if (mt(pointSlope, SLOPE_E) && mt(x, CROSS_PNT))
cout << "Inside purple area!" << endl;
else
cout << "Inside yellow area!" << endl;
}
}
- else if ((lt(-2, x) && lt(x, 2)) && (lt(-6, y) && lt(y, -7))) // outside circle and in rectangle
+ else if ((lt(-2, x) && lt(x, 2)) && (lt(-6, y) && lt(y, -7))) // Outside circle and in rectangle
+ {
cout << "Inside grey area" << endl;
-
+ }
}
/*
* Exercise 1
*/
-double f(unsigned int k, int x) {
+double f(unsigned int k, int x)
+{
double sum = x;
- for (unsigned int i = 2; i <= k; i++) {
- if (i & 1) // if odd
+ for (unsigned int i = 2; i <= k; i++)
+ {
+ if (i & 1)
sum = 3 * sum + 1;
else
sum /= 2;
@@ -102,7 +119,8 @@ double f(unsigned int k, int x) {
return sum;
}
-void ex1() {
+void ex1()
+{
int x;
cin >> x;
@@ -113,35 +131,40 @@ void ex1() {
* Exercise 2
*/
-void setMarmaladeType(int type, int *blueberryMarmalades, int *peachMarmalades, int *rosehipMarmalades) {
+void setMarmaladeType(int type, int *blueberryMarmalades, int *peachMarmalades, int *rosehipMarmalades)
+{
if (type % 7 == 0) *blueberryMarmalades += 1;
- else if (type % 13 == 0) *peachMarmalades += 1;
- else if (type % 17 == 0) *rosehipMarmalades += 1;
+ else if (type % 13 == 0) *peachMarmalades += 1;
+ else if (type % 17 == 0) *rosehipMarmalades += 1;
}
-void ex2() {
+void ex2()
+{
int archiveLength;
cin >> archiveLength;
int blueberryMarmalades = 0, peachMarmalades = 0, rosehipMarmalades = 0;
- for (int i = 0, digit, countryDigits, typeDigits, type; i < archiveLength; i++) {
+ for (int i = 0, digit, countryDigits, typeDigits, type; i < archiveLength; i++)
+ {
cin >> digit;
- cout << digit;
- if (i % 8 == 0) {
+ if (i % 8 == 0) // Reset at start of ID number
+ {
countryDigits = 1;
typeDigits = type = 0;
}
- else if (countryDigits < 3) {
+ else if (countryDigits < 3)
+ {
countryDigits++;
}
- else if (typeDigits < 4) {
+ else if (typeDigits < 4)
+ {
typeDigits++;
(type *= 10) += digit;
}
- else if (typeDigits == 4) {
- cout << endl;
+ else if (typeDigits == 4)
+ {
setMarmaladeType(type, &blueberryMarmalades, &peachMarmalades, &rosehipMarmalades);
}
}
@@ -155,24 +178,29 @@ void ex2() {
* Exercise 4
*/
-bool isInteresting(int n) {
+bool isInteresting(int n)
+{
int lastDigit;
- do {
+ do
+ {
lastDigit = n % 10;
n /= 10;
if ((lastDigit & 1) == ((n % 10) & 1))
return false;
- } while (n != 0);
+ }
+ while (n != 0);
return true;
}
-int sumOfInterestingNumsBetween(int a, int b) {
+int sumOfInterestingNumsBetween(int a, int b)
+{
int sum = 0;
- for (int i = a; i <= b; i++) {
+ for (int i = a; i <= b; i++)
+ {
if (isInteresting(i))
sum += i;
}
@@ -180,7 +208,8 @@ int sumOfInterestingNumsBetween(int a, int b) {
return sum;
}
-void ex4() {
+void ex4()
+{
int a, b;
cin >> a >> b;
@@ -193,19 +222,23 @@ void ex4() {
const int NUMBER_LENGTH = 4, N = 10;
-int genSecretNum() {
+int genSecretNum()
+{
// Initializes random seed (required for getting a random number on every program launch)
// Taken from included reference: https://www.cplusplus.com/reference/cstdlib/rand#example
srand (time(NULL));
int rnd = rand(), result = 0;
- for (int i = 0; i < NUMBER_LENGTH;) {
- if (rnd % 10 != 0) {
+ for (int i = 0; i < NUMBER_LENGTH;)
+ {
+ if (rnd % 10 != 0)
+ {
(result *= 10) += rnd % 10;
i++;
}
- else if (rnd == 0) {
+ else if (rnd == 0)
+ {
rnd = rand();
}
@@ -215,27 +248,34 @@ int genSecretNum() {
return result;
}
-bool digitInsideNum(int digit, int num) {
- while (num != 0) {
+bool digitInsideNum(int digit, int num)
+{
+ while (num != 0)
+ {
if (digit == num % 10)
return true;
+
num /= 10;
}
+
return false;
}
-void countBullsAndCows(int secretNum, int guess, int *bulls, int *cows) {
+void countBullsAndCows(int secretNum, int guess, int *bulls, int *cows)
+{
*bulls = *cows = 0;
// If our secret num can contain zeroes, this (and the corresponding checks) should be changed to some other digit
int secretCows = 0, guessCows = 0;
- /* Find bulls and separate the numbers that dont make a bull */
-
- for (int i = 0; i < NUMBER_LENGTH; i++) {
+ for (int i = 0; i < NUMBER_LENGTH; i++)
+ {
if (guess % 10 == secretNum % 10)
+ {
*bulls += 1;
- else {
+ }
+ else
+ {
(secretCows *= 10) += secretNum % 10;
// Getting unique cows prevents the later cow loop from incrementing cows
@@ -248,7 +288,8 @@ void countBullsAndCows(int secretNum, int guess, int *bulls, int *cows) {
guess /= 10;
}
- while (secretCows != 0) {
+ while (secretCows != 0)
+ {
if (digitInsideNum(secretCows % 10, guessCows))
*cows += 1;
@@ -256,28 +297,35 @@ void countBullsAndCows(int secretNum, int guess, int *bulls, int *cows) {
}
}
-void playBullsAndCows() {
+void playBullsAndCows()
+{
int secNum = genSecretNum(), guess = 0, bulls = 0, cows = 0;
- for (int i = 0; i < N; i++) {
+ for (int i = 0; i < N; i++)
+ {
cin >> guess;
- if (guess == secNum) {
+ if (guess == secNum)
+ {
cout << "Позна!" << endl;
return;
}
- else {
+ else
+ {
countBullsAndCows(secNum, guess, &bulls, &cows);
cout << bulls << " бикове и " << cows << " крави" << endl;
}
}
+
cout << "Не позна в определения брой ходове!" << endl;
}
-void ex5() {
+void ex5()
+{
playBullsAndCows();
}
-int main() {
+int main()
+{
ex0();
}