Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Programming introduction / Homeworks / homework1.cpp
diff options
context:
space:
mode:
Commit69e2c19137a467ea0870b0297b6611e8bddb9f7d (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2021-11-21 16:23:44 +0200
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2021-11-21 16:23:44 +0200
Files1b3106e1affd380d4209f0b4914d799b7eb8a1e7 /Year 1/Programming introduction/Homeworks/homework1.cpp
uni-projects-69e2c19137a467ea0870b0297b6611e8bddb9f7d.tar
uni-projects-69e2c19137a467ea0870b0297b6611e8bddb9f7d.tar.gz
uni-projects-69e2c19137a467ea0870b0297b6611e8bddb9f7d.zip
Parent28d28bfb4c8fe765b93745af78ba9f06b334e65a (diff)
Solved exercise 5 of homework1.cpp
1 files changed, 94 insertions, 1 deletions
-rw-r--r--Year 1/Programming introduction/Homeworks/homework1.cpp95
diff --git a/Year 1/Programming introduction/Homeworks/homework1.cpp b/Year 1/Programming introduction/Homeworks/homework1.cpp
index c6832ad..f76c172 100644
--- a/Year 1/Programming introduction/Homeworks/homework1.cpp
+++ b/Year 1/Programming introduction/Homeworks/homework1.cpp
@@ -1,5 +1,6 @@
#include <iostream>
#include <cmath>
+#include <stdlib.h> // rand and srand for ex. 5
using namespace std;
@@ -105,6 +106,98 @@ void ex4() {
cout << sumOfInterestingNumsBetween(a, b) << endl;
}
+/*
+ * Exercise 5
+ */
+
+const int NUMBER_LENGTH = 4, N = 10;
+
+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) {
+ (result *= 10) += rnd % 10;
+ i++;
+ }
+ else if (rnd == 0) {
+ rnd = rand();
+ }
+
+ rnd /= 10;
+ }
+
+ return result;
+}
+
+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) {
+ *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++) {
+ if (guess % 10 == secretNum % 10)
+ *bulls += 1;
+ else {
+ (secretCows *= 10) += secretNum % 10;
+
+ // Getting unique cows prevents the later cow loop from incrementing cows
+ // multiple times when we have duplicate digits in secretNum and in guessCows
+ if (!digitInsideNum(guess % 10, guessCows))
+ (guessCows *= 10) += guess % 10;
+ }
+
+ secretNum /= 10;
+ guess /= 10;
+ }
+
+ while (secretCows != 0) {
+ if (digitInsideNum(secretCows % 10, guessCows))
+ *cows += 1;
+
+ secretCows /= 10;
+ }
+}
+
+void playBullsAndCows() {
+ int secNum = genSecretNum(), guess = 0, bulls = 0, cows = 0;
+ cout << secNum << endl;
+
+ for (int i = 0; i < N; i++) {
+ cin >> guess;
+
+ if (guess == secNum) {
+ cout << "Позна!" << endl;
+ return;
+ }
+ else {
+ countBullsAndCows(secNum, guess, &bulls, &cows);
+ cout << bulls << " бикове и " << cows << " крави" << endl;
+ }
+ }
+ cout << "Не позна в определения брой ходове!" << endl;
+}
+
+void ex5() {
+ playBullsAndCows();
+}
+
int main() {
- ex4();
+ ex5();
}