2012-10-23 9 views
6

Sto provando a scrivere 2 funzioni, una per leggere la matrice (array 2D) e un'altra per stamparla. Finora ho:Creazione di un array 2D mediante puntatore/malloc, quindi stamparlo

/* Read a matrix: allocate space, read elements, return pointer. The 
    number of rows and columns are given by the two arguments. */ 
double **read_matrix(int rows, int cols){ 

    double **mat = (double **) malloc(sizeof(double *)*rows); 
    int i=0; 
    for(i=0; i<rows; i++){ 
    /* Allocate array, store pointer */ 
    mat[i] = (double *) malloc(sizeof(double)*cols); 
    //what to do after?? 

    return mat; 
} 

allora la funzione della matrice di stampa, non è sicuro se è corretto

void print_matrix(int rows, int cols, double **mat){ 
    for(i=0; i<rows; i++){ /* Iterate of each row */ 
    for(j=0; j<cols; j++){ /* In each row, go over each col element */ 
    printf("%f ",mat[i][j]); /* Print each row element */ 
} 
}} 

e qui è la funzione principale che sto usando per eseguire:

#include <stdio.h> 
#include <stdlib.h> 


double **read_matrix(int rows, int cols); 
void print_matrix(int rows, int cols, double **mat); 
void free_matrix(int rows, double **mat); 

int main(){ 

    double **matrix; 
    int rows, cols; 
    /* First matrix */ 
    printf("Matrix 1\n"); 
    printf("Enter # of rows and cols: "); 
    scanf("%d %d",&rows,&cols); 
    printf("Matrix, enter %d reals: ",rows*cols); 
    matrix = read_matrix(rows,cols); 
    printf("Your Matrix\n"); /* Print the entered data */ 
    print_matrix(rows,cols,matrix); 
    free_matrix(rows, matrix); /* Free the matrix */ 

    return 0;} 
+2

Le votazioni per chiudere perché sembra una domanda per la revisione del codice StackExchange invece di StackOverflow. Il codice sembra fondamentalmente buono, però. –

+0

Qual è il tuo problema esatto? Non vedo nessuna domanda qui ... – Christoph

+0

@BenJackson: è chiuso come [offtopic sul review.stackexchange] (http://codereview.stackexchange.com/q/17833/6143). Non è una bella esperienza. – jfs

risposta

7

Prova questo. potrebbe esserti utile.

#include <stdio.h> 
#include <stdlib.h> 

double **read_matrix(int rows, int cols); 
void print_matrix(int rows, int cols, double **mat); 
void free_matrix(int rows, double **mat); 

double **read_matrix(int rows, int cols){ 

    double **mat = (double **) malloc(sizeof(double *)*rows); 
    int i=0,j=0; 
    for(i=0; i<rows; i++) 
    /* Allocate array, store pointer */ 
     mat[i] = (double *) malloc(sizeof(double)*cols); 

     for(i=0; i<rows; i++){ 
      for(j=0; j<cols; j++){ 
       scanf("%lf",&mat[i][j]); 
      } 
     } 
    return mat; 
} 

void print_matrix(int rows, int cols, double **mat){ 
    int i=0,j=0; 
    for(i=0; i<rows; i++){ /* Iterate of each row */ 
     for(j=0; j<cols; j++){ /* In each row, go over each col element */ 
      printf("%lf ",mat[i][j]); /* Print each row element */ 
     } 
     printf("\n"); 
    } 
} 

void free_matrix(int rows, double **mat){ 
    int i=0; 
    for(i=0;i<rows;i++)  
     free(mat[i]); 
    free(mat); 
} 

int main(){ 

    double **matrix; 
    int rows, cols; 
    /* First matrix */ 
    printf("Matrix 1\n"); 
    printf("Enter # of rows and cols: "); 
    scanf("%d%d",&rows,&cols); 
    printf("Matrix, enter %d reals: \n",rows*cols); 
    matrix = read_matrix(rows,cols); 
    printf("Your Matrix\n"); /* Print the entered data */ 
    print_matrix(rows,cols,matrix); 
    free_matrix(rows, matrix); /* Free the matrix */ 

    return 0; 
} 

Esecuzione:

:~$ gcc exam.c 
:~$ ./a.out 
Matrix 1 
Enter # of rows and cols: 3 
4 
Matrix, enter 12 reals: 
1 
2 
3 
4 
5 
6 
7 
8 
9 
9 
0 
1 
Your Matrix 
1.000000 2.000000 3.000000 4.000000 
5.000000 6.000000 7.000000 8.000000 
9.000000 9.000000 0.000000 1.000000 
+0

@Jenny No non ci sono problemi di seg in questo codice .. Ho modificato i passaggi –