Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Programming introduction / Practice-13.12.2021.cpp
blob: 20c04f151f8002e30de29c4c063eaa878b46b048 (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
#include <iostream>

using namespace std;

// Напишете функция с тип void plusFive(int* a);, която добавя 5 към подадена
// стойност.

void plusFive(int *a) {
	*a += 5;
}

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

	plusFive(&a);

	cout << a << endl;
}

// Задача втора:
// Да се обходи матрица чрез указателна аритметика.

void cinMatrix(int *matrix, int width, int height) {
	for (int i = width * height; i > 0; i--)
		cin >> *(matrix++);
}

void coutMatrix(int *matrix, int width, int height) {
	for (int i = width * height - 1; i >= 0; i--) {
		cout << *(matrix++) << " ";
		if (i % width == 0) cout << endl;
	}
}

void ex2() {
	const int MAX_SIZE = 256;

	int width, height;
	cin >> width >> height;

	int matrix[MAX_SIZE][MAX_SIZE];
	cinMatrix(matrix[0], width, height);
	coutMatrix(matrix[0], width, height);
}

// Задача трета:
// Да се обходи тримерен масив чрез указателна аритметика.

void cinSpace(int *space, int width, int height, int depth) {
	for (int i = width * height * depth; i > 0; i--)
		cin >> *(space++);
}

void coutSpace(int *space, int width, int height, int depth) {
	for (int i = width * height * depth - 1; i >= 0; i--) {
		cout << *(space++) << " ";
		if (i % width == 0 || i % depth == 0) cout << endl;
	}
}

void ex3() {
	const int MAX_SIZE = 64;

	int width, height, depth;
	cin >> width >> height >> depth;

	int space[MAX_SIZE][MAX_SIZE][MAX_SIZE];
	cinSpace(space[0][0], width, height, depth);
	coutSpace(space[0][0], width, height, depth);
}

// Задача четвърта:
// Да се напише функция, която намира сумата на две матрици.

void sumMatrices(int *matrix1, int *matrix2, int *sumMatrix, int width, int height) {
	for (int i = width * height - 1; i >= 0; i--)
		*(sumMatrix++) = *(matrix1++) + *(matrix2++);
}

void ex4() {
	const int MAX_SIZE = 256;

	int width, height;
	cin >> width >> height;

	int matrix1[MAX_SIZE][MAX_SIZE];
	int matrix2[MAX_SIZE][MAX_SIZE];
	int sum[MAX_SIZE][MAX_SIZE];

	cinMatrix(matrix1[0], width, height);
	cinMatrix(matrix2[0], width, height);

	sumMatrices(matrix1[0], matrix2[0], sum[0], width, height);

	coutMatrix(sum[0], width, height);
}

// Задача пета:
// Да се напише функция, която намира произведението на две матрици.

void multMatrices(int *matrix1, int *matrix2, int *mult, int width, int height) {
	for (int i = width * height - 1; i >= 0; i--) {
		for (int j = width; j > 0; j--) {
			*mult += *(matrix1++) * *matrix2;
			matrix2 += width;
		}
		matrix1 -= width;
		matrix2 = (matrix2 - width * height) + 1;
		if (i % width == 0) { 
			matrix1 += width;
			matrix2 -= width;
		}
		mult++;
	}
}

void ex5() {
	const int MAX_SIZE = 256;

	int width, height;
	cin >> width >> height;

	int matrix1[MAX_SIZE][MAX_SIZE];
	int matrix2[MAX_SIZE][MAX_SIZE];
	int mult[MAX_SIZE][MAX_SIZE];

	cinMatrix(matrix1[0], width, height);
	cinMatrix(matrix2[0], width, height);

	multMatrices(matrix1[0], matrix2[0], mult[0], width, height);

	coutMatrix(mult[0], width, height);
}

// Задача шеста*: Game of life
// Нека имаме матрица N x N. Жива клетка наричаме клетка със стойност 1. Мъртва
// клетка наричаме такава със стойност 0. Нека играем следната игра:
//   1. Всяка жива клетка с по - малко от два живи съседа умира.
//   2. Всяка жива клетка с два или три живи съседа оцелява.
//   3. Всяка жива клетка с повече от 3 съседа умира.
//   4. Всяка мъртва клетка с точно три съседа оживява.
// Този процес наричаме една еволюция. Да се реализира програма, която
// визуализира как (по избор):
//   1. Ще изглежда нашият свят след n еволюции.
//   2. Визуализира нашият свят като извежда всяка следваща еволюция. За
//      изчистване на екрана на терминала може да използвате system("cls");
// Линк към оригиналната игра: https://playgameoflife.com/.

void ex6() {

}

int main() {
	ex5();
}