Last active
September 9, 2024 07:58
-
-
Save Darklega228/3f7ae28c70efc5661c0e9fea7786cfbb to your computer and use it in GitHub Desktop.
This file contains 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
#include <iostream> | |
#include <vector> | |
using namespace std; | |
/*Задание 8*/ | |
void fillSpiralMatrix(int M) | |
{ | |
vector<vector<int>> matrix(M, vector<int>(M, 0)); | |
int num = 1; | |
int x = M / 2, y = M / 2; | |
matrix[x][y] = num++; | |
int step = 1; | |
while (step < M) | |
{ | |
// Лево | |
for (int i = 0; i < step; i++) matrix[--x][y] = num++; | |
// Вверх | |
for (int i = 0; i < step; i++) matrix[x][++y] = num++; | |
step++; | |
// Право | |
for (int i = 0; i < step; i++) matrix[++x][y] = num++; | |
// Вниз | |
for (int i = 0; i < step; i++) matrix[x][--y] = num++; | |
step++; | |
} | |
for (int i = 0; i < step - 1; i++) matrix[--x][y] = num++; | |
for (const auto& row : matrix) | |
{ | |
for (int elem : row) | |
{ | |
cout << elem << "\t"; | |
} | |
cout << endl; | |
} | |
} | |
int main() | |
{ | |
setlocale(0, ""); | |
/*Задание 1*/ | |
/*const int M = 5, N = 6; | |
int array[M][N]; | |
srand(time(0)); | |
for (int i = 0; i < M; i++) | |
{ | |
for (int j = 0; j < N; j++) | |
{ | |
array[i][j] = rand() % 21; | |
} | |
} | |
int totalSum = 0, minElement = array[0][0], maxElement = array[0][0]; | |
for (int i = 0; i < M; i++) | |
{ | |
for (int j = 0; j < N; j++) | |
{ | |
totalSum += array[i][j]; | |
if (array[i][j] < minElement) minElement = array[i][j]; | |
if (array[i][j] > maxElement) maxElement = array[i][j]; | |
} | |
} | |
double average = static_cast<double>(totalSum) / (M * N); | |
cout << "Сумма всех элементов: " << totalSum << endl; | |
cout << "Среднее арифметическое: " << average << endl; | |
cout << "Минимальный элемент: " << minElement << endl; | |
cout << "Максимальный элемент: " << maxElement << endl; | |
return 0;*/ | |
/*Задание 2*/ | |
/*const int M = 5; | |
int array[M][M]; | |
srand(time(0)); | |
for (int i = 0; i < M; i++) | |
{ | |
for (int j = 0; j < M; j++) | |
{ | |
array[i][j] = rand() % 21; | |
} | |
} | |
int mainDiagSum = 0, secondaryDiagSum = 0; | |
for (int i = 0; i < M; i++) | |
{ | |
mainDiagSum += array[i][i]; | |
secondaryDiagSum += array[i][M - i - 1]; | |
} | |
cout << "Сумма главной диагонали: " << mainDiagSum << endl; | |
cout << "Сумма побочной диагонали: " << secondaryDiagSum << endl; | |
return 0;*/ | |
/*Задание 3*/ | |
/*const int M = 4, N = 5; | |
int array[M][N]; | |
srand(time(0)); | |
for (int i = 0; i < M; i++) | |
{ | |
for (int j = 0; j < N; j++) | |
{ | |
array[i][j] = rand() % 21 - 10; | |
} | |
} | |
int positiveCount = 0, negativeCount = 0, zeroCount = 0; | |
for (int i = 0; i < M; i++) | |
{ | |
for (int j = 0; j < N; j++) | |
{ | |
if (array[i][j] > 0) positiveCount++; | |
else if (array[i][j] < 0) negativeCount++; | |
else zeroCount++; | |
} | |
} | |
cout << "Количество положительных элементов: " << positiveCount << endl; | |
cout << "Количество отрицательных элементов: " << negativeCount << endl; | |
cout << "Количество нулевых элементов: " << zeroCount << endl; | |
return 0;*/ | |
/*Задание 4*/ | |
/*const int M = 4, N = 5; | |
int array[M][N]; | |
srand(time(0)); | |
for (int i = 0; i < M; i++) { | |
for (int j = 0; j < N; j++) { | |
array[i][j] = rand() % 21; | |
} | |
} | |
cout << "Суммы по строкам:" << endl; | |
for (int i = 0; i < M; i++) { | |
int rowSum = 0; | |
for (int j = 0; j < N; j++) { | |
rowSum += array[i][j]; | |
} | |
cout << "Строка " << i + 1 << ": " << rowSum << endl; | |
} | |
cout << "Суммы по столбцам:" << endl; | |
for (int j = 0; j < N; j++) { | |
int colSum = 0; | |
for (int i = 0; i < M; i++) { | |
colSum += array[i][j]; | |
} | |
cout << "Столбец " << j + 1 << ": " << colSum << endl; | |
} | |
return 0;*/ | |
/*Задание 5*/ | |
/*const int M = 4, N = 5; | |
int array[M][N]; | |
for (int i = 0; i < M; i++) | |
{ | |
for (int j = 0; j < N; j++) | |
{ | |
array[i][j] = (i + 1) * 10 + (j + 1); | |
} | |
} | |
cout << "Массив:" << endl; | |
for (int i = 0; i < M; i++) | |
{ | |
for (int j = 0; j < N; j++) | |
{ | |
cout << array[i][j] << "\t"; | |
} | |
cout << endl; | |
} | |
return 0;*/ | |
/*Задание 6*/ | |
/*const int M = 6, N = 5; | |
int array[M][N]; | |
srand(time(0)); | |
for (int i = 0; i < M; i++) | |
{ | |
for (int j = 0; j < N; j++) | |
{ | |
array[i][j] = rand() % 21; | |
} | |
} | |
cout << "Исходный массив:" << endl; | |
for (int i = 0; i < M; i++) | |
{ | |
for (int j = 0; j < N; j++) | |
{ | |
cout << array[i][j] << "\t"; | |
} | |
cout << endl; | |
} | |
for (int i = 0; i < M - 1; i += 2) | |
{ | |
for (int j = 0; j < N; j++) | |
{ | |
swap(array[i], array[i + 1]); | |
} | |
} | |
cout << "\nМассив после замены:" << endl; | |
for (int i = 0; i < M; i++) | |
{ | |
for (int j = 0; j < N; j++) | |
{ | |
cout << array[i][j] << "\t"; | |
} | |
cout << endl; | |
} | |
return 0;*/ | |
/*Задание 7*/ | |
/*const int M = 4, N = 5; | |
int array[M][N]; | |
srand(time(0)); | |
for (int i = 0; i < M; i++) | |
{ | |
for (int j = 0; j < N; j++) | |
{ | |
array[i][j] = rand() % 201 - 100; | |
} | |
} | |
int minVal = array[0][0], maxVal = array[0][0]; | |
int minIdx = 0, maxIdx = 0; | |
for (int i = 0; i < M; i++) | |
{ | |
for (int j = 0; j < N; j++) | |
{ | |
if (array[i][j] < minVal) | |
{ | |
minVal = array[i][j]; | |
minIdx = i * N + j; | |
} | |
if (array[i][j] > maxVal) | |
{ | |
maxVal = array[i][j]; | |
maxIdx = i * N + j; | |
} | |
} | |
} | |
int start = min(minIdx, maxIdx); | |
int end = max(minIdx, maxIdx); | |
int sumBetween = 0; | |
for (int k = start + 1; k < end; k++) | |
{ | |
sumBetween += array[k / N][k % N]; | |
} | |
cout << "Минимальный элемент: " << minVal << ", Максимальный элемент: " << maxVal << endl; | |
cout << "Сумма элементов между минимальным и максимальным: " << sumBetween << endl; | |
return 0;*/ | |
/*Задание 8*/ | |
int M = 5; | |
fillSpiralMatrix(M); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment