Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <time.h>
- #include <locale.h>
- #include <fstream>
- #include <string>
- using namespace std;
- int NumberElementN(int N)
- {
- cout << "Количество cтрок (не более 10): ";
- cin >> N;
- cout << endl;
- if (N <= 0 || N > 10)
- {
- cout << "Количество cтрок не должно быть меньше 1 или больше 10" << endl;
- return -1;
- }
- else
- return N;
- }
- int NumberElementM(int M)
- {
- cout << "Количество столбцов (не более 10): ";
- cin >> M;
- cout << endl;
- if (M <= 0 || M > 10)
- {
- cout << "Количество столбцов не должно быть меньше 1 или больше 10" << endl;
- return -1;
- }
- else
- return M;
- }
- void GetArray(int N, int M, int** mas, int a, int b)
- {
- for (int i = 0; i < N; i++)
- for (int j = 0; j < M; j++)
- mas[i][j] = a + rand() % (b - a + 1);
- }
- void PutTwoDismensArray(int N, int M, int** mas)
- {
- for (int i = 0; i < N; i++)
- {
- for (int j = 0; j < M; j++)
- {
- if (mas[i][j] < -9)
- cout << mas[i][j] << " ";
- if (mas[i][j] >= -9 && mas[i][j] < 0)
- cout << " " << mas[i][j] << " ";
- if (mas[i][j] > 0 && mas[i][j] < 9)
- cout << " " << mas[i][j] << " ";
- if (mas[i][j] > 9)
- cout << " " << mas[i][j] << " ";
- }
- cout << endl;
- }
- }
- void InOneDimensional(int** mas, int* mas1, int n, int m)
- {
- int k = 0;
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++)
- {
- mas1[k] = mas[i][j];
- k++;
- }
- }
- void InlDismMinElements(int** mas, int* mas1, int N, int M, int row, int column, char value)
- {
- switch (value)
- {
- case 1:
- for (size_t i = 0; i < M; i++)
- mas1[i] = mas[row - 1][i];
- break;
- case 2:
- for (size_t i = 0; i < N; i++)
- mas1[i] = mas[i][column - 1];
- break;
- case 3:
- for (int i = 0; i < N; i++)
- for (int j = 0; j < M; j++)
- if (i == j)
- mas1[i] = mas[i][j];
- break;
- case 4:
- for (int i = 0; i < N; i++)
- for (int j = 0; j < M; j++)
- {
- if (M >= N)
- if (i + j == N - 1)
- mas1[i] = mas[i][j];
- else {}
- else
- if (i + j == M - 1)
- mas1[i] = mas[i][j];
- }
- break;
- }
- }
- void InTwoDimensional(int** mas, int* mas1, int n, int m)
- {
- int k = 0;
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++)
- {
- mas[i][j] = mas1[k];
- k++;
- }
- }
- void PutOneDismensArray(int mas[], const int n, const int m)
- {
- cout << endl;
- for (int i = 0; i < (n * m); i++)
- cout << mas[i] << " ";
- cout << endl;
- }
- void SumElements(int** mas, int N, int M, int row, int column, int SumRow, int SumColumn, long int MultyRow, long int MultyColumn)
- {
- MultyRow = 1; MultyColumn = 1;
- SumRow = 0; SumColumn = 0;
- for (int i = 0; i < M; i++)
- {
- SumRow += mas[row - 1][i];
- MultyRow *= mas[row - 1][i];
- }
- for (int i = 0; i < N; i++)
- {
- SumColumn += mas[i][column - 1];
- MultyColumn *= mas[i][column - 1];
- }
- cout << "Сумма строки " << row << " равна " << SumRow << " -------- Произведение строки равна => " << MultyRow << endl;
- cout << "Сумма столбца " << column << " равна " << SumColumn << " -------- Произведение столбца => " << MultyColumn << endl;
- }
- void SumElementsDiagonal(int** mas, int N, int M, int SumMainDiagonal, int SumSecondaryDiagonal, long int MultyMain, long int MultySecond)
- {
- MultyMain = 1; MultySecond = 1;
- SumMainDiagonal = 0; SumSecondaryDiagonal = 0;
- for (int i = 0; i < N; i++)
- for (int j = 0; j < M; j++)
- {
- if (i == j)
- {
- SumMainDiagonal += mas[i][j];
- MultyMain *= mas[i][j];
- }
- if (M >= N)
- if (i + j == N - 1)
- {
- SumSecondaryDiagonal += mas[i][j];
- MultySecond *= mas[i][j];
- }
- else {}
- else
- if (i + j == M - 1)
- {
- SumSecondaryDiagonal += mas[i][j];
- MultySecond *= mas[i][j];
- }
- }
- cout << "Сумма главной диагонали равна " << SumMainDiagonal << " ---- Произведение главной => " << MultyMain << endl;
- cout << "Сумма побочной строки равна " << SumSecondaryDiagonal << " ----- Произведение побочной => " << MultySecond << endl;
- }
- void MaxElement(int mas[], int n)
- {
- int indexMax = 0;
- int max = mas[0];
- for (int i = 1; i < n; i++)
- if (mas[i] > max)
- {
- max = mas[i];
- indexMax = i;
- }
- cout << "Максимальный элемент " << max << endl;
- }
- void MaxAndMinElementDiagonal(int mas[], int m, int n)
- {
- int indexMax = 0;
- int max = mas[0];
- int indexMin = 0;
- int min = mas[0];
- if (m >= n)
- for (int i = 1; i < n; i++)
- if (mas[i] > max)
- {
- max = mas[i];
- indexMax = i;
- }
- else {}
- else {
- for (int i = 1; i < m; i++)
- if (mas[i] < min)
- {
- min = mas[i];
- indexMin = i;
- }
- else {}
- }
- cout << "Максимальный элемент " << max << endl;
- cout << "Минимальный элемент " << min << endl;
- }
- void MinElement(int mas[], int n)
- {
- int indexMin = 0;
- int min = mas[0];
- for (int i = 1; i < n; i++)
- if (mas[i] < min)
- min = mas[i];
- cout << "Минимальный элемент " << min << endl;
- }
- void bubbleSort(int mas[], int n)
- {
- int tmp = 0;
- for (int i = 0; i < n; i++)
- {
- for (int j = (n - 1); j >= (i + 1); j--)
- {
- if (mas[j] < mas[j - 1])
- {
- tmp = mas[j];
- mas[j] = mas[j - 1];
- mas[j - 1] = tmp;
- }
- }
- }
- }
- void searchFunct(int** mas, int n, int m)
- {
- int key, verify = 0, number; char value;
- cout << "Введите элемент для поиска: ";
- cin >> key;
- cout << "Выберите условие поиска\n> Весь массив - введите число '1'\n> Относительно строки - '2'\n";
- cout << "> Относительно столбца - '3'\n> Относительно главной диагонали - '4'\n> Относительно побочной диагонали - '5'\n";
- cout << "> Относительно верхнего треугольника - '6'\n> Относительно нижнего треугольника - '7'\n";
- cin >> value;
- switch (value)
- {
- case '1':
- for (int i = 0; i < m; i++)
- for (int j = 0; j < n; j++)
- if (mas[i][j] == key)
- {
- cout << "Элемент был найден в " << i + 1 << " строке, " << j + 1 << " столбце" << endl;
- verify++;
- }
- break;
- case '2':
- cout << "Введите номер строки: "; cin >> number;
- if (number < 1 || number > m)
- {
- cout << "Этой строки нет";
- break;
- }
- for (int i = 0; i < m; i++)
- if (mas[number - 1][i] == key)
- {
- cout << "Элемент был найден в " << i + 1 << " столбце" << endl;
- verify++;
- }
- break;
- case '3':
- cout << "Введите номер столбца: "; cin >> number;
- if (number < 1 || number > n)
- {
- cout << "Этого столбца нет";
- break;
- }
- for (int i = 0; i < n; i++)
- if (mas[i][number - 1] == key)
- {
- cout << "Элемент был найден в " << i + 1 << " строке" << endl;
- verify++;
- }
- break;
- case '4':
- for (int i = 0; i < m; i++)
- for (int j = 0; j < n; j++)
- if (i == j)
- if (mas[i][j] == key)
- {
- cout << "Элемент был найден в " << i + 1 << " строке, " << j + 1 << " столбце" << endl;
- verify++;
- }
- break;
- case '5':
- for (int i = 0; i < m; i++)
- for (int j = 0; j < n; j++)
- if (n >= m)
- if (i + j == n - 1)
- if (mas[i][j] == key)
- {
- cout << "Элемент был найден в " << i + 1 << " строке, " << j + 1 << " столбце" << endl;
- verify++;
- }
- else {}
- else
- if (i + j == m - 1)
- if (mas[i][j] == key)
- {
- cout << "Элемент был найден в " << i + 1 << " строке, " << j + 1 << " столбце" << endl;
- verify++;
- }
- break;
- case '6':
- for (int i = 0; i < m; i++)
- for (int j = 0; j < n; j++)
- if (i >= j)
- if (mas[j][i] == key)
- {
- cout << "Элемент был найден в " << j + 1 << " строке, " << i + 1 << " столбце" << endl;
- verify++;
- }
- break;
- case '7':
- for (int i = 0; i < m; i++)
- for (int j = 0; j < n; j++)
- if (j >= i)
- if (mas[j][i] == key)
- {
- cout << "Элемент был найден в " << j + 1 << " строке, " << i + 1 << " столбце" << endl;
- verify++;
- }
- break;
- }
- if (verify < 1)
- {
- cout << "Элемент не был найден" << endl;
- }
- }
- void countTriangle(int n, int m)
- {
- int i, j;
- int upper_count = 0;
- int lower_count = 0;
- for (i = 0; i < m; i++)
- for (j = 0; j < n; j++)
- if (i >= j)
- upper_count++;
- printf("Верхняя сумма равна %d\n", upper_count);
- for (i = 0; i < m; i++)
- for (j = 0; j < n; j++)
- if (j >= i)
- lower_count++;
- printf("Нижняя сумма равна %d", lower_count);
- }
- void sortRow(int** Matrix, int n, int m)
- {
- int row;
- cout << "Сортируемая строка: "; cin >> row;
- int tmp = 0;
- for (int i = 0; i < m; i++)
- {
- for (int j = (m - 1); j >= (i + 1); j--)
- {
- if (Matrix[row - 1][j] < Matrix[row - 1][j - 1])
- {
- tmp = Matrix[row - 1][j];
- Matrix[row - 1][j] = Matrix[row - 1][j - 1];
- Matrix[row - 1][j - 1] = tmp;
- }
- }
- }
- }
- void sortColumn(int** Matrix, int n, int m)
- {
- int column;
- cout << "Сортируемый столбец: "; cin >> column;
- int tmp = 0;
- for (int i = 0; i < n; i++)
- {
- for (int j = (n - 1); j >= (i + 1); j--)
- {
- if (Matrix[j][column - 1] < Matrix[j - 1][column - 1])
- {
- tmp = Matrix[j][column - 1];
- Matrix[j][column - 1] = Matrix[j - 1][column - 1];
- Matrix[j - 1][column - 1] = tmp;
- }
- }
- }
- }
- void transposition(int** Matrix, const int n, const int m)
- {
- int** arr = new int* [m];
- for (int i = 0; i < m; i++)
- arr[i] = new int[n];
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++)
- arr[j][i] = Matrix[i][j];
- cout << endl;
- for (int i = 0; i < m; i++)
- {
- for (int j = 0; j < n; j++)
- {
- if (arr[i][j] < -9)
- cout << arr[i][j] << " ";
- if (arr[i][j] >= -9 && arr[i][j] < 0)
- cout << " " << arr[i][j] << " ";
- if (arr[i][j] > 0 && arr[i][j] < 9)
- cout << " " << arr[i][j] << " ";
- if (arr[i][j] > 9)
- cout << " " << arr[i][j] << " ";
- }
- cout << endl;
- }
- for (int i = 0; i < m; i++)
- delete[] arr[i];
- delete[] arr;
- }
- int** multyMatrix(int** Matrix, const int n, const int m)
- {
- int number;
- cout << "Введите число для умножение матрицы на число\n";
- cin >> number;
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++)
- {
- Matrix[i][j] *= number;
- }
- return Matrix;
- }
- void firstColumn(int** Matrix, int n, int m)
- {
- int result(m + 1);
- for (int i(0); i < n; i++)
- {
- for (int j(0); j < m; j++)
- {
- if (Matrix[i][j] == 0)
- if (j < result)
- result = j;
- }
- }
- if (result < m + 1)
- cout << "Номер первого столбца содержащего нулевой элемент: " << result + 1 << endl;
- else
- cout << "Нулевой элемент в массиве не найден\n";
- }
- void descendingSort(int* Arr, const int n)
- {
- int tmp = 0;
- for (int i = 0; i < n; i++)
- {
- for (int j = (n - 1); j >= (i + 1); j--)
- {
- if (Arr[j] > Arr[j - 1])
- {
- tmp = Arr[j];
- Arr[j] = Arr[j - 1];
- Arr[j - 1] = tmp;
- }
- }
- }
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- int n = 0, m = 0; string value;
- int selectRow = 0, selectColumn = 0;
- n = NumberElementN(n);
- if (n == -1)
- return 0;
- m = NumberElementM(m);
- if (m == -1)
- return 0;
- int** Matrix = new int* [n];
- for (int i = 0; i < n; i++)
- Matrix[i] = new int[m];
- GetArray(n, m, Matrix, -100, 100);
- PutTwoDismensArray(n, m, Matrix);
- int* Array = new int[n * m];
- cout << "\nХотите преобразовать матрицу в одномерный массив?\n1) Да\n2) Нет\n" << endl;
- cin >> value;
- InOneDimensional(Matrix, Array, n, m);
- if (value == "1")
- {
- PutOneDismensArray(Array, n, m);
- cout << endl;
- }
- cout << "Хотите отсортировать матрицу?\n1) Да\n2) Нет\n" << endl;
- cin >> value;
- cout << endl;
- if (value == "1")
- {
- cout << "Всю матрицу?\n1) Да\n2) Только строку\n3) Только столбец\n" << endl;
- cin >> value;
- if (value == "2")
- sortRow(Matrix, n, m);
- else
- if (value == "3")
- sortColumn(Matrix, n, m);
- else {
- bubbleSort(Array, n * m);
- InTwoDimensional(Matrix, Array, n, m);
- }
- cout << "Отсортированная матрица: " << endl;
- PutTwoDismensArray(n, m, Matrix);
- cout << endl;
- }
- cout << "Выберите строку: "; cin >> selectRow;
- if (selectRow <= 1 || selectRow >= n)
- {
- cout << "Этой строки нет\n" << endl;
- }
- long int MultyRow = 1, MultyColumn = 1, MultyMain = 1, MultySecond = 1;
- int SumRow = 0, SumColumn = 0, SumMainDiagonal = 0, SumSecondaryDiagonal = 0;
- cout << "Выберите столбец: "; cin >> selectColumn;
- if (selectColumn >= 1 || selectColumn <= m)
- {
- SumElements(Matrix, n, m, selectRow, selectColumn, SumRow, SumColumn, MultyRow, MultyColumn);
- cout << "Сумма строки " << selectRow << " равна ";
- cout << SumRow << " , Произведение строки равна = " << MultyRow << endl;
- cout << "Сумма столбца " << selectColumn << " = ";
- cout << SumColumn << " , Произведение столбца = " << MultyColumn << endl;
- }
- else {
- cout << "Этого столбца нет\n" << endl;
- }
- SumElementsDiagonal(Matrix, n, m, SumMainDiagonal, SumSecondaryDiagonal, MultyMain, MultySecond);
- cout << "Сумма главной диагонали = " << SumMainDiagonal << " , Произведение главной = " << MultyMain << endl;
- cout << "Сумма побочной строки = " << SumSecondaryDiagonal << " , Произведение побочной = " << MultySecond << endl;
- cout << "\nХотите найти минимальный и максимальный элемент?\n1) Да\n2) Нет\n";
- cin >> value;
- InOneDimensional(Matrix, Array, n, m);
- if (value == "1")
- {
- int num = 0;
- if (m >= n)
- num = m;
- else
- num = n;
- int* selectedRowOrColumn = new int[num];
- cout << "Выберите строку: "; cin >> selectRow;
- cout << "Выберите столбец: "; cin >> selectColumn;
- cout << "\n\n";
- cout << "-< Матрица >-" << endl;
- MinElement(Array, n * m);
- MaxElement(Array, n * m);
- cout << "-< В строке " << selectRow << " >- " << endl;
- InlDismMinElements(Matrix, selectedRowOrColumn, n, m, selectRow, selectColumn, 1);
- MaxElement(selectedRowOrColumn, m);
- MinElement(selectedRowOrColumn, m); cout << endl;
- cout << "-< В столбце " << selectColumn << " >- " << endl;
- InlDismMinElements(Matrix, selectedRowOrColumn, n, m, selectRow, selectColumn, 2);
- MaxElement(selectedRowOrColumn, n);
- MinElement(selectedRowOrColumn, n); cout << endl;
- cout << "-< В главной диагонали >-" << endl;
- InlDismMinElements(Matrix, selectedRowOrColumn, n, m, selectRow, selectColumn, 3);
- MaxAndMinElementDiagonal(selectedRowOrColumn, n, m); cout << endl;
- cout << "-< В побочной диагонали >-" << endl;
- InlDismMinElements(Matrix, selectedRowOrColumn, n, m, selectRow, selectColumn, 4);
- MaxAndMinElementDiagonal(selectedRowOrColumn, n, m);
- }
- cout << "\nХотите найти элемент в массиве?\n1) Да\n2) Нет\n";
- cin >> value;
- if (value == "1")
- searchFunct(Matrix, n, m);
- countTriangle(n, m);
- cout << "\nХотите транспонировать матрицу?\n1) Да\n2) Нет\n";
- cin >> value;
- if (value == "1")
- transposition(Matrix, n, m);
- cout << "\nХотите умножить матрицу на число?\n1) Да\n2) Нет\n";
- cin >> value;
- if (value == "1")
- {
- Matrix = multyMatrix(Matrix, n, m);
- PutTwoDismensArray(n, m, Matrix);
- }
- cout << "Поиск нулевого элемента:" << endl;
- firstColumn(Matrix, n, m); cout << endl;
- InOneDimensional(Matrix, Array, n, m);
- descendingSort(Array, n * m);
- InTwoDimensional(Matrix, Array, n, m);
- PutTwoDismensArray(n, m, Matrix);
- }
Advertisement
Add Comment
Please, Sign In to add comment