0% found this document useful (0 votes)
5 views5 pages

Matrix Operations: Rotate, Zigzag, Mirror

Uploaded by

homelander495
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Matrix Operations: Rotate, Zigzag, Mirror

Uploaded by

homelander495
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

// Matrix rotate

#include <iostream>
#include <vector>

// Function to rotate a square matrix clockwise


std::vector<std::vector<int>> rotateMatrixClockwise(const std::vector<std::vector<int>>& matrix) {
int N = [Link]();
std::vector<std::vector<int>> rotated(N, std::vector<int>(N, 0));

for (int i = 0; i < N; ++i) {


for (int j = 0; j < N; ++j) {
rotated[j][N - 1 - i] = matrix[i][j];
}
}

return rotated;
}

// Function to print a matrix


void printMatrix(const std::vector<std::vector<int>>& matrix) {
int N = [Link]();
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}
}

int main() {
int N;
std::cout << "Enter the size of the square matrix: ";
std::cin >> N;

std::vector<std::vector<int>> matrix(N, std::vector<int>(N, 0));

std::cout << "Enter the elements of the matrix:" << std::endl;


for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
std::cin >> matrix[i][j];
}
}

std::vector<std::vector<int>> rotatedMatrix = rotateMatrixClockwise(matrix);

std::cout << "Original matrix:" << std::endl;


printMatrix(matrix);

std::cout << "Rotated matrix (clockwise):" << std::endl;


printMatrix(rotatedMatrix);

return 0;
}
Zig-Zag

#include <iostream>
#include <vector>

// Function to print matrix elements in zigzag manner


void printMatrixZigzag(const std::vector<std::vector<int>>& matrix) {
int rows = [Link]();
int cols = matrix[0].size();

for (int i = 0; i < rows; ++i) {


if (i % 2 == 0) {
// Even rows: left to right
for (int j = 0; j < cols; ++j) {
std::cout << matrix[i][j] << " ";
}
} else {
// Odd rows: right to left
for (int j = cols - 1; j >= 0; --j) {
std::cout << matrix[i][j] << " ";
}
}
}
}

int main() {
int rows, cols;
std::cout << "Enter the number of rows and columns: ";
std::cin >> rows >> cols;

std::vector<std::vector<int>> matrix(rows, std::vector<int>(cols, 0));

std::cout << "Enter the elements of the matrix:" << std::endl;


for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
std::cin >> matrix[i][j];
}
}

std::cout << "Matrix elements in zigzag manner: ";


printMatrixZigzag(matrix);
std::cout << std::endl;

return 0;
}
-------------------------------------------******************************-------------------------------------------
Scalar multiplication
#include <iostream>
#include <vector>

// Function to perform scalar multiplication of a matrix


std::vector<std::vector<int>> scalarMultiplyMatrix(const std::vector<std::vector<int>>& matrix, int
scalar) {
int rows = [Link]();
int cols = matrix[0].size();

std::vector<std::vector<int>> result(rows, std::vector<int>(cols, 0));

for (int i = 0; i < rows; ++i) {


for (int j = 0; j < cols; ++j) {
result[i][j] = matrix[i][j] * scalar;
}
}

return result;
}

// Function to print a matrix


void printMatrix(const std::vector<std::vector<int>>& matrix) {
int rows = [Link]();
int cols = matrix[0].size();

for (int i = 0; i < rows; ++i) {


for (int j = 0; j < cols; ++j) {
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}
}

int main() {
int rows, cols, scalar;

std::cout << "Enter the number of rows and columns of the matrix: ";
std::cin >> rows >> cols;

std::cout << "Enter the elements of the matrix:" << std::endl;


std::vector<std::vector<int>> matrix(rows, std::vector<int>(cols, 0));
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
std::cin >> matrix[i][j];
}
}

std::cout << "Enter the scalar value for multiplication: ";


std::cin >> scalar;

std::vector<std::vector<int>> result = scalarMultiplyMatrix(matrix, scalar);

std::cout << "Resultant matrix after scalar multiplication:" << std::endl;


printMatrix(result);
return 0;
}

--------------------------------***********************************---------------------------------

Mirror
#include <iostream>
#include <vector>

// Function to display the mirror of a matrix across its diagonal


void mirrorMatrixDiagonal(const std::vector<std::vector<int>>& matrix) {
int rows = [Link]();
int cols = matrix[0].size();

if (rows != cols) {
std::cout << "Matrix must be square to find its diagonal mirror." << std::endl;
return;
}

std::vector<std::vector<int>> mirroredMatrix(rows, std::vector<int>(cols, 0));

for (int i = 0; i < rows; ++i) {


for (int j = 0; j < cols; ++j) {
mirroredMatrix[i][j] = matrix[j][i]; // Swap rows and columns
}
}

std::cout << "Mirror of the matrix across its diagonal:" << std::endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
std::cout << mirroredMatrix[i][j] << " ";
}
std::cout << std::endl;
}
}

int main() {
int rows, cols;

std::cout << "Enter the number of rows and columns of the matrix: ";
std::cin >> rows >> cols;

std::vector<std::vector<int>> matrix(rows, std::vector<int>(cols, 0));

std::cout << "Enter the elements of the matrix:" << std::endl;


for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
std::cin >> matrix[i][j];
}
}

mirrorMatrixDiagonal(matrix);

return 0;
}

You might also like