-
Notifications
You must be signed in to change notification settings - Fork 2
/
find transpose of a matrix.c
89 lines (74 loc) · 1.7 KB
/
find transpose of a matrix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// A utility function to print an array
void printArray (int arr[], int n)
{
for (int i = 0; i < n; i++){
if(i==0)
printf("[%d,",arr[i]);
else if(i==n-1)
printf("%d]",arr[i]);
else
printf("%d,",arr[i]);
}
printf("\n");
}
void Swap(int* a,int* b){
int temp= *a;
*a=*b;
*b=temp;
}
int random_randint(int from,int to){
int x = ((unsigned int)time(NULL));
srand((unsigned int)x);
return (rand()%((to-from)+1)+from);
}
void Shuffle_array(int arr[],int n){
for(int i=n-1;i>0;i--){
int j=random_randint(0,i);
Swap(&arr[i],&arr[j]);
}
}
void Shuffle_Matrix(int row,int colum,int arr[row][colum]){
for(int i=row-1;i>0;i--){
for (int j=colum-1; j>=0; j--) {
int r=random_randint(0,i);
int c=random_randint(0,i);
Swap(&arr[i][j],&arr[r][c]);
}
}
}
void print_Matrix(int r,int c,int arr[r][c])
{
int i, j;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
}
void transpose(int r,int c,int A[r][c],int B[c][r]){
int i, j;
for (i = 0; i < c; i++)
for (j = 0; j < r; j++)
B[i][j] = A[j][i];
}
int main(void) {
int disp[2][4] = {
{10, 11, 12, 13},
{14, 15, 16, 17}
};
int new_m[4][2];
transpose(2,4,disp,new_m);
print_Matrix(2,4,disp);
printf("\n");
int x = ((unsigned int)time(NULL));
srand((unsigned int)x);
Shuffle_Matrix(2,4,disp);
print_Matrix(4,2,new_m);
printf("\n");
}