Bonjour,
Je dois coder la linéarisation d'une matrice en zigzag. Mais ça ne marche pas. J'ai des erreurs ac Purify, des Array Bounds Read, Array Bounds Write et des Zero Page Read.
Voici mon code source et mon test.
et mon fichier test
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include "vector.h" #include <stdio.h> #include <stdbool.h> #include <stdlib.h> matrice allouer_matrice(short int l,short int c){ matrice m; short int k; m.hauteur=l; m.largeur=c; m.coeff=malloc(l*sizeof(short int*)); for(k=0; k<l; k++){ m.coeff[k]=malloc(c*sizeof(short int)); } return m; } vecteur creer_vecteur(const int capacite) { vecteur v; v.card= capacite; v.coeff= malloc(capacite*sizeof(Element)); return v; } void liberer_matrice(matrice* m){ short int k; for(k=0; k<m->hauteur; k++) free(m->coeff[k]); } void detruire_vecteur(vecteur* v) { free(v->coeff); } int taille_vecteur(vecteur v) { return v.card; } bool est_pair(int i){ return(i%2==0); } vecteur lineariser(matrice m){ vecteur v; int i=0,j=0,k=0; // i= indice ligne matrice //j= indice colonne matrice //k= indice ligne vecteur v=creer_vecteur(m.hauteur*m.largeur); v.coeff[k]=m.coeff[j][i]; for (k=1; k<v.card-1; k++){ if (((j == m.hauteur-1) && (!est_pair(i))) || ((j==0) && (est_pair(i)))){ i++; } else if (((i == m.largeur-1) && (est_pair(j))) || ( (i==0) && (!est_pair(j)) ) ){ j++; } else if (est_pair(i+j)){ i++; j--; } else if (!est_pair(i+j)){ i--; j++; } v.coeff[k]= m.coeff[j][i]; // !!!!!!! ERREUR } return v; } void afficher_vecteur(vecteur v){ printf("[ "); for (int i=0;i<v.card;i++){ printf(" %d",v.coeff[i]); } printf(" ]\n"); }
Merci d'éclairer ma lanterne.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 int main(){ matrice t; t=allouer_matrice(8,8); printf("ok\n"); vecteur v; /**Allocation de mémoire**/ //if (t.coeff != NULL){ t.hauteur=8; t.largeur=8; t.coeff[0][0]=1; t.coeff[0][1]=3; t.coeff[0][2]=4; t.coeff[0][3]=10; t.coeff[0][4]=11; t.coeff[0][5]=21; t.coeff[0][6]=22; t.coeff[0][7]=36; t.coeff[1][0]=2; t.coeff[1][1]=5; t.coeff[1][2]=9; t.coeff[1][3]=12; t.coeff[1][4]=20; t.coeff[1][5]=23; t.coeff[1][6]=35; t.coeff[1][7]=37; t.coeff[2][0]=6; t.coeff[2][1]=8; t.coeff[2][2]=13; t.coeff[2][3]=19; t.coeff[2][4]=24; t.coeff[2][5]=34; t.coeff[2][6]=38; t.coeff[2][7]=49; t.coeff[3][0]=7; t.coeff[3][1]=14; t.coeff[3][2]=18; t.coeff[3][3]=25; t.coeff[3][4]=33; t.coeff[3][5]=39; t.coeff[3][6]=48; t.coeff[3][7]=50; t.coeff[4][0]=15; t.coeff[4][1]=17; t.coeff[4][2]=26; t.coeff[4][3]=32; t.coeff[4][4]=40; t.coeff[4][5]=47; t.coeff[4][6]=51; t.coeff[4][7]=58; t.coeff[5][0]=16; t.coeff[5][1]=27; t.coeff[5][2]=31; t.coeff[5][3]=41; t.coeff[5][4]=46; t.coeff[5][5]=52; t.coeff[5][6]=57; t.coeff[5][7]=59; t.coeff[6][0]=28; t.coeff[6][1]=30; t.coeff[6][2]=42; t.coeff[6][3]=45; t.coeff[6][4]=53; t.coeff[6][5]=56; t.coeff[6][6]=60; t.coeff[6][7]=63; t.coeff[7][0]=29; t.coeff[7][1]=43; t.coeff[7][2]=44; t.coeff[7][3]=54; t.coeff[7][4]=55; t.coeff[7][5]=61; t.coeff[7][6]=62; t.coeff[7][7]=64; for (int i=0;i<=7;i++){ for(int j=0;j<=7;j++){ printf("%3d ",t.coeff[i][j]); } printf("\n"); } v=lineariser(t); afficher_vecteur(v); detruire_vecteur(&v); liberer_matrice(&t); //} return EXIT_SUCCESS; }
Principe du zigzag!
![]()
Partager