2011-12-07 103 views
0

我試圖使用函數來複制矩陣的行並返回指向該行的指針,然後打印該行,但我的get_row()函數失敗,任何幫助將不勝感激, 隨機數據是程序的指定部分,我將不得不以相同的方式獲得一列,轉置和子矩陣,但我希望如果我明白如何做get_row()生病能夠做到的其餘部分:C使用指針創建矩陣,然後從矩陣中複製元素

我在get_row()算法是錯誤的 這裏是我的代碼:

我的主要(修訂):

int main() {  
    int m,n,t,check; 
    double **mat,*matc; 


    check = 0 ; 
    while (check != 2) 
    { 
     printf("\nEnter the size of your matrix m x n in the form m,n : "); 
     check = scanf("%d, %d", &m, &n); 
     _flushall() ; 
    }; 

    int row; 
    mat = (double **) malloc(m * sizeof(double*)) ; 
    for(row = 0; row<m; row++) { 
     mat[row] = (double *) malloc(n * sizeof(double)); 
    }  

    srand((unsigned int) time(NULL)); 
    *rand_matrix(*mat,m,n); 
    print_matrix(*mat,m,n); 
    check = 0 ; 
    while (check != 1) 
    { 
     printf("\nEnter the row you would like to see : "); 
     check = scanf("%d", &t); 
     _flushall() ; 
    };  
    *matc=*get_row(*mat,n,t); 
    print_matrix(matc,4,n); 
    check = 0 ; 
    while (check != 1) 
    { 
     printf("\nEnter the column you would like to see : "); 
     check = scanf("%d", &t); 
     _flushall() ; 
    } 
    printf("\nMatrix column: [%d]\n",t); 
    get_column(*mat,m, n, t); 
    getch(); 
    transpose( *mat, n, m); 
    getch(); 
    free(mat); 
} 

這些功能的即時通訊使用,看看get_row(),並檢查你是否能發現什麼即時通訊做錯了,歡呼聲

//FUNCTION TO POPULATE MATRIX WITH RANDOM DOUBLES 
double *rand_matrix(double *mat, int m, int n) { 
    double *usermat=mat; 
    int i; 
    for (i=0;i<m*n;i++) { 
     *usermat++=i+1; //populates with 1 to m*n 
    } 
    return mat; 
} 

//PRINTS MATRIX 
void print_matrix(double *mat, int m, int n) { 
    int i,j; 
    printf("\nMatrix dimensions: [%d,%d]\n",m,n); 

    double *usermat=mat; 

    for (i=0;i<m;i++) { 
     usermat=(mat+i*n); 
     for (j=0;j<n;j++) { 
      printf(" %0.2lf ",*usermat++); 
     } 
     printf("\n"); 
    } 
}  

//GET ROW  
double *get_row(double *mat, int n,int t) { 
    int i,j; 

    printf("\nMatrix row: [%d]\n",t); 

    double *usermat=mat; 

    printf("\n"); 
    usermat=(mat+n*(t-1)); 
    for (j=0;j<n;j++) { 
     *usermat++; 
    } 
    printf("\n"); 
    return usermat; 
} 
+0

會發生什麼?你期望發生什麼?爲了簡化測試和調試,請勿使用隨機數據。就像填充矩陣中的每一行一樣(例如0,1,2 ...到行-1)。 –

+0

對於未指定的道歉,我必須用這個程序解決的問題中指定的是隨機數據必須使用。 –

+0

您可以在實際程序中使用隨機數據,並在測試時使用非隨機數據。您可以使用預處理器來檢查「DEBUG」宏,它只在您自己構建時纔會定義。 –

回答

0

工作代碼:當你運行你的程序

int i, j; 

float** rand_matrix(float **mat, int m, int n){ 
    float** backup = mat; 
    for (i=0;i<m;i++){ 
     for (j=0;j<n;j++){ 
      mat[i][j] = rand(); 
     } 
    } 
    return backup; 
} 

//PRINTS MATRIX 
void print_matrix(float **mat, int row, int col){ 
    printf("\nMatrix dimensions: [%d,%d]\n", row, col); 

    for (i=0;i<row;i++){ 
     for (j=0;j<col;j++){ 
      printf(" %f ", mat[i][j]); 
     } 
     printf("\n"); 
    } 
} 

void print_row(float **mat, int row, int cols) 
{ 
    printf("\nPrinting row: %d\n", row); 
    for(i = 0; i<cols; i++) 
    { 
     printf("%f", *(mat[row]+i)); 
    } 
} 

//void print_col(float **mat, int rows, int col) 
//{ 
// printf("\nPrinting col: %d\n", col); 
// for(i = 0; i<rows; i++) 
// { 
//  printf("%f ", (mat[i]+col)); 
// } 
//} 

//GET ROW 
float* get_row(float **mat, int row){ 
    return mat[row]; 
} 

int main(){  
    int row, col, rownum, check; 
    float **mat; 
    float *matc; 

    check = 0 ; 
    while (check != 2) 
    { 
     printf("\nEnter the size of your matrix m x n in the form m,n : "); 
     check = scanf("%d, %d", &row, &col); 
     _flushall() ; 
    }; 

    mat = (float **) malloc(row * sizeof(float*)) ; 
    for(i = 0; i<row; i++) { 
     mat[i] = (float *) malloc(col * sizeof(float)); 
    } 

    srand((unsigned int) time(NULL)); 
    mat=rand_matrix(mat, row, col); 
    print_matrix(mat, row, col); 
    check = 0 ; 
    while (check != 1) 
    { 
     printf("\nEnter the row you would like to see : "); 
     check = scanf("%d", &rownum); 
     _flushall() ; 
    };  

    matc = get_row(mat, rownum); 

    print_row(&matc, 0, col); 
    check = 0 ; 
    while (check != 1) 
    { 
     printf("\nEnter the column you would like to see : "); 
     check = scanf("%d", &rownum); 
     _flushall() ; 
    } 
    //printf("\nMatrix column: [%d]\n", rownum); 
    //get_column(mat, row, col, rownum); 
    //getch(); 
    //transpose( mat, row, col); 
    //getch(); 
    free(mat); 
} 
+0

要自己釋放內存泄漏...... :) – c0da

+0

另外,取消print_col()的註釋以打印列。 – c0da

+0

你會如何編寫get_col()函數? –

2

我的第一個建議是使用二維在C中創建矩陣時使用數組語法,而不是嘗試使用一個指針來訪問所有元素。

所以,

double **mat; 
int row; 
mat = (double **) malloc(m * sizeof(double*)) ; 
for(row = 0; row<m; row++) { 
    mat[row] = (double *) malloc(n * sizeof(double)); 
} 

然後

double element = mat[i][j]; 

這是這麼容易的工作,你會很感激這樣做的方式。它在內存中的開銷很小。在許多算法中,由於不再涉及乘法,所以它將比使用算法獲得(i,j)元素的平坦(i * cols + j)座標更有效。

嘗試重新審視手邊的問題後,您的代碼會更簡單。現在看起來你已經厭倦了,並且對這個問題感到沮喪。

+0

+ 1爲二維矩陣的建議...他做的混亂計算將消失...然後... – c0da

+0

良好的輸入,我已經改變了我自己的代碼像你建議,但即時通訊仍然堅持複製get_row func中的元素,有什麼建議嗎?在一個本科生進入C級.. –

+0

再次,你的建議解決了我的另一個問題,乾杯! –