我想對矩陣進行轉置 當column=row和column>row時,代碼可以作業。 但當行>列時,我得到了錯誤的答案
。所有的代碼 :
#include <stdio.h>
#include <stdlib.h>
int main() {int row, column, tmp;
scanf("%d",&row) 。
scanf("%d",&列)。
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" , target[i*column j])。
printf("
")。)
}}
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
34
56
輸出(我從我的代碼中得到的東西)。
1 3 5
5 4 6
輸出(我應該得到什么)。
1 3 5
2 4 6
輸入作業:
12 3
4 5 6
輸出:
1 4
2 5
3 6
uj5u.com熱心網友回復:
正如@Bob_建議的那樣,你錯誤地計算了進入目標陣列的偏移量。
你的源影像有column列和row行;但轉置后的影像是row列和column行!
你的源影像有column列和row行。(順便說一下--這是個糟糕的識別符號選擇;考慮將num_columns和num_rows作為轉置函式的引數)。因此,我相信你需要有:
target[j * row i] = image[i * column j];
在你的內回圈中。
uj5u.com熱心網友回復:
為了了解你在做什么,我建議你列印你的演算法中計算的索引值。
問題是i可以大于column(j和row也是如此)。你必須使用%來改進你的代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/322474.html
標籤:
