Représentation graphique
Bonjour,
Comment je fais pour représenter graphiquement une matrice en programant en C ???
C'est possible le code.
Merci.
Comment je fais pour représenter graphiquement une matrice en programant en C ???
C'est possible le code.
Merci.
Configuration: Linux Firefox 2.0.0.13
1 réponse
-
Par exemple comme ceci :
#include <stdio.h> #include <stdlib.h> #include <assert.h> typedef struct matrix_t{ unsigned nb_row; unsigned nb_col; double **values; } matrix_t; matrix_t *new_matrix(unsigned nb_row0,unsigned nb_col0){ unsigned i; matrix_t *m = (matrix_t *)malloc(sizeof(matrix_t)); m->nb_row = nb_row0; m->nb_col = nb_col0; m->values = (double **) calloc(sizeof(double *),nb_row0); for(i=0;i<nb_row0;++i){ m->values[i] = (double *) calloc(sizeof(double),nb_col0); } return m; } void delete_matrix(matrix_t *m){ unsigned i,nb_row = m->nb_row; for(i=0;i<nb_row;++i) free(m->values[i]); free(m->values); free(m); } inline void set(matrix_t *m,const unsigned row,const unsigned col,const double value){ assert(row < m->nb_row); assert(col < m->nb_col); m->values[row][col] = value; } inline double get(const matrix_t *m,const unsigned row,const unsigned col){ assert(row < m->nb_row); assert(col < m->nb_col); return m->values[row][col]; } void write_matrix(FILE *fp,const char *format,const matrix_t *m){ unsigned i,j,nb_row = m->nb_row,nb_col=m->nb_col; for(i=0;i<nb_row;++i){ for(j=0;j<nb_col;++j){ fprintf(fp,format,get(m,i,j)); fprintf(fp,"\t"); } fprintf(fp,"\n"); } } int main(){ const unsigned nb_row=5,nb_col=4; // on crée la matrice en mémoire matrix_t *m = new_matrix(nb_row,nb_col); // on initialise la matrice Mij = 10*i + j unsigned i,j; for(i=0;i<nb_row;++i){ for(j=0;j<nb_col;++j){ set(m,i,j,10*i+j); } } // on affiche le contenu de la matrice write_matrix(stdout,"%.3lf",m); // on vide la mémoire delete_matrix(m); // à décommenter sous windows // getchar(); return 0; }
ce qui donne à l'exécution :0.000 1.000 2.000 3.000 10.000 11.000 12.000 13.000 20.000 21.000 22.000 23.000 30.000 31.000 32.000 33.000 40.000 41.000 42.000 43.000
Bonne chance