Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <iomanip>
- using namespace std;
- void input(int matr[10][10], int &n, int &m) {
- cout << "Введите размеры матрицы." << endl;
- cin >> n >> m;
- cout << "Введите элементы матрицы." << endl;
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++) {
- cin >> matr[i][j];
- }
- }
- void change(int matr[10][10], int n, int m) {
- int x1=-1,y1=-1,x2=-1,y2=-1,c, check1 = 1, check2 = 1;
- for (int i=0;i<n;++i)
- for (int j=0;j<m;++j)
- if (check1 && matr[i][j] % 2 == 0) {
- x1 = i;
- y1 = j;
- check1 = 0;
- }
- for (int i=n-1;i>=0;i--) {
- for (int j = m-1; j >= 0; j--) {
- if (check2 && matr[i][j] == 0) {
- x2 = i;
- y2 = j;
- check2 = 0;
- }
- }
- }
- matr[x2][y2] = matr[x1][y1];
- matr[x1][y1] = 0;
- }
- void output(int matr[10][10], int n, int m){
- for (int i=0;i<n;i++){
- for (int j=0;j<m;j++){
- cout.width(5);
- cout << matr[i][j] << " "; }
- cout << endl;
- }
- }
- int main(){
- int matr[10][10], n=0, m=0, p=0, z=0;
- setlocale(LC_ALL,".1251");
- input(matr,n,m);
- cout << "Исходная матрица:\n";
- for (int i=0;i<n;i++){
- for (int j=0;j<m;j++)
- cout << matr[i][j] << " ";
- cout << endl;
- }
- for (int i=0;i<n;i++)
- for (int j=0;j<m;j++) {
- if (matr[i][j] % 2 == 0)
- p++;
- if (matr[i][j] == 0) {
- z++;
- }
- }
- if (p>0 && z>0){
- change(matr,n,m);
- cout << "Измененная матрица:" << endl;
- output(matr,n,m);
- } else
- cout << "Матрица не изменилась." << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement