2013-07-02 2 views
1

나는이 프로그램을 어떤 크기이든 관계없이 행렬을 조 변경해야합니다. 그러나 그것은 예상대로 작동하지 않고 왜 나는 출력을 얻지 못하지만 컴파일 오류는 없지만 {1,2,3}, {4,5,6}의 출력은 { 1,0,0}, {0,0,4}, 나에게 전혀 이해가되지 않습니다. 저는 종이에 여러 번 "메모리 스냅 샷"을 통해 작업 해 왔지만, 제가 누락 된 것을 찾을 수는 없었습니다.이 시점에서 또 다른 눈 세트가 필요합니다. 덕분입니다.누군가가이 프로그램의 문제를 해결할 수 있습니까? (포인터를 사용하여 행렬을 바꿉니다)

#include "stdafx.h" 

#include <fstream> 

#include <iostream> 

#include <iomanip> 

#include <string> 

#include <cmath> 

#include <vector> 

using namespace std; 

void squaretranspose(int &M, int &MT, int ROWS, int COLS); 

int main(void) 
{ 
    int M[2][3]={{1,2,3},{4,5,6}}; 
    int MT[3][2]={0}; 
    int ROWS(2),COLS(3); 

int i,j; 
cout << " The entries of the original matrix " << endl; 
for(i=0;i<=ROWS-1;i++) 
{ 
    for(j=0;j<=COLS-1;j++) 
{ 
cout<<M[i][j]<<"\t"; 
} 
cout << endl; 
} 
squaretranspose(M[0][0],MT[0][0],ROWS,COLS); 
cout << " The entries of the transposed non-square matrix " << endl; 
for(i=0;i<=COLS-1;i++) 
{ 
    cout << endl; 
    for(j=0;j<=ROWS-1;j++) 
{ 
cout<<MT[i][j]<<"\t"; 
} 
} 
system ("PAUSE"); 
return 0; 
} 
void squaretranspose (int &M, int &MT, int ROWS, int COLS) 
{ 
    // declare pointers to change the input matrice's values 
    int *ptr,*ptrT; 
// declare indices for a row by row process 
    int i,j; 
// declare placeholder 2d vectors for swapping the I,j, entries to ,j,i entries 
    vector < vector<int>> temp(ROWS,COLS); 
    vector < vector<int>> tempT(COLS,COLS); 
    vector < vector<int>> temp_T(ROWS,ROWS); 
// set the pointers to point to the first entry of the input and output matrices 
    ptr = &M; 
    ptrT = &MT; 

// if rows=cols we want to use 2d vector temp 
    if (ROWS=COLS) 
    { 
// store all of the input matrice's values in the 2d vector "temp" 
    for(i=0;i<=ROWS-1;i++) 
    { 
     for(j=0;j<=COLS-1;j++) 
     { 

// set the i,j th entry of the 2d vector "temp" equal to the value currently pointed to by the pointer 
     temp[i][j]=*ptr; 
// increment the pointer to the address of the next entry of the input matrix unless we are on the last entry 
       if ((i!=ROWS-1)&&(j!=COLS-1)) 
       { 
       ptr++; 
       } 
     } 
     } 
    } 
// reset pointer address to first entry 
    ptr=&M; 
// the for loop for swapping the j,i entries of the 2d vector "temp" with the i,j entries of the input matrix 
    for(i=0;i<=ROWS-1;i++) 
    { 
     for(j=0;j<=COLS-1;j++) 
     { 
// if j is not equal to i swap the value pointed to by the pointer (the i,j entry of the input matrix) for the j,ith entries value of the 2d vector "temp" 
     if (j!=i) 
      { 
      *ptr=temp[j][i]; 
      } 
     // increment the pointer if it is not on the last entry 
       if ((i!=ROWS-1)&&(j!=COLS-1)) 
       { 
       ptr++; 
       } 
     } 
    }*/ 


// if ROWS<COLS we want to have 2d vector tempT 
if (ROWS<COLS) 
{ 
// store all of the input matrice's values in the 2d vector "tempT" 
    for(i=0;i<=ROWS-1;i++) 
    { 
     for(j=0;j<=COLS-1;j++) 
     { 
// set the j,ith entry of the 2d vector "temp" equal to the value currently pointed to by the pointer 
     tempT[j][i]=*ptr; 
// increment the pointer to the address of the next entry of the input matrix 
     if (((i!=(ROWS-1))&&(j!=(COLS-1)))) 
     { 
      ptr++; 
     } 
     } 
    } 
    ptr=&M; 
// transport the entries of tempT into the output matrix MT 
    for(i=0;i<=COLS-1;i++) 
    { 
     for(j=0;j<=ROWS-1;j++) 
     { 
     *ptrT=tempT[i][j]; 
// increment the pointer 
     if (((i!=ROWS-1)&&(j!=COLS-1))) 
     { 
      ptrT++; 
     } 
     } 
    } 
} 
ptrT=&MT; 

// if ROWS>COLS we want to use the 2d vector temp_T 
if (ROWS>COLS) 
{ 
// store all of the input matrice's values in the 2d vector "temp_T" 
    for(i=0;i<=ROWS-1;i++) 
    { 
     for(j=0;j<=COLS-1;j++) 
     { 

// set the j,i th entry of the 2d vector "temp" equal to the value currently pointed to by the pointer 
     temp_T[j][i]=*ptr; 
// increment the pointer 
     if (((i!=ROWS-1)&&(j!=COLS-1))) 
     { 
      ptrT++; 
     } 
    } 

// the for loop for swapping the j,i entries of the 2d vector "temp" with the i,j entries of the input matrix 
    for(i=0;i<=COLS-1;i++) 
    { 
     for(j=0;j<=ROWS-1;j++) 
     { 
// if j is not equal to i swap the value pointed to by the pointer (the i,j entry of the input matrix) for the j,ith entries value of the 2d vector "temp" 
      if (j!=i) 
      { 
      *ptrT=temp_T[j][i]; 
      } 
// increment the pointer 
     if (((i!=ROWS-1)&&(j!=COLS-1))) 
     { 
      ptrT++; 
     } 
     } 
    } 
} 
return; 
} 

// 그래서 거기는 COLS는 매우 작고 ROWS 말 때문에 중간 2D 사각형 벡터의 매우 큰이지만, 다른 모든 것이 꽤 잘하는 경우는 매우 효율적이지 않다이며,이 경우 제대로 작동했다.

답변

0

DIY 색인 생성을 사용하십시오 : ptrT[ index(j,i,COLS,ROWS) ] = ptr[ index(i,j,ROWS,COLS) ]. 이제 2D indeces를 1D indeces로 변환하는 함수를 작성하면됩니다.

0

은 당신의 코드로,이 라인의 주요 문제의 몇 가지가 있습니다

if (ROWS=COLS) 

아마 될 운명이되었다

if (ROWS == COLS) 

첫 번째 경우는 ROWSCOLS의 값을 할당합니다 두 번째 경우에는 평등한지 검사 할 것입니다. 모든 for 루프에서는 <을 사용할 때 <=을 사용해야합니다. 그렇지 않으면 배열 범위 밖에서 액세스하게됩니다.

template <int n, int m> 
void squaretranspose(int a[n][m], int b[m][n]) 
{ 
    for (int i = 0; i < n; i++) 
    { 
     for (int j = 0; j < m; j++) 
     { 
      b[j][i] = a[i][j]; 
     } 
    } 
} 

그러나 행렬을 전치하는 가장 빠르고 간단한 방법은 반전 될 것입니다 : 그 너머

는, 코드가 너무 복잡, 전치 기능이 하나의 가능한 방법이다, 아주 정직해야한다 귀하의 좌표이므로 (i,j)에 액세스하는 대신 (j,i)에 액세스하십시오. 반면에 성능이 주요 관심사라면이 previous thread은 내 솔루션뿐 아니라 다른 솔루션과도 잘 어울립니다.

관련 문제