aboutsummaryrefslogtreecommitdiff
path: root/semestre 3/structures des données/tme/tme1-2/exo2/question4-6.c
blob: d75179a51bc1b2e84e1bbe6fba6ed09bca8505aa (plain)
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
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

typedef struct{
    int size;
    int* content;
} Matrice;

Matrice* alloue_matrice(int n){
    int** content = (int**) malloc(n*n*sizeof(int));
    Matrice* m = (Matrice*) malloc(sizeof(Matrice));
    m->content = content;
    return m
}

void desalloue_matrice(Matrice* m){
    free(m->content);
    free(m);
}

void remplir_matrice(Matrice* m, int max){
    srand(time(null));
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            m->content[i][j] = rand()%max;
        }
    }
}

void affiche_matrice(Matrice* m){
    printf("[");
    for (int i = 0; i < n; i++){
        printf(" [");
        for (int j = 0; j < n; j++){
            printf("%d ", m->content[i][j]);
        }
        printf("]%c", i != n -1 ? '\n' : ' ');
    }
    printf("]\n");
}

Matrice* produit_matrice(Matrice* m1, Matrice* m2){
    if (m1->size != m2->size){
        return NULL;
    }
    Matrice* prod = alloue_matrice(m1->size);
    for (int i = 0; i < m1->size; i++){
        for (int k = 0; k < m1->size; k++){
            prod->content[i][k] = 0;
            for (int j = 0; j < m1->size; j++){
                prod->content[i][k] += m1->content[i][j]*m2->content[j][k];
            }
        }
    }
    return prod;
}

/* Reste en O(n3), mais est plus rapide que produit_matrice car on arrete les boucles plus tot */
Matrice* produit_matrice_trian(Matrice* m_sup, Matrice* m_inf){
    if (m_sup->size != m_inf->size){
        return NULL;
    }
    Matrice* prod = alloue_matrice(m_sup->size);
    for (int i = 0; i < m_sup->size; i++){
        for (int k = 0; k < m_sup->size; k++){
            prod->content[i][k] = 0;
            for (int j = i; j < m_sup->size; j++){
                prod->content[i][k] = m_sup->content[i][j]*m_inf->content[j][k];
            }
        }
    }
    return prod;
}