C使用指针创建矩阵,然后从矩阵中复制元素

时间:2011-12-07 12:47:44

标签: c pointers matrix

我试图使用一个函数来复制一行矩阵并返回指向该行的指针,然后打印该行,但是我的get_row()函数失败了,任何帮助都会非常感激, 随机数据是程序的一个指定部分,然后我必须以相同的方式获得一个列,转置和子矩阵,但我希望如果我理解如何为get_row()生成其余部分:< / p>

我在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;
}  

2 个答案:

答案 0 :(得分: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)坐标更有效,因为不再涉及乘法。

尝试重新审视手头的问题后,您的代码会更简单。现在,看起来你已经厌倦了并且对这个问题感到沮丧。

答案 1 :(得分: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);
}