Eoin Murray
Eoin Murray

Reputation: 1955

C creating a matrix using pointers and then copying elements from the matrix

Im trying to use a function to copy a row of a matrix and return a pointer to that row, then print that row, but my get_row() function is failing, any help would be most appreciated, random data is a specified piece of the program, i will then have to get a column, transpose and sub matrix the same way, but i hope if i understand how to do it for get_row() ill be able to do the rest:

my algorithm in get_row() is wrong here is my code:

my main(revised):

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);
}

These are the functions im using, have a look at get_row(), and check if you can spot what im doing wrong, cheers

//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;
}  

Upvotes: 0

Views: 13203

Answers (2)

c0da
c0da

Reputation: 1009

Working code:

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);
}

Upvotes: 1

buddhabrot
buddhabrot

Reputation: 1586

My first advice would be to use a two-dimensional array syntax in C when you make matrices, instead of trying to use one pointer to access all elements.

So,

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

Then

double element = mat[i][j];

This is so much easier to work with, you will be grateful doing it that way. It has a minimal overhead in memory. In many algorithms it will be more efficient than using arithmetics to get the flat (i*cols + j) coordinate of an (i,j) element since there is no multiplication involved anymore.

Try to revisit the problem at hand after doing that, your code will be simpler. Right now it looks like you tired and frustrated yourself with the problem.

Upvotes: 2

Related Questions