C Multiplication de la matrice de programmation - Manipulation matricielle et algorithme

Table des matières:

Anonim

Introduction à la multiplication matricielle en programmation C

Dans l'article C Programmation de la multiplication matricielle, une matrice est une grille utilisée pour stocker des données dans un format structuré. Il est souvent utilisé avec un tableau, où les données sont représentées en lignes horizontales et en colonnes verticales. Les matrices sont souvent utilisées dans les langages de programmation et sont utilisées pour représenter les données dans une structure graphique. En programmation, si l'utilisateur veut multiplier, ajouter, soustraire et diviser deux matrices, l'ordre de la matrice doit être déclaré en premier. Une fois que l'ordre de la matrice est déclaré pour la première et la deuxième matrice, les éléments (entrée) pour les matrices doivent être saisis par l'utilisateur. Si l'ordre de la matrice n'est pas proportionné les uns aux autres, alors le message d'erreur sera affiché qui est implanté par un programmeur dans l'énoncé de condition. Si une matrice ne contient qu'une seule ligne, elle est appelée vecteur de ligne et si elle ne contient qu'une seule colonne, elle est appelée vecteur de colonne.

Une matrice qui contient le même nombre de lignes et de colonnes, puis on l'appelle une matrice carrée. La matrice est utilisée pour stocker un groupe de données connexes. Certains langages de programmation sont utilisés pour prendre en charge les matrices en tant que type de données offrant plus de flexibilité qu'un tableau statique. Au lieu de stocker les valeurs dans une matrice, elles peuvent être stockées en tant que variable individuelle, un programme peut accéder et effectuer des opérations sur les données plus efficacement. En programmation C, les multiplications matricielles se font en utilisant des tableaux, des fonctions, des pointeurs. Par conséquent, nous allons discuter d'un algorithme pour la multiplication matricielle avec l'organigramme, qui peut être utilisé pour écrire le code de programmation pour la multiplication matricielle 3 × 3 dans un langage de haut niveau. Cette explication détaillée vous aidera à analyser le mécanisme de travail de la multiplication matricielle et vous aidera à comprendre comment écrire du code.

Algorithme de multiplication de matrice de programmation C

Étape 1: démarrez le programme.

Étape 2: Entrez la ligne et la colonne de la première (a) matrice.

Étape 3: Entrez la ligne et la colonne de la deuxième matrice (b).

Étape 4: Entrez les éléments de la première (a) matrice.

Étape 5: Entrez les éléments de la deuxième matrice (b).

Étape 6: Imprimez les éléments de la première (a) matrice sous forme matricielle.

Étape 7: Imprimez les éléments de la deuxième (b) matrice sous forme matricielle.

Étape 8: définissez une boucle jusqu'à la ligne.

Étape 9: définissez une boucle intérieure jusqu'à la colonne.

Étape 10: définissez une autre boucle intérieure jusqu'à la colonne.

Étape 11: Multipliez la première (a) et la seconde (b) matrice et stockez l'élément dans la troisième matrice (c)

Étape 12: Imprimez la matrice finale.

Étape 13: Arrêtez le programme.

Organigramme de la multiplication matricielle

Exemple de multiplication de matrice de programmation C

Le programme C effectue une multiplication matricielle, regardons quelques exemples.

Code:

#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)

Production:

Fonctionnement de la multiplication de la matrice de programmation C

  • Dans le programme ci-dessus, nous avons initialisé les variables et les tableaux à l'intérieur de la méthode principale en type de données entier (int).
  • Après la partie d'initialisation, nous obtenons l'ordre de la matrice de l'utilisateur pour la première matrice, puis simultanément l'utilisateur doit déclarer l'ordre de la deuxième matrice.
  • Une fois que l'ordre des matrices est déclaré, la partie condition s'exécute, le programme continue de s'exécuter uniquement si l'ordre satisfait la condition ou bien le programme est terminé ou arrêté sur cette partie elle-même.
  • Une fois la condition satisfaite, l'utilisateur doit saisir les éléments de la matrice comme entrées pendant l'exécution.
  • Selon la multiplication de matrice d'entrée utilisateur est calculée.
  • Le programme de matrice ci-dessus est simple et peut calculer la mise à jour 25 × 25, nous pouvons donc simplement éditer dans le tableau les nombres requis.

Avantages de la multiplication de la matrice de programmation C

  • Le langage de programmation C prend en charge la matrice comme type de données et offre plus de flexibilité. Et il consomme également moins de mémoire lors du traitement.
  • En stockant les valeurs dans une matrice plutôt qu'en tant que variables individuelles, le programme C peut accéder et effectuer des opérations sur les données plus efficacement.
  • Il est plus facile d'extraire des informations sur la rotation des objets, et aussi facile à manipuler dans le programme C.

Conclusion

La multiplication matricielle est utilisée à plusieurs reprises dans les programmes pour représenter une structure de données graphiques, qui est utilisée pour stocker plusieurs vecteurs et elle est également utilisée dans de nombreuses applications comme la résolution d'équations linéaires et plus encore. Beaucoup de recherches ont été faites sur la multiplication des matrices en utilisant un nombre minimum d'opérations.

Article recommandé

Ceci est un guide pour la multiplication de la matrice de programmation C. Ici, nous discutons du travail de la manipulation de la matrice, de l'algorithme, de l'organigramme et des exemples ainsi que des différents avantages de la programmation c. Vous pouvez également consulter nos autres articles suggérés pour en savoir plus -

  1. Introduction aux tableaux en programmation C
  2. Modèles en programmation C - (Exemples)
  3. D'entretiens chez C Programming | Top 13
  4. Qu'est-ce que le langage de programmation R?
  5. Tableaux dans la structure de données