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 208 209 210
| #include<stdio.h>
#include<stdlib.h>
#include<windows.h>
void Allouer_Matrice ( int ***Matrice, int Colonne, int Ligne );
void Sommer_Matrice ( int **Matrice, int **Matrice_A, int **Matrice_B, int Ligne, int Colonne );
void Soustraire_Matrice ( int **Matrice, int **Matrice_A, int **Matrice_B, int Ligne, int Colonne );
void Initialiser_Matrice ( int **Matrice, int Ligne, int Colonne, int Valeur );
void Remplir_Matrice_Aleatoirement ( int **Matrice, int Ligne, int Colonne, int OFFSET, int Valeur );
void Multiplier_Matrice ( int **Matrice, int **Matrice_A, int **Matrice_B, int Ligne_A, int Colonne_A, int Ligne_B, int Colonne_B );
void Afficher_Matrice ( int **Matrice, int Ligne, int Colonne );
void Desallouer_Matrice ( int **Matrice, int Colonne );
int Sont_Multipliables ( int Colonne_A, int Ligne_B );
int main (void)
{
int **MatriceA=NULL;
int **MatriceB=NULL;
int **Matrice=NULL;
Allouer_Matrice( &MatriceA, 4, 4 );
Allouer_Matrice( &MatriceB, 4, 4 );
Allouer_Matrice( &Matrice , 4, 4 );
Remplir_Matrice_Aleatoirement( MatriceA, 4, 4, 4, 4 );
Remplir_Matrice_Aleatoirement( MatriceB, 4, 4, 4, 4 );
Initialiser_Matrice ( Matrice , 4, 4, 0 );
printf("\n\n///////////////////MATRICE A///////////////////\n");
Afficher_Matrice( MatriceA, 4, 4 );
printf("\n\n///////////////////MATRICE B///////////////////\n");
Afficher_Matrice( MatriceB, 4, 4 );
Sommer_Matrice( Matrice, MatriceA, MatriceB, 4, 4 );
printf("\n\n/////////////////MATRICE SOMMEE////////////////\n");
Afficher_Matrice( Matrice, 4, 4 );
Soustraire_Matrice( Matrice, MatriceA, MatriceB, 4, 4 );
printf("\n\n///////////////MATRICE SOUSTRAITE//////////////\n");
Afficher_Matrice( Matrice, 4, 4 );
Multiplier_Matrice( Matrice, MatriceA, MatriceB, 4, 4, 4, 4 );
printf("\n\n///////////////MATRICE MULTIPLIEE//////////////\n");
Afficher_Matrice( Matrice, 4, 4 );
Desallouer_Matrice( MatriceA, 4 );
Desallouer_Matrice( MatriceB, 4 );
Desallouer_Matrice( Matrice, 4 );
getchar();
}
void Allouer_Matrice(int ***Matrice, int Ligne, int Colonne)
{
if ( Matrice != NULL )
{
int i=0;
// Allouer les colonnes
*Matrice = malloc( Ligne*sizeof(int*) );
// Allouer les lignes
for ( i=0 ; i<Ligne ; i++)
{
(*Matrice)[i]=malloc( Colonne*sizeof(int) );
}
}
else printf("\n\nERREUR DANS LA FONCTION Allouer_Mactrice\n + VERIFIEZ QUE MATRICE EST CORRECTEMENT REFERENCEE\n\n");
}
void Initialiser_Matrice(int **Matrice, int Ligne, int Colonne, int Valeur)
{
if ( Matrice != NULL )
{
int i, j;
for ( i=0 ; i<Ligne ; i++ )
{
for ( j=0 ; j<Colonne ; j++ )
{
Matrice[i][j]=Valeur;
}
}
}
else printf("\n\nERREUR DANS LA FONCTION Initialiser_Matrice\n + VERIFIEZ QUE MATRICE EST CORRECTEMENT REFERENCEE\n\n");
}
void Remplir_Matrice_Aleatoirement(int **Matrice, int Ligne, int Colonne, int OFFSET, int Valeur)
{
if ( Matrice != NULL )
{
int i, j;
for ( i=0 ; i<Ligne ; i++ )
{
for ( j=0 ; j<Colonne ; j++ )
{
Matrice[i][j]=OFFSET + rand()%(Valeur+1);
}
}
}
else printf("\n\nERREUR DANS LA FONCTION Remplir_Matrice_Aleatoirement\n + VERIFIEZ QUE MATRICE EST CORRECTEMENT REFERENCEE\n\n");
}
void Sommer_Matrice( int **Matrice, int **Matrice_A, int **Matrice_B, int Ligne, int Colonne)
{
if ( Matrice !=NULL && Matrice_A != NULL && Matrice_B != NULL )
{
int i,j;
for ( i=0 ; i<Ligne ; i++ )
{
for ( j=0 ; j<Colonne ; j++ )
{
Matrice[i][j]=Matrice_A[i][j]+Matrice_B[i][j];
}
}
}
else printf("\n\nERREUR DANS LA FONCTION Sommer_Matrice\n + VERIFIEZ QUE MATRICE EST CORRECTEMENT REFERENCEE\n\n");
}
void Soustraire_Matrice( int **Matrice, int **Matrice_A, int **Matrice_B, int Ligne, int Colonne)
{
if ( Matrice !=NULL && Matrice_A != NULL && Matrice_B != NULL )
{
int i,j;
for ( i=0 ; i<Ligne ; i++ )
{
for ( j=0 ; j<Colonne ; j++ )
{
Matrice[i][j]=Matrice_A[i][j]-Matrice_B[i][j];
}
}
}
else printf("\n\nERREUR DANS LA FONCTION Soustraire_Matrice\n + VERIFIEZ QUE MATRICE EST CORRECTEMENT REFERENCEE\n\n");
}
void Afficher_Matrice(int **Matrice, int Ligne, int Colonne)
{
if ( Matrice != NULL )
{
int i, j;
for ( i=0 ; i<Ligne ; i++ )
{
for ( j=0 ; j<Colonne ; j++)
{
printf("\nMatrice[%d][%d]=%d", i+1, j+1, Matrice[i][j]);
}
}
}
else printf("\n\nERREUR DANS LA FONCTION Afficher_Matrice\n + VERIFIEZ QUE MATRICE EST CORRECTEMENT REFERENCEE\n\n");
}
void Desallouer_Matrice( int **Matrice, int Colonne )
{
if ( Matrice != NULL )
{
int i;
for ( i=1 ; i<Colonne ; i++ )
{
free(Matrice[i]);
}
free(Matrice);
}
else printf("\n\nERREUR DANS LA FONCTION Desallouer_Matrice\n + VERIFIEZ QUE MATRICE EST CORRECTEMENT REFERENCEEn\n");
}
int Sont_Multipliables( int Colonne_A, int Ligne_B )
{
if ( Colonne_A == Ligne_B )
{
return 1;
}
else return 0;
}
void Multiplier_Matrice( int **Matrice, int **Matrice_A, int **Matrice_B, int Ligne_A, int Colonne_A, int Ligne_B, int Colonne_B )
{
if ( Matrice != NULL && Matrice_A != NULL && Matrice_B != NULL && Sont_Multipliables( Colonne_A, Ligne_B ) == 1 )
{
int i,j;
int Repeter=0;
for ( i=0 ; i<Ligne_A ; i++ )
{
for ( j=0 ; j<Colonne_B ; j++ )
{
while ( Repeter != Colonne_A )
{
Matrice[i][j]=Matrice[i][j]+ Matrice_A[i][Repeter]*Matrice_B[Repeter][j];
Repeter++;
}
Repeter=0;
}
}
}
else printf("\n\nERREUR DANS LA FONCTION Multiplier_Matrice\n + VERIFIEZ QUE MATRICE EST CORRECTEMENT REFERENCEE\n + LES DIMENSIONS DES MATRICES NE SONT PAS COMPATIBLES POUR LA MULTIPLICATION\n\n");
} |
Partager