C Multiplicación de la matriz de programación - Matriz de manipulación y algoritmo

Tabla de contenido:

Anonim

Introducción a la multiplicación matricial en la programación en C

En el artículo C Multiplicación de matriz de programación, una matriz es una cuadrícula que se utiliza para almacenar datos en un formato estructurado. A menudo se usa con una tabla, donde los datos se representan en filas horizontales y columnas verticales. Las matrices se usan a menudo en lenguajes de programación y se usan para representar los datos en una estructura gráfica. En la programación, si el usuario quiere multiplicar, sumar, restar y dividir dos matrices, entonces el orden de la matriz debe declararse primero. Una vez que se declara el orden de la matriz para la primera y la segunda matriz, el usuario necesita ingresar los elementos (entrada) para las matrices. Si el orden de la matriz no es proporcional entre sí, se mostrará el mensaje de error implantado por un programador en la declaración de condición. Si una matriz contiene solo una fila, se llama vector de fila, y si contiene solo una columna, se llama vector de columna.

Una matriz que contiene el mismo número de filas y columnas, luego se llama matriz cuadrada. Matrix se usa para almacenar un grupo de datos relacionados. Algunos de los lenguajes de programación se utilizan para admitir matrices como un tipo de datos que ofrece más flexibilidad que una matriz estática. En lugar de almacenar los valores en una matriz, puede almacenarse como una variable individual, un programa puede acceder y realizar operaciones en los datos de manera más eficiente. En la programación en C, las multiplicaciones de matrices se realizan mediante matrices, funciones, punteros. Por lo tanto, vamos a analizar un algoritmo para la multiplicación de matrices junto con el diagrama de flujo, que puede usarse para escribir código de programación para la multiplicación de matrices 3 × 3 en un lenguaje de alto nivel. Esta explicación detallada lo ayudará a analizar el mecanismo de trabajo de la multiplicación de matrices y le ayudará a comprender cómo escribir código.

Algoritmo de Multiplicación de Matriz de Programación C

Paso 1: Inicia el programa.

Paso 2: Ingrese la fila y la columna de la primera (a) matriz.

Paso 3: Ingrese la fila y la columna de la segunda (b) matriz.

Paso 4: Ingrese los elementos de la primera (a) matriz.

Paso 5: Ingrese los elementos de la segunda (b) matriz.

Paso 6: Imprima los elementos de la primera (a) matriz en forma de matriz.

Paso 7: Imprima los elementos de la segunda (b) matriz en forma de matriz.

Paso 8: establezca un bucle hasta la fila.

Paso 9: establezca un bucle interno hasta la columna.

Paso 10: establezca otro bucle interno hasta la columna.

Paso 11: Multiplique la primera (a) y la segunda (b) matriz y almacene el elemento en la tercera matriz (c)

Paso 12: imprima la matriz final.

Paso 13: detener el programa.

Diagrama de flujo de la multiplicación de matrices

Ejemplo de multiplicación de matriz de programación en C

El programa C realiza la multiplicación de matrices, veamos algunos ejemplos.

Código:

#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));
)
)

Salida:

Funcionamiento de la multiplicación de la matriz de programación en C

  • En el programa anterior, hemos inicializado las variables y las matrices dentro del método principal en el tipo de datos entero (int).
  • Después de la parte de inicialización, estamos obteniendo el orden de la matriz del usuario para la primera matriz, luego, simultáneamente, el usuario debe declarar el orden de la segunda matriz.
  • Una vez que se declara el orden de las matrices, la parte de la condición se ejecutará, el programa continuará ejecutándose solo si la orden cumple la condición o si el programa se terminará o se detendrá en esa parte.
  • Una vez que se cumple la condición, el usuario debe ingresar los elementos de la matriz como entradas durante el tiempo de ejecución.
  • Según la matriz de entrada del usuario, se calcula la multiplicación.
  • El programa de matriz anterior es simple y puede calcular la actualización 25 × 25, por lo que simplemente podemos editar en la matriz a los números requeridos.

Ventajas de la multiplicación de la matriz de programación en C

  • El lenguaje de programación C admite la matriz como tipo de datos y ofrece más flexibilidad. Y también consume menos memoria durante el procesamiento.
  • Al almacenar valores en una matriz en lugar de como variables individuales, el programa C puede acceder y realizar operaciones en los datos de manera más eficiente.
  • Es más fácil extraer información sobre la rotación de objetos, y también es fácil de manipular en el programa C.

Conclusión

La multiplicación de matrices se usa repetidamente en programas para representar una estructura de datos gráficos, que se usa para almacenar múltiples vectores y también se usa en muchas aplicaciones como resolver ecuaciones lineales y más. Se han realizado muchas investigaciones sobre la multiplicación de matrices utilizando un número mínimo de operaciones.

Artículo recomendado

Esta es una guía para la multiplicación de la matriz de programación en C. Aquí discutimos el trabajo de manipulación de matriz, algoritmo, diagrama de flujo y ejemplos junto con diferentes ventajas en la programación en C. También puede consultar nuestros otros artículos sugeridos para obtener más información:

  1. Introducción a las matrices en la programación en C
  2. Patrones en la programación en C - (Ejemplos)
  3. C Preguntas de la entrevista de programación | Top 13
  4. ¿Qué es el lenguaje de programación R?
  5. Matrices en estructura de datos