A

C code posted
created at 15 May 16:52, updated at 01 Jun 17:59

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#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;
}
5.72 KB in 6 ms with coderay