Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Programming introduction / Homeworks / homework1.cpp
blob: 1ce8a2c16eae5cc709a9a4ec79cd65ab9b423901 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// 
// ++===================++
// || Оценка: 5.45/6.00 ||
// ++===================++
//
// +-----------+
// | !Бележки! |
// +-----------+
// Трета ти е грешна, не проверяваш правилно (тествай с други числа)  -0.50
// И на първа грешно проверяваш сивото, пробвай с 0 и - 6.5 примерно  -0.05
//

#include <iostream> // cin, cout, endl
#include <cmath>    // fabs, sqrt
#include <stdlib.h> // rand, srand

using namespace std;

/*
 * Exercise 0
 */

/* Floating point arithmetic */

double const FLOAT_EPSILON = 0.001;

bool eq(double a, double b)
{
	return fabs(a - b) < FLOAT_EPSILON;
}

bool lt(double a, double b)
{
	return !eq(a, b) && (a - b) < FLOAT_EPSILON;
}

bool mt(double a, double b)
{
	return lt(b, a);
}

/* Planimetry */

double distanceBetweenPoints(double x1, double y1, double x2, double y2)
{
	double sideA = x1 - x2, sideB = y1 - y2;
	return sqrt(sideA * sideA + sideB * sideB); // Pythagorean theorem
}

void printPointLocation(double x, double y) {
	// The coordinates of points B, C, D and E are some combination of positive and/or negative values of 2.828,
	// because BC and DE make 45 degrees angle with the abscissa
	float const CROSS_PNT = 2.828,
				SLOPE_BC =  CROSS_PNT / CROSS_PNT,
				SLOPE_DE = -CROSS_PNT / CROSS_PNT;
	char const NOWHERE[] = "Nowhere!";

	if (eq(x, 0))
		cout << NOWHERE << endl; // On a line
	else if (lt(distanceBetweenPoints(0, 0, x, y), 4.0))
	{
		double pointSlope = y / x;

		// Added brackets in some places for improved readability
		if (eq(x, -CROSS_PNT) || eq(x, CROSS_PNT) || eq(pointSlope, SLOPE_BC) || (eq(pointSlope, SLOPE_DE) && mt(y, 0)))
		{
			cout << NOWHERE << endl; // On a line
		}
		else if (mt(pointSlope, SLOPE_BC))
		{
			cout << "Inside " << ((x > 0) ? "green" : "blue" ) << " area!" << endl;
		}
		else if (lt(pointSlope, SLOPE_DE) && x < 0)
		{
			cout << "Inside red area!" << endl;
		}
		else if (mt(x, CROSS_PNT))
		{
			cout << "Inside purple area!" << endl;
		}
		else if (mt(x, -CROSS_PNT) && x < 0)
		{
			cout << "Inside pink area!" << endl;
		}
		else if (x > 0)
			cout << "Inside yellow area!" << endl;
		else
			cout << NOWHERE << endl;
	}
	else if (lt(-2, x) && lt(x, 2) && mt(-6, y) && mt(y, -7))
		cout << "Inside grey area!" << endl;
	else
		cout << NOWHERE << endl;
}

/* Exercise calculations */

void ex0()
{
	double x, y;
	cin >> x >> y;

	printPointLocation(x, y);
}

/*
 * Exercise 1
 */

double f(unsigned int k, int x)
{
	unsigned int sum = x;

	for (unsigned int i = 2; i <= k; i++)
	{
		if (sum & 1)
			sum = 3 * sum + 1;
		else
			sum /= 2;
	}

	return sum;
}

void ex1()
{
	int x;
	cin >> x;

	cout << f(35, x) << endl;
}

/*
 * Exercise 2
 */

void setMarmaladeType(unsigned int type, unsigned int* const blueberryMarmalades, unsigned int* const peachMarmalades, unsigned int* const rosehipMarmalades)
{
	if      (type %  7 == 0) *blueberryMarmalades += 1;
	else if (type % 13 == 0) *peachMarmalades     += 1;
	else if (type % 17 == 0) *rosehipMarmalades   += 1;
}

void readCodesAndCountMarmalades(unsigned int archiveLength, unsigned int* const blueberryMarmalades, unsigned int* const peachMarmalades, unsigned int* const rosehipMarmalades)
{
	archiveLength -= archiveLength % 8; // The last code could be invalid (shorter than 8 digits)

	for (unsigned int i = 0, digit, countryDigits, typeDigits, type; i < archiveLength; i++)
	{
		cin >> digit;

		if (i % 8 == 0) // Reset at start of every code
		{
			countryDigits = 1;
			typeDigits = type = 0;
		}
		else if (countryDigits < 3)
		{
			countryDigits++;
		}
		else if (typeDigits < 4)
		{
			typeDigits++;
			type = type * 10 + digit;
		}
		else if (typeDigits == 4)
		{
			setMarmaladeType(type, blueberryMarmalades, peachMarmalades, rosehipMarmalades);
		}
	}
}

void ex2()
{
	unsigned int archiveLength;
	cin >> archiveLength;

	unsigned int blueberryMarmalades = 0, peachMarmalades = 0, rosehipMarmalades = 0;
	readCodesAndCountMarmalades(archiveLength, &blueberryMarmalades, &peachMarmalades, &rosehipMarmalades);
	
	cout << "Blueberry marmalades: " << blueberryMarmalades << endl;
	cout << "Peach marmalades: " << peachMarmalades << endl;
	cout << "Rosehip marmalades: " << rosehipMarmalades << endl;
}

/*
 * Exercise 3
 */

bool isInteresting(int n)
{
	while (n > 9)
	{
		if ((n & 1) == !!(n & 2))
			return false;
		n /= 10;
	}

	return true;
}

int sumOfInterestingNumsBetween(int a, int b)
{
	int sum = 0;

	for (int i = a; i <= b; i++)
	{
		if (isInteresting(i))
			sum += i;
	}

	return sum;
}

void ex3()
{
	int a, b;
	cin >> a >> b;

	cout << sumOfInterestingNumsBetween(a, b) << endl;
}

/* 
 * Exercise 4
 */

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;
	bool takenDigits[10] = { 0 };
	takenDigits[0] = true; // we can't have 0s in secret

	for (int i = 0; i < NUMBER_LENGTH; i++)
	{
		while (takenDigits[rnd % 10])
		{
			rnd /= 10;
			if (rnd == 0)
				rnd = rand();
		}
		result = result * 10 + rnd % 10;
		takenDigits[rnd % 10] = true;
	}

	return result;
}

bool digitInsideNum(int digit, int num)
{
	while (num != 0)
	{
		if (digit == num % 10)
			return true;

		num /= 10;
	}

	return false;
}

bool isValidGuess(int guess)
{
	if (guess < 0) return false;

	bool recordedDigits[10] = { 0 };
	recordedDigits[0] = true; // We don't have zeroes in secret number

	for (int i = 0; i < NUMBER_LENGTH; i++)
	{
		if (guess == 0 || recordedDigits[guess % 10])
			return false;
		
		recordedDigits[guess % 10] = true;
		guess /= 10;
	}

	return guess == 0; // False if wider than NUMBER_LENGTH digits
}

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; 

	for (int i = 0; i < NUMBER_LENGTH; i++)
	{
		if (guess % 10 == secretNum % 10)
			*bulls += 1;
		else
		{
			secretCows = secretCows * 10 + secretNum % 10;
			// If the guess (and secret) can have duplicate digits, this should be done only with the unique ones
			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;

	for (int i = 0; i < N; i++)
	{
		cin >> guess;

		if (!isValidGuess(guess))
		{
			cout << "Your guess is invalid!" << endl;
		}
		else if (guess == secNum)
		{
			cout << "You win!" << endl;
			return;
		}
		else
		{
			countBullsAndCows(secNum, guess, &bulls, &cows);
			cout << bulls << " bulls and " << cows << " cows" << endl;
		}
	}

	cout << "You lose! The number was " << secNum << " and you couldn't guess it in " << N << " guesses!" << endl;
}

void ex4()
{
	playBullsAndCows();
}

int main()
{
	ex4();
}