'Merge 2 c++ functions [closed]

So I have two codes, one to swap the diagonals of a matrix, and the second is to square root of the moved numbers.

How can I merge these two codes together so that I have an output matrix with interchanged diagonals and squareddiagonals at the same time?

This is the first code - swaps diagonals

#include<bits/stdc++.h>
using namespace std;

#define N 3

// Function to interchange diagonals
void interchangeDiagonals(int array[][N])
{
    // swap elements of diagonal
    for (int i = 0; i < N; ++i)
    if (i != N / 2)
    swap(array[i][i], array[i][N - i - 1]);

    for (int i = 0; i < N; ++i)
    {
    for (int j = 0; j < N; ++j)
            cout<<" "<< array[i][j];
    cout<<endl;
    }
}

int main()
{
    int array[N][N] = {1, 2, 3,
                    4, 5, 6,
                    7, 8, 9};
    interchangeDiagonals(array);
    return 0;
}

This is the second code - squares the diagonals

// Simple CPP program to print squares of
// diagonal elements.
#include <iostream>
using namespace std;

#define MAX 100

// function of diagonal square
void diagonalsquare(int mat[][MAX], int row,
                                int column)
{
    // This loop is for finding square of first
    // diagonal elements
    cout << "Diagonal one : ";
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < column; j++)

            // if this condition will become true
            // then we will get diagonal element
            if (i == j)

                // printing square of diagonal element
                cout << mat[i][j] * mat[i][j] << " ";
    }

    // This loop is for finding square of second
    // side of diagonal elements
    cout << " \n\nDiagonal two : ";
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < column; j++)

            // if this condition will become true
            // then we will get second side diagonal
            // element
            if (i + j == column - 1)

                // printing square of diagonal element
                cout << mat[i][j] * mat[i][j] << " ";
    }
}

// Driver code
int main()
{
    int mat[][MAX] = { { 1, 2, 3 },
                    { 4, 5, 6 },
                    { 7, 8, 9 } };
    diagonalsquare(mat, 3, 3);
    return 0;
}
c++


Solution 1:[1]

Declare a new function where both functions are called on the same matrix.

Additionally, using namespace std; and #define [const name] (value) are bad practice. Use std::[object/function name] and constexpr [const name] (value);.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cannon