Загрузка...

ЛР "Багатовимірні масиви "

Тема в разделе C/C++ создана пользователем Cycling228 22 ноя 2019. 160 просмотров

Загрузка...
  1. Cycling228
    Cycling228 Автор темы 22 ноя 2019 2 9 ноя 2019
    Даны 2 целочисленные матрицы размером n * m. Написать программу, которая формирует двумерный массив по следующему правилу: нечетные строки массива 1 и четные строки массива 2.

    Две матрицы создал , а дальше хз! Хелпаните пж.

    Код

    #include<iostream>
    #include "windows.h"
    using namespace std;

    int main()
    {
    SetConsoleCP(1251);
    SetConsoleOutputCP(1251);
    int i,j,n, m, Matr[10][10], Matr2[10][10];
    cout << "Введіть кількість рядків:";
    cin >> n;
    cout << "Введіть кількість стовбців:" ;
    cin >> m;
    cout << "Введіть значення матриці 1:";
    for (i = 0; i < n; i++)
    for (j = 0; j < m; j++)
    cin >> Matr[i][j];
    cout << "Matr \n";
    for (i = 0; i < n; i++)
    {
    for (j = 0; j < m; j++)
    cout << Matr[i][j] << "\t";
    cout << endl;
    }
    cout << "Введіть значення матриці 2:";
    for (i = 0; i < n; i++)
    for (j = 0; j < m; j++)
    cin >> Matr2[i][j];
    cout << "Matr2 \n";
    for (i = 0; i < n; i++)
    {
    for (j = 0; j < m; j++)
    cout << Matr2[i][j] << "\t";
    cout << endl;
    }
    system("pause");
    return 0;


    }
     
    22 ноя 2019 Изменено
  2. ketch_inactive2790559
    ketch_inactive2790559 24 ноя 2019 Заблокирован(а) 80 21 ноя 2019
    Если тебе надо больше двух, сначала проси юзера ввести необходимое кол-во матриц, а потом циклом перебирай и создавай оставляя так же просьбу с вводом значения матрицы.
    Сорри, если что-то не так понял, я Эльфийский не знаю.
     
  3. Jus1x_tv
    Jus1x_tv 25 ноя 2019 6 14 ноя 2019
    Что за парные строки и не парные строки? Дай пояснение
     
  4. Cycling228
    Cycling228 Автор темы 25 ноя 2019 2 9 ноя 2019
    нечетные строки массива 1 и четные массива 2 должны занестись в массив 3
    --- Сообщение объединено с предыдущим 25 ноя 2019
    Спасибо за ответ! И да , перевел с Эльфийского на "ЛЮДСКОЙ" язык !
     
  5. ketch_inactive2790559
    ketch_inactive2790559 25 ноя 2019 Заблокирован(а) 80 21 ноя 2019
    Обращайся
     
  6. Jus1x_tv
    Jus1x_tv 25 ноя 2019 6 14 ноя 2019
    Код

    #include <iostream>
    #include <locale>
    #include <iomanip>

    using namespace std;

    int main()
    {
    setlocale(LC_CTYPE, "Russian");
    short *FirstMatrix, *SecondMatrix, *ResultMatrix;
    short Rows, Columns;
    // initialization
    cout << "Введите кол-во строк в матрице: "; cin >> Rows;
    cout << "Введите кол-во столбцов в матрице: "; cin >> Columns;
    FirstMatrix = new short[Rows * Columns];
    SecondMatrix = new short[Rows * Columns];
    ResultMatrix = new short[Rows * Columns];
    srand((unsigned)time(NULL));
    // first matrix
    cout << endl << "First Matrix" << endl;
    for (short i = 0; i < Rows; i++)
    {
    for (short j = 0; j < Columns; j++)
    {
    *(FirstMatrix + i * Columns + j) = rand() % 21 - 10;
    cout << setw(3) << *(FirstMatrix + i * Columns + j) << " ";
    }
    cout << endl;
    }
    //second matrix
    cout << endl << "Second Matrix" << endl;
    for (short i = 0; i < Rows; i++)
    {
    for (short j = 0; j < Columns; j++)
    {
    *(SecondMatrix + i * Columns + j) = rand() % 21 - 10;
    cout << setw(3) << *(SecondMatrix + i * Columns + j) << " ";
    }
    cout << endl;
    }

    // Fill ResultMatrix
    for (short i = 0; i < Rows; i += 2)
    {
    for (short j = 0; j < Columns; j++)
    {
    *(ResultMatrix + i * Columns + j) = *(FirstMatrix + i * Columns + j);
    }
    }
    for (short i = 1; i < Rows; i += 2)
    {
    for (short j = 0; j < Columns; j++)
    {
    *(ResultMatrix + i * Columns + j) = *(SecondMatrix + i * Columns + j);
    }
    }
    // Result
    cout << "Result: " << endl;
    for (short i = 0; i < Rows; i++)
    {
    for (short j = 0; j < Columns; j++)
    {
    cout << setw(3) << *(ResultMatrix + i * Columns + j) << " ";
    }
    cout << endl;
    }
    // del matrixs Освобождение динамической памяти
    delete[] FirstMatrix;
    delete[] SecondMatrix;
    delete[] ResultMatrix;
    }
     
  7. Cycling228
    Cycling228 Автор темы 25 ноя 2019 2 9 ноя 2019
    Jus1x_tv, Друг, спасибо большое !!
     
Top