Trying to create two functions (one to swap columns and one to swap rows) of matrices in one-dimensional arrays. Obviously, they'd be structurally similar.
void col_swap(int a[], int col1, int col2, int colSize, int rowSize) {
int d[size];
int space = col2 - col1;
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++) {
if (i == (col1 - 1) || i == (j * (col1-1))) {
d[i+space] = a[i];
} else if (i == (col2 - 1) || i == (j * (col2-1))) {
d[i-space] = a[i];
} else {
d[i] = a[i];
}
}
printf("%d ", d[i]);
if ((i+1) % colSize == 0) {
printf("\n");
}
}
}
Yes, the matrix must be in a one-dimensional array. This doesn't work fully either.
EDIT: COL1 and COL2 are not "the first column of matrix" and "the second column of matrix", respectively. They are any two columns of a matrix that we want to switch.
void row_swap(int a[], int row1, int row2, int rowSize, int colSize) {
for (int i = 0; i < colSize; i++) {
int temp = a[i*rowSize+row1];
a[i*rowSize+row1] = a[i*rowSize+row2];
a[i*rowSize+row2] = temp;
}
for (int i = 0; i < size; i++) {
if (i % colSize == 0) {
printf("\n");
}
printf("%d ", a[i]);
}
}
I have the row_swap function as above but when I give it a matrix
1, 4,
2, 3,
3, 2,
4, 1
It returns
1, 3
2, 4
3, 1
4, 2