Code Generator | C/C++

Symmetrical Columns Finder in Matrices

This C++ solution identifies columns in two matrices that form palindromic sequences, compares the count of these columns, and outputs the results. It includes functions for input, palindrome checking, and result display, ensuring modular


Empty image or helper icon

Prompt

Дано: прямоугольные матрицы А[N,M] B[N,M]. Определить номера столбцов, все 
элементы которых образуют симметричные последовательности (палиндромы) и выяснить в которой матрице больше симметричных последовательностей. Проверку каждого 
столбца на то, является ли он палиндромом, оформить как функцию. Ввод и вывод элементов матрицы оформить как функцию.

Answer

C/C++ Solution for Identifying Symmetrical Columns in Matrices

Overview

This solution will define the necessary functions to:

  1. Input two rectangular matrices (A and B) of size N x M.
  2. Check each column in both matrices to determine if all its elements form a palindrome.
  3. Identify and output the indices of the columns that contain palindromic sequences.
  4. Compare the number of palindromic columns between the two matrices and indicate which matrix has more.

Code Structure

  1. Function to Input Matrices: Handles reading elements for matrices.
  2. Function to Check for Palindrome: Determines if a given column is a palindrome.
  3. Main Function: Coordinates the overall execution: input, processing, and output.

Code Implementation

#include 
#include 

using namespace std;

/**
 * Function to input the matrix elements.
 *
 * @param matrix The matrix to fill with elements.
 * @param rows The number of rows in the matrix.
 * @param cols The number of columns in the matrix.
 */
void inputMatrix(vector>& matrix, int rows, int cols) {
    cout << "Enter elements for the matrix (" << rows << "x" << cols << "):" << endl;
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            cin >> matrix[i][j];
        }
    }
}

/**
 * Function to check if a column is a palindrome.
 *
 * @param matrix The matrix containing the column.
 * @param colIndex The index of the column to check.
 * @param rows The number of rows in the matrix.
 * @return true if the column is a palindrome, false otherwise.
 */
bool isPalindromeColumn(const vector>& matrix, int colIndex, int rows) {
    for (int i = 0; i < rows / 2; ++i) {
        if (matrix[i][colIndex] != matrix[rows - 1 - i][colIndex]) {
            return false; // Not a palindrome if any opposite elements differ
        }
    }
    return true; // It's a palindrome
}

/**
 * Main function to execute the program logic.
 */
int main() {
    int N, M;
    
    cout << "Enter number of rows (N) and columns (M): ";
    cin >> N >> M;

    vector> matrixA(N, vector(M));
    vector> matrixB(N, vector(M));

    // Input matrices A and B
    inputMatrix(matrixA, N, M);
    inputMatrix(matrixB, N, M);

    vector palinColsA, palinColsB;

    // Check for palindromic columns in matrix A
    for (int j = 0; j < M; ++j) {
        if (isPalindromeColumn(matrixA, j, N)) {
            palinColsA.push_back(j);
        }
    }

    // Check for palindromic columns in matrix B
    for (int j = 0; j < M; ++j) {
        if (isPalindromeColumn(matrixB, j, N)) {
            palinColsB.push_back(j);
        }
    }

    // Output results
    cout << "Matrix A palindrome columns: ";
    for (int col : palinColsA) {
        cout << col << " ";
    }
    cout << endl;

    cout << "Matrix B palindrome columns: ";
    for (int col : palinColsB) {
        cout << col << " ";
    }
    cout << endl;

    // Compare number of palindromic columns
    if (palinColsA.size() > palinColsB.size()) {
        cout << "Matrix A has more palindromic columns." << endl;
    } else if (palinColsB.size() > palinColsA.size()) {
        cout << "Matrix B has more palindromic columns." << endl;
    } else {
        cout << "Both matrices have the same number of palindromic columns." << endl;
    }

    return 0;
}

Code Usage Example

  1. Compile the code using a C++ compiler.
  2. Run the executable.
  3. Input the size of the matrices (N and M) followed by the elements.

Sample Input

Enter number of rows (N) and columns (M): 3 3
Enter elements for the matrix (3x3):
1 2 1
4 5 4
1 2 1
Enter elements for the matrix (3x3):
3 2 3
6 7 8
3 2 3

Sample Output

Matrix A palindrome columns: 0 2 
Matrix B palindrome columns: 0 2 
Both matrices have the same number of palindromic columns.

Conclusion

This solution effectively handles input, checks for palindromic columns, and compares two matrices according to the requirements. The structure ensures that the code is modular and easy to maintain, adhering to best practices in software engineering.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This C++ solution identifies columns in two matrices that form palindromic sequences, compares the count of these columns, and outputs the results. It includes functions for input, palindrome checking, and result display, ensuring modular code structure.