Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to rotate a matrix 90 degrees clockwise in C

2 Answers

0 votes
#include <stdio.h>

#define N 3 // Matrix size

void rotate90Clockwise(int matrix[N][N]) {
    // Step 1: Transpose the matrix
    for (int i = 0; i < N; i++) {
        for (int j = i; j < N; j++) {
            int temp = matrix[i][j];
            matrix[i][j] = matrix[j][i];
            matrix[j][i] = temp;
        }
    }

    // Step 2: Reverse each row
    for (int i = 0; i < N; i++) {
        for (int j = 0, k = N - 1; j < k; j++, k--) {
            int temp = matrix[i][j];
            matrix[i][j] = matrix[i][k];
            matrix[i][k] = temp;
        }
    }
}

// Function to print the matrix
void printMatrix(int matrix[N][N]) {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

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

    printf("Original Matrix:\n");
    printMatrix(matrix);

    rotate90Clockwise(matrix);

    printf("\nRotated Matrix:\n");
    printMatrix(matrix);

    return 0;
}



/*
run:

Original Matrix:
1 2 3 
4 5 6 
7 8 9 

Rotated Matrix:
7 4 1 
8 5 2 
9 6 3

*/

 



answered May 29, 2025 by avibootz
0 votes
#include <stdio.h>

#define ROWS 3  // Original matrix rows
#define COLS 4  // Original matrix columns

void rotate90Clockwise(int matrix[ROWS][COLS], int rotated[COLS][ROWS]) {
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            rotated[j][ROWS - 1 - i] = matrix[i][j]; // Mapping to rotated position
        }
    }
}

// Function to print a matrix
void printMatrix(int rows, int cols, int matrix[][ROWS]) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int matrix[ROWS][COLS] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    };

    int rotated[COLS][ROWS]; // New rotated matrix

    rotate90Clockwise(matrix, rotated);

    printf("\nRotated Matrix:\n");
    printMatrix(COLS, ROWS, rotated);

    return 0;
}



/*
run:

Rotated Matrix:
9 5 1 
10 6 2 
11 7 3 
12 8 4 

*/

 



answered May 29, 2025 by avibootz

Related questions

2 answers 133 views
2 answers 136 views
2 answers 146 views
2 answers 159 views
2 answers 126 views
2 answers 126 views
...