【发布时间】:2021-11-13 22:32:39
【问题描述】:
我想转置矩阵 代码在 column=row 和 column>row 时有效 但是当行>列时我得到错误的答案
所有代码:
#include <stdio.h>
#include <stdlib.h>
int main() {int row,column,tmp;
scanf("%d",&row);
scanf("%d",&column);
int *image=(int*)malloc(sizeof(int)*row*column);
int *target=(int*)malloc(sizeof(int)*row*column);
for(int i=0;i<row;i++){
for(int j=0;j<column;j++){
scanf("%d",&tmp);
image[i*column+j]=target[i*column+j]=tmp;
}
}
transpose(row,column,target,image);
for (int i = 0; i< m; i++) {
for (int j = 0; j < n; j++)
printf("%d\t", target[i*column+j]);
printf("\n");
}}
void transpose(int row, int column,int* target,int* image) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
target[j * column + i] = image[i * column + j];
}
}
}
我在图像中的矩阵,我想在目标中转置。
input :
1 2
3 4
5 6
output(what i get from my code) :
1 3 5
5 4 6
output (what should i get ) :
1 3 5
2 4 6
input work :
1 2 3
4 5 6
output :
1 4
2 5
3 6
【问题讨论】:
-
请显示您调用
transpose-function 的代码; -
请edit您的问题并创建一个minimal reproducible example。我建议添加一个
main函数来提供必要的变量,用硬编码的输入值填充数组,调用transpose并打印结果。 -
请注意
target和image有不同的column值(一个应该是row,如果你正在转置),在你用来计算指数的公式中。 -
image和target具有不同的宽度,但您使用相同的column引用它们。 -
图像和目标的宽度相同(m*n)