Title / Description
Code #include <stdio.h> #include <stdlib.h> void saisir_dim_matrice(unsigned int * lig, unsigned int * col); int** alloc_matrice(unsigned int lig, unsigned int col); void desalloc_matrice(int** mat, unsigned int lig); void saisir_coeffs_matrice(int** mat, unsigned int lig, unsigned int col); void aff_coeffs_matrice(int** mat, unsigned int lig, unsigned int col); int** somme_matrices(int** m1, unsigned int m1_lig, unsigned int m1_col, int** m2, unsigned int m2_lig, unsigned int m2_col, unsigned int* somme_lig, unsigned int* somme_col ); int main() { int** m1 = NULL; unsigned int m1_lig = 0, m1_col = 0; int** m2 = NULL; unsigned int m2_lig = 0, m2_col = 0; int** s = NULL; unsigned int s_lig = 0, s_col = 0; //saisir matrice 1 printf("** Saisir votre 1er matrice : \n"); saisir_dim_matrice(&m1_lig, &m1_col); m1 = alloc_matrice(m1_lig, m1_col); saisir_coeffs_matrice(m1, m1_lig, m1_col); printf("\n"); //saisir matrice 2 printf("** Saisir votre 2eme matrice : \n"); saisir_dim_matrice(&m2_lig, &m2_col); m2 = alloc_matrice(m2_lig, m2_col); saisir_coeffs_matrice(m2, m2_lig, m2_col); printf("\n"); //calcul de la somme printf("Calcul de la somme de vos deux matrices... "); s = somme_matrices(m1, m1_lig, m1_col, m2, m2_lig, m2_col, &s_lig, &s_col); printf("ok\n"); //affichage des matrices printf("\n** Affichage de m1 **\n"); aff_coeffs_matrice(m1, m1_lig, m1_col); printf("\n** Affichage de m2 **\n"); aff_coeffs_matrice(m2, m2_lig, m2_col); printf("\n** Affichage de la somme **\n"); aff_coeffs_matrice(s, s_lig, s_col); //Desallocation des matrices desalloc_matrice(m1, m1_lig); desalloc_matrice(m2, m2_lig); desalloc_matrice(s, s_lig); return 0; } void saisir_dim_matrice(unsigned int * lig, unsigned int * col) { /* //version sans saisie sécurisée printf("Nbr de lignes : "); scanf(" %d", lig); //ne pas mettre le & car lig est un pointeur vers la case ou on veut écrire printf("Nbr de colonnes : "); scanf(" %d", col); */ //version avec une saisie sécurisée int lig_tmp = 0, col_tmp = 0; do{ printf("Nbr de lignes >0 : "); scanf(" %d", &lig_tmp); //ne pas mettre le & car lig est un pointeur vers la case ou on veut écrire }while( lig_tmp <= 0); *lig = lig_tmp; do { printf("Nbr de colonnes >0 : "); scanf(" %d", &col_tmp); }while( col_tmp <= 0); *col = col_tmp; return; } int** alloc_matrice(unsigned int lig, unsigned int col) { int** mat = NULL; //allocation automatique => variable locale => PILE mat = (int**)malloc(lig*sizeof(int*)); //malloc => allocation dynamique => TAS if( mat == NULL) { printf(" ERREUR : allocation 1er dim matrice impossible...\n"); return NULL; // ou alors on peut sortir du programme } for(int i = 0; i < lig ; i++) { mat[i] = (int*)malloc(col*sizeof(int)); if( mat[i] == NULL) { printf(" ERREUR : allocation 2eme dim matrice impossible...\n"); //attention il faut désallouer tout ce qui a été alloué précédemment for(int k = 0 ; k < i ; i++) { free(mat[k]); } free(mat); return NULL; // ou alors on peut sortir du programme } } return mat; } void saisir_coeffs_matrice(int** mat, unsigned int lig, unsigned int col) { //printf(" \t (fct saisir) mat : %p\n", mat); //pour constater que c'est bien le même tableau if( mat == NULL) { printf("ERREUR : saisie impossible, matrice non allouee...\n"); return; //on aurait pu sortir du programme si ça pose vraiment problème } for(int i = 0; i < lig ; i++) { for(int j = 0 ; j < col ; j++) { printf("m[%d][%d]=", i, j); scanf(" %d", &mat[i][j]); } } return; } void aff_coeffs_matrice(int** mat, unsigned int lig, unsigned int col) { if( mat == NULL) { printf("VIDE (matrice non allouee...)\n"); return; //on aurait pu sortir du programme si ça pose vraiment problème } for(int i = 0; i < lig ; i++) { for(int j = 0 ; j < col ; j++) { printf("%6d ",mat[i][j]); } printf("\n"); } return; } void desalloc_matrice(int** mat, unsigned int lig) { if(mat == NULL) return; for(int i = 0; i < lig ; i++) { free(mat[i]); } free(mat); return; } int** somme_matrices(int** m1, unsigned int m1_lig, unsigned int m1_col, int** m2, unsigned int m2_lig, unsigned int m2_col, unsigned int* somme_lig, unsigned int* somme_col) { int** somme_matrice = NULL; //somme n'est défini que si les dimensions des matrices sont identiques if (m1_lig != m2_lig || m1_col != m2_col) { printf("Erreur : les dimensions des matrices ne correspondent pas.\n"); *somme_lig = 0; *somme_col = 0; return NULL; } somme_matrice = alloc_matrice(m1_lig, m1_col); //la matrice n'a pas pu être allouée if(somme_matrice == NULL){ *somme_lig = 0; *somme_col = 0; return NULL; } //calcul de la somme *somme_lig = m1_lig; *somme_col = m1_col; for (unsigned int i = 0; i < *somme_lig; i++) { for (unsigned int j = 0; j < *somme_col; j++) { somme_matrice[i][j] = m1[i][j] + m2[i][j]; } } return somme_matrice; }
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code