Ecrire un programme qui réalise l'addition de deux matrices A et B de mêmes dimensions N et M.
Rappel:
/ \ / \ / \ | a b c d | | a' b' c' d' | | a+a' b+b' c+c' d+d' | | e f g h | + | e' f' g' h' | = | e+e' f+f' g+g' h+h' | | i j k l | | i' j' k' l' | | i+i' j+j' k+k' l+l' | \ / \ / \ /
b) La matrice B est ajoutée à A.
a) Le résultat de l'addition sera mémorisé dans une troisième matrice C qui sera ensuite affichée.
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 |
#include <stdio.h> main() { /* Déclarations */ int A[50][50]; /* matrice donnée */ int B[50][50]; /* matrice donnée */ int C[50][50]; /* matrice résultat */ int N, M; /* dimensions des matrices */ int I, J; /* indices courants */ /* Saisie des données */ printf("Nombre de lignes (max.50) : "); scanf("%d", &N ); printf("Nombre de colonnes (max.50) : "); scanf("%d", &M ); printf("*** Matrice A ***\n"); for (I=0; I<N; I++) for (J=0; J<M; J++) { printf("Elément[%d][%d] : ",I,J); scanf("%d", &A[I][J]); } printf("*** Matrice B ***\n"); for (I=0; I<N; I++) for (J=0; J<M; J++) { printf("Elément[%d][%d] : ",I,J); scanf("%d", &B[I][J]); } /* Affichage des matrices */ printf("Matrice donnée A :\n"); for (I=0; I<N; I++) { for (J=0; J<M; J++) printf("%7d", A[I][J]); printf("\n"); } printf("Matrice donnée B :\n"); for (I=0; I<N; I++) { for (J=0; J<M; J++) printf("%7d", B[I][J]); printf("\n"); } /* Affectation du résultat de l'addition à C */ for (I=0; I<N; I++) for (J=0; J<M; J++) C[I][J] = A[I][J]+B[I][J]; /* Edition du résultat */ printf("Matrice résultat C :\n"); for (I=0; I<N; I++) { for (J=0; J<M; J++) printf("%7d", C[I][J]); printf("\n"); } return 0; |
b) La matrice B est ajoutée à A.
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 |
#include <stdio.h> main() { /* Déclarations */ int A[50][50]; /* matrice donnée et résultat */ int B[50][50]; /* matrice donnée */ int N, M; /* dimensions des matrices */ int I, J; /* indices courants */ /* Saisie des données */ printf("Nombre de lignes (max.50) : "); scanf("%d", &N ); printf("Nombre de colonnes (max.50) : "); scanf("%d", &M ); printf("*** Matrice A ***\n"); for (I=0; I<N; I++) for (J=0; J<M; J++) { printf("Elément[%d][%d] : ",I,J); scanf("%d", &A[I][J]); } printf("*** Matrice B ***\n"); for (I=0; I<N; I++) for (J=0; J<M; J++) { printf("Elément[%d][%d] : ",I,J); scanf("%d", &B[I][J]); } /* Affichage des matrices */ printf("Matrice donnée A :\n"); for (I=0; I<N; I++) { for (J=0; J<M; J++) printf("%7d", A[I][J]); printf("\n"); } printf("Matrice donnée B :\n"); for (I=0; I<N; I++) { for (J=0; J<M; J++) printf("%7d", B[I][J]); printf("\n"); } /* Addition de B à A */ for (I=0; I<N; I++) for (J=0; J<M; J++) A[I][J] += B[I][J]; /* Edition du résultat */ printf("Matrice résultat A :\n"); for (I=0; I<N; I++) { for (J=0; J<M; J++) printf("%7d", A[I][J]); printf("\n"); } return 0; } |