Corrigé du TP sur les matrices Langage C : Pointeurs, compilation

publicité
// --------------------------------------//
Fichier matrice.h
// --------------------------------------struct SMatrice{
int **coeff;
int nbl;
int nbc;
};
typedef struct SMatrice *Matrice;
Matrice creerMatrice(int, int);
void afficherMatrice(Matrice);
int recNbl(Matrice);
int recNbc(Matrice);
int recCase(Matrice, int, int);
Matrice affCase(Matrice, int, int, int);
Matrice additionMatrice(Matrice, Matrice);
Matrice multiplicationMatrice(Matrice, Matrice);
// --------------------------------------//
Fichier matrice.c
// --------------------------------------#include <stdio.h>
#include <stdlib.h>
#include "matrice.h"
Matrice creerMatrice(int lignes, int cols){
Matrice ptMat;
int i, j;
ptMat = (Matrice) malloc (sizeof(struct SMatrice));
ptMat->nbl = lignes;
ptMat->nbc = cols;
ptMat->coeff = (int**)malloc((ptMat->nbl)*sizeof(int*));
for(i=0; i<(ptMat->nbl); i++){
ptMat->coeff[i] = (int*)malloc((ptMat->nbc)*sizeof(int));
for(j=0; j<(ptMat->nbc); j++){
ptMat->coeff[i][j]=0;
}
}
}
void afficherMatrice(Matrice mat){
int i, j;
for(i=0; i<(mat->nbl); i++){
for(j=0; j<(mat->nbc); j++){
printf("%d ", mat->coeff[i][j]);
}
printf("\n");
}
}
int recNbl(Matrice mat){
return mat->nbl;
1
}
int recNbc(Matrice mat){
return mat->nbc;
}
int recCase(Matrice mat, int valLigs, int valCols){
return mat->coeff[valLigs][valCols];
}
Matrice affCase(Matrice mat, int valLigs, int valCols, int element){
mat->coeff[valLigs][valCols]=element;
return mat;
}
Matrice additionMatrice(Matrice mat1, Matrice mat2){
Matrice ptMat;
int i,j;
ptMat = creerMatrice(mat1->nbl, mat1->nbc);
if((mat1->nbl==mat2->nbl)&&(mat1->nbc==mat2->nbc)){
for(i=0; i<mat1->nbl; i++){
for(j=0; j<mat2->nbc; j++){
ptMat->coeff[i][j]= mat1->coeff[i][j]+mat2->coeff[i][j];
}
}
return ptMat;
}else{
printf("Impossible d'additionner les deux matrices\n");
exit(0);
}
}
Matrice multiplicationMatrice(Matrice mat1, Matrice mat2){
Matrice ptMat;
int i,j,k;
ptMat = creerMatrice(mat1->nbl, mat2->nbc);
if(mat1->nbc==mat2->nbl){
for(i=0; i<mat1->nbl; i++){
for(j=0; j<mat2->nbc; j++){
for(k=0; k<mat1->nbc; k++){
ptMat->coeff[i][j] = ptMat->coeff[i][j]+(mat1>coeff[i][k]*mat2->coeff[k][j]);
}
}
}
return ptMat;
}else{
printf("Impossible de multiplier les deux matrices\n");
exit(0);
}
}
int main(){
Matrice matA, matB, matC, matAdd, matMulti;
int caseA;
//creation de la matrice matA avec 3lignes et 4colonnes
printf("\n************ Matrice matA *****************\n");
matA=creerMatrice(3,4);
//affichage de la matrice matA apres creation et sans modification
printf("voici la matrice matA apres la creation : \n");
afficherMatrice(matA);
//modification de la matrice matA
/**** modification de la premiere ligne ******/
matA = affCase(matA, 0, 0, 0);
matA = affCase(matA, 0, 1, 1);
matA = affCase(matA, 0, 2, 1);
matA = affCase(matA, 0, 3, 3);
/**** modification de la deuxieme ligne ******/
matA = affCase(matA, 1, 0, 1);
matA = affCase(matA, 1, 1, 5);
matA = affCase(matA, 1, 2, 0);
2
matA = affCase(matA, 1, 3, 1);
/**** modification de la troisieme ligne ******/
matA = affCase(matA, 2, 0, 1);
matA = affCase(matA, 2, 1, 2);
matA = affCase(matA, 2, 2, 1);
matA = affCase(matA, 2, 3, 0);
//affichage de la matrice matA apres creation et sans modification
printf("voici la matrice matA apres la modification : \n");
afficherMatrice(matA);
//recuperation d'un element de la matrice matA
caseA = recCase(matA, 2, 1);
printf("Voici la valeur de la case de matA choisie (L3/C2): %d\n", caseA);
//Creation de la matrice matB (3lignes/4colonnes) pour l'addition avec matA
printf("\n************ Matrice matB *****************\n");
matB=creerMatrice(3,4);
//modification de la matrice matB
/**** modification de la premiere ligne ******/
matB = affCase(matB, 0, 0, 1);
matB = affCase(matB, 0, 1, 0);
matB = affCase(matB, 0, 2, 0);
matB = affCase(matB, 0, 3, 2);
/**** modification de la deuxieme ligne ******/
matB = affCase(matB, 1, 0, 0);
matB = affCase(matB, 1, 1, 1);
matB = affCase(matB, 1, 2, 1);
matB = affCase(matB, 1, 3, 1);
/**** modification de la troisieme ligne ******/
matB = affCase(matB, 2, 0, 3);
matB = affCase(matB, 2, 1, 1);
matB = affCase(matB, 2, 2, 2);
matB = affCase(matB, 2, 3, 1);
//affichage de la matrice matB
printf("voici la matrice matB apres la modification : \n");
afficherMatrice(matB);
//addition de matA+matB
printf("\n************ matA + matB *****************\n");
matAdd = additionMatrice(matA, matB);
//affichage du resultat de l'addition
printf("l'addition entre matA et matB donne le resultat suivant : \n");
afficherMatrice(matAdd);
//Creation de la matrice matC (4lignes/2colonnes) pour la multiplication
printf("\n************ Matrice matC *****************\n");
matC=creerMatrice(4,2);
//modification de la matrice matB
/**** modification de la premiere ligne ******/
matC = affCase(matC, 0, 0, 1);
matC = affCase(matC, 0, 1, 0);
/**** modification de la deuxieme ligne ******/
matC = affCase(matC, 1, 0, 1);
matC = affCase(matC, 1, 1, 1);
/**** modification de la troisieme ligne ******/
matC = affCase(matC, 2, 0, 0);
matC = affCase(matC, 2, 1, 0);
/**** modification de la quatrieme ligne ******/
matC = affCase(matC, 3, 0, 0);
matC = affCase(matC, 3, 1, 1);
//affichage de la matrice matC
printf("voici la matrice matC apres la modification : \n");
afficherMatrice(matC);
avec matA
//multiplication de matA*matC
printf("\n************ matA x matC *****************\n");
matMulti = multiplicationMatrice(matA, matC);
//affichage du resultat de la multiplication
printf("la multiplication entre matA et matC donne le resultat suivant : \n");
afficherMatrice(matMulti);
return (0);
}
3
# --------------------------------------#
Fichier Makefile
# --------------------------------------BIN = matrice
OBJECTS = matrice.o
CC = gcc
all: $(OBJECTS)
$(CC) $(OBJECTS) -o $(BIN) -lm
suites.o: matrice.c matrice.h
$(CC) -c matrice.c
clean:
rm -f $(OBJECTS) $(BIN) *~
// --------------------------------------//
Résultat
// --------------------------------------************ Matrice matA *****************
voici la matrice matA apres la creation :
0 0 0 0
0 0 0 0
0 0 0 0
voici la matrice matA apres la modification :
0 1 1 3
1 5 0 1
1 2 1 0
Voici la valeur de la case de matA choisie (L3/C2): 2
************ Matrice matB *****************
voici la matrice matB apres la modification :
1 0 0 2
0 1 1 1
3 1 2 1
************ matA + matB *****************
l'addition entre matA et matB donne le resultat suivant :
1 1 1 5
1 6 1 2
4 3 3 1
************ Matrice matC *****************
voici la matrice matC apres la modification :
1 0
1 1
0 0
0 1
************ matA x matC *****************
la multiplication entre matA et matC donne le resultat suivant :
1 4
6 6
3 2
4
Téléchargement