Last active
March 28, 2025 10:41
-
-
Save Helen460/ac781cab52bd18ff452f8c0f89201672 to your computer and use it in GitHub Desktop.
5DZ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Одновимірні масиви: | |
| // 1. Ввести масив із 5 чисел, а потім вивести його у зворотному порядку. | |
| #include <iostream> | |
| using namespace std; | |
| int main() | |
| { | |
| const int size = 5; | |
| int ar[size]; | |
| for (int i = 0; i < size; i++) { | |
| cin >> ar[i]; | |
| } | |
| for (int i = size - 1; i >= 0; i--) { | |
| cout << ar[i] << " "; | |
| } | |
| cout << "\n"; | |
| } | |
| // 2. Створити масив із 20 випадкових чисел. Вивести всі елементи масиву з парними індексами. | |
| #include <iostream> | |
| using namespace std; | |
| int main() | |
| { | |
| const int size = 20; | |
| int ar[size]; | |
| for (int i = 0; i < size; i++) { | |
| ar[i] = rand() % 100; | |
| } | |
| for (int i = 0; i < size; i += 2) { | |
| cout << ar[i] << " "; | |
| } | |
| cout << "\n"; | |
| } | |
| // 3. Створити масив із 10 випадкових чисел у діапазоні від -20 до 20. Визначити кількість, суму та середнє арифметичне додатних елементів масиву. | |
| #include <iostream> | |
| #include <windows.h> | |
| using namespace std; | |
| int main() | |
| { | |
| SetConsoleOutputCP(1251); | |
| const int size = 10; | |
| int ar[size]; | |
| for (int i = 0; i < size; i++) { | |
| ar[i] = rand() % 41 - 20; | |
| } | |
| int count = 0; | |
| int sum = 0; | |
| for (int i = 0; i < size; i++) { | |
| if (ar[i] > 0) { | |
| count++; | |
| sum += ar[i]; | |
| } | |
| } | |
| cout << "Кількість додатних елементів: " << count << "\n"; | |
| cout << "Сума додатних елементів: " << sum << "\n"; | |
| cout << "Середнє арифметичне: " << (double)sum / count << "\n"; | |
| } | |
| // Двовимірні масиви | |
| // 1. Дано двовимірний масив розмірністю M x N, заповнений випадковими числами з діапазону від 0 до 20. Визначити суму елементів масиву, середнє арифметичне, мінімальний і максимальний елемент. | |
| #include <iostream> | |
| #include <windows.h> | |
| #include <algorithm> | |
| #include <iomanip> | |
| using namespace std; | |
| int main() | |
| { | |
| SetConsoleOutputCP(1251); | |
| const int M = 4; | |
| const int N = 5; | |
| int ar[M][N]{}; | |
| for (int i = 0; i < M; i++) { | |
| for (int j = 0; j < N; j++) { | |
| ar[i][j] = rand() % 21; | |
| printf("%3d ", ar[i][j]); | |
| } | |
| cout << "\n"; | |
| } | |
| int sum = 0; | |
| int minVal = 21; | |
| int maxVal = -1; | |
| for (int i = 0; i < M; i++) { | |
| for (int j = 0; j < N; j++) { | |
| sum += ar[i][j]; | |
| minVal = min(minVal, ar[i][j]); | |
| maxVal = max(maxVal, ar[i][j]); | |
| } | |
| } | |
| double avg = (double)sum / (M * N); | |
| cout << "\nСума елементів: " << sum << "\n"; | |
| cout << "Середнє арифметичне: " << avg << "\n"; | |
| cout << "Мінімальний елемент: " << minVal << "\n"; | |
| cout << "Максимальний елемент: " << maxVal << "\n"; | |
| } | |
| // 2. Дано двовимірний масив розмірністю M x M, заповнений випадковими числами з діапазону від 0 до 20. Визначити суму елементів, розташованих на головній діагоналі, а також суму елементів, розташованих на побічній діагоналі. | |
| #include <iostream> | |
| #include <windows.h> | |
| #include <algorithm> | |
| #include <iomanip> | |
| using namespace std; | |
| int main() | |
| { | |
| SetConsoleOutputCP(1251); | |
| const int M = 4; | |
| int ar[M][M]{}; | |
| for (int i = 0; i < M; i++) { | |
| for (int j = 0; j < M; j++) { | |
| ar[i][j] = rand() % 21; | |
| printf("%3d ", ar[i][j]); | |
| } | |
| cout << "\n"; | |
| } | |
| int mainDiagSum = 0; | |
| int secDiagSum = 0; | |
| for (int i = 0; i < M; i++) { | |
| mainDiagSum += ar[i][i]; | |
| secDiagSum += ar[i][M - 1 - i]; | |
| } | |
| cout << "\nСума елементів на головній діагоналі: " << mainDiagSum << "\n"; | |
| cout << "Сума елементів на побічній діагоналі: " << secDiagSum << "\n"; | |
| } | |
| // 3. Дано двовимірний масив розмірністю M x N, заповнений випадковими числами з діапазону від -10 до 10. Визначити кількість додатних, від’ємних і нульових елементів. | |
| #include <iostream> | |
| #include <windows.h> | |
| #include <algorithm> | |
| #include <iomanip> | |
| using namespace std; | |
| int main() | |
| { | |
| SetConsoleOutputCP(1251); | |
| const int M = 4; | |
| const int N = 5; | |
| int ar[M][N]{}; | |
| for (int i = 0; i < M; i++) { | |
| for (int j = 0; j < N; j++) { | |
| ar[i][j] = rand() % 21 - 10; | |
| printf("%3d ", ar[i][j]); | |
| } | |
| cout << "\n"; | |
| } | |
| int posCount = 0, negCount = 0, zeroCount = 0; | |
| for (int i = 0; i < M; i++) { | |
| for (int j = 0; j < N; j++) { | |
| if (ar[i][j] > 0) posCount++; | |
| else if (ar[i][j] < 0) negCount++; | |
| else zeroCount++; | |
| } | |
| } | |
| cout << "\nКількість додатних елементів: " << posCount << "\n"; | |
| cout << "Кількість від'ємних елементів: " << negCount << "\n"; | |
| cout << "Кількість нульових елементів: " << zeroCount << "\n"; | |
| } | |
| // Проект "Лабіринт" | |
| // 1. Якщо персонаж дійшов до виходу з лабіринту в нижньому правому куті, гра завершується перемогою (вивести діалог із повідомленням: "Перемога – знайдено вихід"). | |
| #include <windows.h> | |
| #include <iostream> | |
| #include <ctime> | |
| #include <conio.h> | |
| using namespace std; | |
| int main() | |
| { | |
| srand(time(0)); | |
| system("mode con cols=210 lines=70"); | |
| system("title Maze"); | |
| HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); | |
| CONSOLE_CURSOR_INFO cci; | |
| cci.bVisible = false; | |
| cci.dwSize = 100; | |
| SetConsoleCursorInfo(h, &cci); | |
| const int width = 75; | |
| const int height = 20; | |
| int maze[height][width]; | |
| enum maze_objects { HALL, WALL, GOLD, VRAG }; | |
| enum direction { DOWN = 80, UP = 72, LEFT = 75, RIGHT = 77 }; | |
| enum colors { BLUE = 9, RED = 12, YELLOW = 14, DARKGREEN = 2 }; | |
| for (int y = 0; y < height; y++) | |
| { | |
| for (int x = 0; x < width; x++) | |
| { | |
| int random = rand() % 4; | |
| maze[y][x] = random; | |
| if (x == 0 || y == 0 || x == width - 1 || y == height - 1) | |
| maze[y][x] = WALL; | |
| if (x == 0 && y == 2 || x == 1 && y == 2 || x == width - 1 && y == height - 3) | |
| maze[y][x] = HALL; | |
| if (maze[y][x] == WALL) | |
| { | |
| SetConsoleTextAttribute(h, DARKGREEN); | |
| cout << (char)178; | |
| } | |
| else | |
| { | |
| cout << " "; | |
| } | |
| } | |
| cout << "\n"; | |
| } | |
| COORD pers = { 1, 2 }; | |
| SetConsoleCursorPosition(h, pers); | |
| SetConsoleTextAttribute(h, BLUE); | |
| cout << (char)1; | |
| while (true) | |
| { | |
| int direct = _getch(); | |
| if (direct == 224) | |
| direct = _getch(); | |
| SetConsoleCursorPosition(h, pers); | |
| cout << " "; | |
| if (direct == RIGHT && maze[pers.Y][pers.X + 1] != WALL) | |
| { | |
| pers.X++; | |
| } | |
| else if (direct == LEFT && maze[pers.Y][pers.X - 1] != WALL) | |
| { | |
| pers.X--; | |
| } | |
| else if (direct == UP && maze[pers.Y - 1][pers.X] != WALL) | |
| { | |
| pers.Y--; | |
| } | |
| else if (direct == DOWN && maze[pers.Y + 1][pers.X] != WALL) | |
| { | |
| pers.Y++; | |
| } | |
| if (pers.X == width - 1 && pers.Y == height - 3) | |
| { | |
| cout << "\nПеремога – знайдено вихід!"; | |
| break; | |
| } | |
| SetConsoleCursorPosition(h, pers); | |
| SetConsoleTextAttribute(h, BLUE); | |
| cout << (char)1; | |
| } | |
| } | |
| //2. Якщо всі монетки лабіринту зібрані, гра завершується перемогою (вивести діалог із повідомленням: "Перемога – монети зібрано"). | |
| #include <windows.h> | |
| #include <iostream> | |
| #include <ctime> | |
| #include <conio.h> | |
| using namespace std; | |
| int main() | |
| { | |
| srand(time(0)); | |
| system("mode con cols=210 lines=70"); | |
| system("title Maze"); | |
| HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); | |
| CONSOLE_CURSOR_INFO cci; | |
| cci.bVisible = false; | |
| cci.dwSize = 100; | |
| SetConsoleCursorInfo(h, &cci); | |
| const int width = 75; | |
| const int height = 20; | |
| int maze[height][width]; | |
| int coinsCollected = 0; | |
| const int totalCoins = 10; | |
| enum maze_objects { HALL, WALL, GOLD, VRAG }; | |
| enum direction { DOWN = 80, UP = 72, LEFT = 75, RIGHT = 77 }; | |
| enum colors { BLUE = 9, RED = 12, YELLOW = 14, DARKGREEN = 2 }; | |
| for (int y = 0; y < height; y++) | |
| { | |
| for (int x = 0; x < width; x++) | |
| { | |
| int random = rand() % 4; | |
| maze[y][x] = random; | |
| if (x == 0 || y == 0 || x == width - 1 || y == height - 1) | |
| maze[y][x] = WALL; | |
| if (x == 0 && y == 2 || x == 1 && y == 2 || x == width - 1 && y == height - 3) | |
| maze[y][x] = HALL; | |
| if (random == GOLD && rand() % 10 < 3) // Монети з'являються з ймовірністю 30% | |
| maze[y][x] = GOLD; | |
| } | |
| } | |
| COORD pers = { 1, 2 }; | |
| SetConsoleCursorPosition(h, pers); | |
| SetConsoleTextAttribute(h, BLUE); | |
| cout << (char)1; | |
| while (true) | |
| { | |
| int direct = _getch(); | |
| if (direct == 224) | |
| direct = _getch(); | |
| SetConsoleCursorPosition(h, pers); | |
| cout << " "; | |
| if (direct == RIGHT && maze[pers.Y][pers.X + 1] != WALL) | |
| { | |
| pers.X++; | |
| } | |
| else if (direct == LEFT && maze[pers.Y][pers.X - 1] != WALL) | |
| { | |
| pers.X--; | |
| } | |
| else if (direct == UP && maze[pers.Y - 1][pers.X] != WALL) | |
| { | |
| pers.Y--; | |
| } | |
| else if (direct == DOWN && maze[pers.Y + 1][pers.X] != WALL) | |
| { | |
| pers.Y++; | |
| } | |
| if (maze[pers.Y][pers.X] == GOLD) | |
| { | |
| coinsCollected++; | |
| maze[pers.Y][pers.X] = HALL; | |
| } | |
| if (coinsCollected == totalCoins) | |
| { | |
| cout << "\nПеремога – монети зібрано!"; | |
| break; | |
| } | |
| if (pers.X == width - 1 && pers.Y == height - 3) | |
| { | |
| cout << "\nПеремога – знайдено вихід!"; | |
| break; | |
| } | |
| SetConsoleCursorPosition(h, pers); | |
| SetConsoleTextAttribute(h, BLUE); | |
| cout << (char)1; | |
| } | |
| } | |
| //3. Додати новий тип об'єктів лабіринту – "ліки", які при збиранні відновлюють здоров'я на 25 очок. Здоров'я персонажа не може бути більше 100 очок. Якщо здоров'я на максимумі, ліки не можна зібрати. Якщо здоров'я закінчилося (впало до 0), гра завершується поразкою (вивести діалог із повідомленням: "Поразка – здоров'я закінчилося"). | |
| #include <windows.h> | |
| #include <iostream> | |
| #include <ctime> | |
| #include <conio.h> | |
| using namespace std; | |
| int main() | |
| { | |
| srand(time(0)); | |
| system("mode con cols=210 lines=70"); | |
| system("title Maze"); | |
| HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); | |
| CONSOLE_CURSOR_INFO cci; | |
| cci.bVisible = false; | |
| cci.dwSize = 100; | |
| SetConsoleCursorInfo(h, &cci); | |
| const int width = 75; | |
| const int height = 20; | |
| int maze[height][width]; | |
| int health = 100; | |
| int coinsCollected = 0; | |
| const int totalCoins = 10; | |
| enum maze_objects { HALL, WALL, GOLD, VRAG, MEDICINE }; | |
| enum direction { DOWN = 80, UP = 72, LEFT = 75, RIGHT = 77 }; | |
| enum colors { BLUE = 9, RED = 12, YELLOW = 14, DARKGREEN = 2, GREEN = 10 }; | |
| for (int y = 0; y < height; y++) | |
| { | |
| for (int x = 0; x < width; x++) | |
| { | |
| int random = rand() % 5; | |
| maze[y][x] = random; | |
| if (x == 0 || y == 0 || x == width - 1 || y == height - 1) | |
| maze[y][x] = WALL; | |
| if (x == 0 && y == 2 || x == 1 && y == 2 || x == width - 1 && y == height - 3) | |
| maze[y][x] = HALL; | |
| if (random == GOLD && rand() % 10 < 3) | |
| maze[y][x] = GOLD; | |
| if (random == MEDICINE && rand() % 10 < 3) | |
| maze[y][x] = MEDICINE; | |
| } | |
| } | |
| COORD pers = { 1, 2 }; | |
| SetConsoleCursorPosition(h, pers); | |
| SetConsoleTextAttribute(h, BLUE); | |
| cout << (char)1; | |
| while (true) | |
| { | |
| int direct = _getch(); | |
| if (direct == 224) | |
| direct = _getch(); | |
| SetConsoleCursorPosition(h, pers); | |
| cout << " "; | |
| if (direct == RIGHT && maze[pers.Y][pers.X + 1] != WALL) | |
| { | |
| pers.X++; | |
| } | |
| else if (direct == LEFT && maze[pers.Y][pers.X - 1] != WALL) | |
| { | |
| pers.X--; | |
| } | |
| else if (direct == UP && maze[pers.Y - 1][pers.X] != WALL) | |
| { | |
| pers.Y--; | |
| } | |
| else if (direct == DOWN && maze[pers.Y + 1][pers.X] != WALL) | |
| { | |
| pers.Y++; | |
| } | |
| if (maze[pers.Y][pers.X] == GOLD) | |
| { | |
| coinsCollected++; | |
| maze[pers.Y][pers.X] = HALL; | |
| } | |
| if (maze[pers.Y][pers.X] == MEDICINE) | |
| { | |
| if (health < 100) | |
| { | |
| health += 25; | |
| if (health > 100) health = 100; | |
| maze[pers.Y][pers.X] = HALL; | |
| } | |
| } | |
| if (health <= 0) | |
| { | |
| cout << "\nПоразка – здоров'я закінчилося!"; | |
| break; | |
| } | |
| if (coinsCollected == totalCoins) | |
| { | |
| cout << "\nПеремога – монети зібрано!"; | |
| break; | |
| } | |
| if (pers.X == width - 1 && pers.Y == height - 3) | |
| { | |
| cout << "\nПеремога – знайдено вихід!"; | |
| break; | |
| } | |
| SetConsoleCursorPosition(h, pers); | |
| SetConsoleTextAttribute(h, BLUE); | |
| cout << (char)1; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment