50173. Matrix Operations

I'm a slow walker, but I never walk backwards.

Problem Description

Write functions to rotate and transpose an 8 by 8 matrix.

10000001
00000000
00000100
00000000
00000000
00000000
01000000
10000100

We can represent this matrix with a C99 uint_64, which has 64 bits, where the least significant 8 bits are the first row of the matrix, and so on, until the most significant 8 bits are the last row of the matrix.

lowbit                                                   highbit
0         1         2         3         4         5         6
0123456789012345678901234567890123456789012345678901234567890123
1000000100000000000001000000000000000000000000000100000010000100

Now given a matrix as a C99 unsigned 64 bit integer, you need to implement the following functions.

  • void printMatrix(uint64_t* matrix);
    Print a matrix and its relative uint_64 as indicated above.

  • void rotateMatrix(uint64_t* matrix);
    Rotate a matrix clockwise. Then the matrix will like this.

    10000001
    01000000
    00000000
    00000000
    00000000
    10000100
    00000000
    00000001
  • void transposeMatrix(uint64_t *matrix); Transpose a matrix. Then the matrix will like this.

    10000001
    00000010
    00000000
    00000000
    00000000
    00100001
    00000000
    10000000

Source codes

main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include "matrixOperations.h"
#include<stdint.h>
 
int main() {
   uint64_t num;
   char operation;
   scanf("%lu", &num);
   while (1) {
       scanf("%c", &operation);
       if (operation == 'p') {
           printMatrix(&num);
           break;
       } else if (operation == 'r')
           rotateMatrix(&num);
       else if (operation == 't')
           transposeMatrix(&num);
   }
   return 0;
}

matrixOperations.h

1
2
3
4
#include<stdint.h>
void printMatrix(uint64_t *matrix);
void rotateMatrix(uint64_t *matrix);
void transposeMatrix(uint64_t *matrix);

Inputs Format

In the input files, the first line is the initial value of the matrix. The following lines are operations. r is for rotateMatrix, t is for transposeMatrix and p is for printMatrix.

Outputs Format

There are nine lines in the output. The first line is the uint64_t value of the matrix and the last eight lines are the matrix.

Sample Input 1

2378463553207140481
r
p

Sample Output 1

9223408320738493057
10000001
01000000
00000000
00000000
00000000
10000100
00000000
00000001

Sample Input 2

2378463553207140481
t
p

Sample Output 2

72202729572810881
10000001
00000010
00000000
00000000
00000000
00100001
00000000
10000000

Sample Input 3

2378463553207140481
r
t
p

Sample Output 3

9295464815264793121
10000100
01000000
00000000
00000000
00000000
00000100
00000000
10000001

Discussion