2011-12-06 8 views
0

私は2次行列(ポインタの2次元動的配列)を持ち、行/列の順序を変更する必要があります。マトリックスが非常に大きいので、すべての要素をコピーするのではなくポインタを変更することに決めました。私は計算を指定する別の配列も持っています。 行の順列は次のように指定します。 4,3,2,1 - 最初の行は4番目の行、2番目の行は3番目の列などでなければなりません。 同じ状況が列にあります。 http://xmages.net/storage/10/1/0/8/6/thumb/thumb_40561a35.jpg行/列順列

どのように行の順序(ポインタの順列)の変更のようなアルゴリズムを実装するには?ここは私のバージョンですが、動作しません。ポインタをコピーしたいのですが、要素の代わりに要素がコピーされ、セグメント化エラーが表示されます。私はアドレスを取得するために「&」を追加すると 、コンパイラは構文エラーであることを言う:

orderOfRows[i] = &auxMatrix[computation[i]]; 

ここで、それは私のコードです:

static int N = 6; 
static int **orderOfRows; 
int **sourceMatrix; 
int **auxMatrix; 

int main() { 
int* computation = (int*)malloc(N*sizeof(int)); 

computation[0] = 1; 
computation[1] = 6; 
computation[2] = 3; 
computation[3] = 7; 
computation[4] = 4; 
computation[5] = 2; 

} 
printf("After computation has been done: \n"); 
changeRowOrder(computation); 

void changeRowOrder(int *computation) { 
int i; 
// change rows order and dopy them into a temp array 
for(i = 0; i < N; ++i) { 
    // static arrays 
    orderOfRows[i] = auxMatrix[computation[i]]; 
} 
// recover matrix 
for(i = 0; i < N; ++i) { 
    auxMatrix[i] = orderOfRows[i]; 
} 


void allocate2dMemory() { 

int i = 0; 
sourceMatrix = (int**)malloc(N * sizeof(int *)); 

if(sourceMatrix == NULL) { 
    fprintf(stderr, "out of memory\n"); 
    exit(2); 
} 

for(i = 0; i < N; i++) { 
    sourceMatrix[i] = (int*)malloc(N * sizeof(int)); 
    if(sourceMatrix[i] == NULL) { 
     fprintf(stderr, "out of memory\n"); 
     exit(2); 
    } 
} 

auxMatrix = (int**)malloc(N * sizeof(int *)); 

if(auxMatrix == NULL) { 
    fprintf(stderr, "out of memory\n"); 
    exit(2); 
} 

for(i = 0; i < N; i++) { 
    auxMatrix[i] = (int*)malloc(N * sizeof(int)); 
    if(auxMatrix[i] == NULL) { 
     fprintf(stderr, "out of memory\n"); 
     exit(2); 
    } 
} 

orderOfRows = (int**)malloc(N * sizeof(int *)); 

if(orderOfRows == NULL) { 
    fprintf(stderr, "out of memory\n"); 
    exit(2); 
} 

for(i = 0; i < N; i++) { 
    orderOfRows[i] = (int*)malloc(N * sizeof(int)); 
    if(orderOfRows[i] == NULL) { 
     fprintf(stderr, "out of memory\n"); 
     exit(2); 
    } 
} 

} 
} 

私は2 * N(コピーポインタを過ごすことになりますその後、N * Nの代わりに回復操作を実行します。 そして私は別の質問があります。このアイデアを使ってどのように列の並べ替えをすることができますか?それが不可能な場合は、どのように私は列の順列を行うことができますが、行列のすべての要素をコピーすることはできません?あなたはcomputation[1] = 6を持っている場合

、それがあることを意味:プログラミング言語は、あなたが

for (int j = 0; j < N; ++j) 
{ 
    orderOfRows[i][j] = auxMatrix[computation[i]][j]; 
} 

そしてもう一つが持つべき唯一のC.

答えて

1

代わりの

orderOfRows[i] = &auxMatrix[computation[i]]; 

ですauxMatrix[computation[i]]を実行すると、auxMatrix[6]にアクセスしようとします。 auxMatrixのサイズは6x6であるため、しないでください。

+0

考えていただきありがとうございますが、ラインの声は(i = 0; i user565447

1

あなたはコピーせずに、行と列の両方を交換することができるようにしたい場合は、次の2つの補助指標持っている必要がありますしようとしている。

int *row_index; 
int *col_index; 

をし、これらを使用して、行列の要素にアクセス:

element = matrix[row_index[row]][col_index[col]]; 

行または列をスワップするには、row_indexまたはcol_indexの対応する要素を交換するだけです。

+0

お願いします。例を挙げてください。どのようにして列の順序を変更できますか。私は行の順序をどのように変えることができるのか理解しています。私は上で説明しました(画像を参照)。列の順序を変更する操作は想像できません。なぜなら、各列の先頭にポインタがないからです。 – user565447