Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <string>
- using namespace std;
- int main() {
- const int size = 3;
- cout.setf(ios::fixed);
- cout.precision(0);
- srand(time(NULL));
- setlocale(LC_ALL, "ru");
- int i, j, temp;
- const int ROW = 6;
- const int COL = 6;
- float a[ROW][COL];
- float b[3][6];
- float c[size][size];
- int ans;
- cout << "Нажмите 1, если Вы хотите заполнить матрицу самостоятельно" <<
- " и 2, если требуется автоматическое её заполнение:";
- cin >> ans;
- if (ans == 1 || ans == 2)
- ;
- else {
- while (ans != 1 && ans != 2 || cin.fail()) {
- cin.clear();
- cin.ignore();
- cout << "Извините, Вы ввели некорректное значение. Повторите попытку:" << endl;
- cin >> ans;
- }
- }
- if (ans == 1) {
- for (int i = 0; i < ROW; i++) {
- for (int j = 0; j < COL; j++) {
- cout << "Пожалуйста, введите значения элемента матрицы. Число должно быть больше 0 и меньше 99 " << endl;
- cin >> a[i][j];
- if (a[i][j] > 0 && a[i][j] < 99) {
- }
- else {
- while (a[i][j] < 0 || a[i][j] > 99 || cin.fail()) {
- cin.clear();
- cin.ignore();
- cout << "\nВы ввели некорректное значение. Повторите попытку: ";
- cin >> a[i][j];
- }
- }
- }
- }
- for (int i = 0; i < ROW; i++) {
- cout << endl;
- for (int j = 0; j < COL; j++) {
- cout << a[i][j] << " ";
- }
- }
- }
- else if (ans == 2) {
- for (int i = 0; i < ROW; i++) {
- int randNum = rand() % 100;
- for (int j = 0; j < COL; j++) {
- a[i][j] = rand() % 100;
- }
- }
- for (int i = 0; i < ROW; i++) {
- cout << endl;
- for (int j = 0; j < COL; j++) {
- cout << a[i][j] << " ";
- }
- }
- }
- cout << endl;
- cout << endl;
- cout << endl;
- for (int j = 0; j < 6; j++) {
- b[0][j] = a[0][j] / a[1][j];
- if (a[1][j] == 0) {
- b[0][j] = 0;
- }
- }
- for (int j = 0; j < 6; j++) {
- b[1][j] = a[2][j] / a[3][j];
- if (a[3][j] == 0) {
- b[1][j] = 0;
- }
- }
- for (int j = 0; j < 6; j++) {
- b[2][j] = a[4][j] / a[5][j];
- if (a[5][j] == 0) {
- b[2][j] = 0;
- }
- }
- cout << "Матрица, полученная делением четных строк на нечетные: " << endl;
- for (int i = 0; i < size; i++)
- {
- cout << endl;
- for (int j = 0; j < 6; j++) {
- cout << b[i][j] << " ";
- }
- }
- cout << endl << endl;
- for (int i = 0; i < size; i++) {
- c[i][0] = b[i][0] / b[i][1];
- if (b[i][1] == 0) {
- c[i][0] = 0;
- }
- }
- for (int i = 0; i < 3; i++) {
- c[i][1] = b[i][2] / b[i][3];
- if (b[i][3] == 0) {
- c[i][1] = 0;
- }
- }
- for (int i = 0; i < size; i++) {
- c[i][2] = b[i][4] / b[i][5];
- if (b[i][5] == 0) {
- c[i][2] = 0;
- }
- }
- cout << "Матрица, полученная делением четных столбцов на нечетные: " << endl;
- for (int i = 0; i < size; i++) {
- cout << endl;
- for (int j = 0; j < size; j++) {
- cout << c[i][j] << " ";
- }
- }
- cout << endl << endl;
- for (int m = 0; m < (size * size - 1); m++) //сдвиги очередных элементов в правильную позицию
- /*сдвиг элемента массива в правильную позицию*/
- for (int i = 0; i < size; i++) {
- for (int j = 0; j < size; j++) {
- /*АНАЛИЗ НА ПОСЛЕДНИЙ ЭЛЕМЕНТ МАССИВА*/
- if (i == size - 1 && j == size - 1) { //Если строка последняя и справа тупик, то ничего не делаем
- continue;
- }
- /*КОНЕЦ АНАЛИЗА НА ПОСЛЕДНЮЮ СТРОКУ*/
- if (c[i][j] > c[i][j + 1]) { //Если элемент не на своей позиции
- temp = c[i][j]; //Обмен местами
- c[i][j] = c[i][j + 1];
- c[i][j + 1] = temp;
- }
- }
- }
- swap(c[0][1], c[1][0]); //три пары элементов массива
- swap(c[0][2], c[2][0]);
- swap(c[1][2], c[2][1]);
- cout << "Конечная матрица:\n\n";
- for (int i = 0; i < size; i++) {
- cout << endl;
- for (int j = 0; j < size; j++) {
- cout << c[i][j] << " ";
- }
- }
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement