Untitled

Run Settings
LanguageC++
Language Version
Run Command
#include <iostream> #include <string> #include <ctime> #include <iomanip> using namespace std; int getInteger(const char* text) { bool isError = false; std::string s; int number = 0; do { isError = false; std::cout << text << std::endl; std::cin >> s; for (size_t i = 0; i < s.length(); i++) { if ((int)(s[i]) < 0 || !isdigit(s[i])) //if (!(s[i] >= '0' && s[i] <= '9')) { isError = true; break; } } if (!isError) number = atoi(s.c_str()); } while (isError || number <= 0); return number; } void task907() { cout << "Task 907" << endl; const int rows = 4; const int cols = 5; int summ = 0; int array[rows][cols]; for (int y = 0; y < rows; y++) { for (int x = 0; x < cols; x++) { cout << (array[y][x] = rand() % 5) << " "; } cout << endl; } for (int i = -rows + 1; i < rows; i++) { for (int y = 0; y < rows; y++) { for (int x = 0; x < cols; x++) { if (y + i == x) { summ += array[y][x]; } } } cout << endl << "Summ = " << summ << " : " << i << " diagonal" << endl; summ = 0; } } void task1047() { cout << "Task 1047" << endl; const int ColsRows = 4; int arr[ColsRows][ColsRows]; cout << "Before" << endl; for (int i = 0; i < ColsRows; i++) { for (int j = 0; j < ColsRows; j++) { cout << (arr[i][j] = rand() % 10) << " "; } cout << endl; } cout << endl << endl; for (int y = 0; y < ColsRows; y++) { int temp = arr[y][0]; for (int x = 0; x < ColsRows / 2; x++) { arr[y][0] = arr[y][ColsRows - 2]; } arr[y][ColsRows - 2] = temp; } cout << "After (A)" << endl; for (int i = 0; i < ColsRows; i++) { for (int j = 0; j < ColsRows; j++) { cout << arr[i][j] << " "; } cout << endl; } cout << endl << endl; for (int i = 0; i < ColsRows; i++) { int temp = arr[1][i]; for (int j = 0; j < ColsRows / 2; j++) { arr[1][i] = arr[ColsRows - 1][i]; } arr[ColsRows - 1][i] = temp; } cout << "After (B)" << endl; for (int i = 0; i < ColsRows; i++) { for (int j = 0; j < ColsRows; j++) { cout << arr[i][j] << " "; } cout << endl; } cout << endl << endl; } void task942() { cout << "Task 942" << endl << "Matrix before" << endl; const int row = 6; const int column = 7; int massiv[row][column]; for (int y = 0; y <row; y++) { for (int x = 0; x < column; x++) { cout << (massiv[y][x] = rand() % 10) << " "; } cout << endl; } cout << endl << "After" << endl; for (int y = 0; y < row; y++) { for (int x = 0; x < column; x++) { if (y + x == ( column < row?column : row) - 1) { cout << setw(3) << (massiv[y][x] = 100) << " "; } else { cout << setw(3) << massiv[y][x] << " "; } } cout << endl; } } void task1012() { cout << "Task 1012" << endl; const int size = 4; int arr[size][size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { cout << (arr[i][j] = rand() % 2) << " "; } cout << endl; } bool equil = true; int row = 0; for ( row = 0; row < size; row++) { for (int x = 0; x < size; x++) { if (arr[ row][x] == arr[x][row]) { equil = true; } else { equil = false; break; } } } cout << endl; if (equil) { cout << " Equil k-row and k-column = " << row << endl; } cout << endl; } void task977() { cout << "Task 977" << endl << endl; const int n = 8; int array[n][n] = {}; int count = 1; for (int i = 0; i < n / 2; i++) { for (int y = i; y < n - i; y++) { array[i][y] = count++; } for (int y = i + 1; y < n - i; y++) { array[y][n - 1 - i] = count++; } for (int x = n - 1 - i; x > 0 + i; x--) { array[n - 1 - i][x - 1] = count++; } for (int y = n - 1 - i; y > 1 + i; y--) { array[y - 1][0 + i] = count++; } } if (n % 2 == 1) { array[n / 2][n / 2] = n * n; } for (int y = 0; y < n; y++) { for (int x = 0; x < n; x++) { cout << array[y][x] << "\t"; } cout << endl; } } int main() { srand(time(0)); int c = 0; while (c != 1) { int inputnumber = getInteger("Choose task ( 907, 942, 977, 1012, 1047) "); cout << endl; switch (inputnumber) { case 907: task907(); break; case 942: task942(); break; case 977: task977(); break; case 1012: task1012(); break; case 1047: task1047(); break; } } }
Editor Settings
Theme
Key bindings
Full width
Lines