Multiplication of Matrices in C

0

Hi, I have a Problem with the multiplication of matrices, when I want to multiply a 2x2 by a 2x10, it throws me incorrect values. I appreciate the help ...

#include <stdio.h>
#include <stdlib.h>

// constantes que se aplican a matrices 2 x 2

const int ROW2=2;
const int COLUMNS2=2;

// constantes que se aplican a matrices 2 x 10

const int COLUMNAS10=10;

void multiplicaciondematrices (int matriz [ROW2][COLUMNS2], int matrizx10 [ROW2][COLUMNAS10], int matrixfinal[ROW2][COLUMNAS10]);// Prototipo de multiplicar una matriz 2 x 2 por una 2 x 10
void cargamatriz2x10 (int matrixrecargada [ROW2][COLUMNAS10] ); // prototipo que carga la matrix 2 x 10
void muestramatriz2x10 ( int mmatrixrecargada [ROW2] [COLUMNAS10]); // prototipo de la funcion que muestra la matriz 2 x 10
void cargamatriz2x2 (int matriz [ROW2][COLUMNS2] );// Prototipo que carga la matrix 2x2


int main()
{

    // Se declaran las variables que se van a usar en la funcion que multiplica la matriz 2 x 2 por una 2 x 10

    int matrizfinal [ROW2][COLUMNAS10];
    int matrizx10 [ROW2][COLUMNAS10];
    int matriz[ROW2][COLUMNS2];

    // Llamado a las funciones de carga de matrices

    printf(" Cargue una matriz de 2 x 2 \n\n");
    cargamatriz2x2(matriz);
    printf(" Cargue una matriz de 2 x 10\n\n");
    cargamatriz2x10(matrizx10);

    multiplicaciondematrices(matriz, matrizx10,matrizfinal);
    muestramatriz2x10(matrizfinal);


    return 0;
}


// Funcion que carga una matriz de 2 x 2

void cargamatriz2x2 (int matriz [ROW2][COLUMNS2] )
{
    int i=0;
    int j=0;

    system("cls");
    for (i=0; i<ROW2; i++)
    {
        for (j=0; j<COLUMNS2; j++)
        {
            printf("Ingres un Valor Entero en la posicion fila(%d): columna(%d): ",i,j);
            scanf(" %d",&matriz [i][j]);
        }
    }
}

// Funcion que carga una matriz 2 x 10
void cargamatriz2x10 (int matrizx10 [ROW2][COLUMNAS10] )
{
    int i=0;
    int j=0;

    system("cls");
    for (i=0; i<ROW2; i++)
    {
        for (j=0; j<COLUMNAS10; j++)
        {
            printf("Ingres un Valor Entero en la posicion fila(%d): columna(%d): ",i,j);

            scanf(" %d",&matrizx10 [i][j]);
        }
    }
}

// Funcion que multiplica una matriz 2x2 por una 2 x 10

void multiplicaciondematrices (int matriz [ROW2][COLUMNS2], int matrizx10 [ROW2][COLUMNAS10], int matrizfinal[ROW2][COLUMNAS10])
{
    int i=0;
    int u=0;
    int k=0;

    for(i=0; i<ROW2; i++)
    {
      for(u=0; u<COLUMNS2; u++)
        {
            matrizfinal[i][k]=0;
            for(k=0; k<COLUMNAS10; k++)
            {
                matrizfinal[i][k]+=(matriz[i][u]*matrizx10[u][k]);
            }
        }
    }
}

// Funcion que muestra una matriz 2 x 10

void muestramatriz2x10 ( int matriz [ROW2] [COLUMNAS10])
{
    int i=0;
    int j=0;

    system("cls");


    for (i=0; i<ROW2; i++)
    {
        printf("\n \n \t");
        for (j=0; j<COLUMNAS10; j++)
        {
            if (matriz [i][j]<9)
            {
                printf(" ");
                printf(" %d ",matriz[i][j]);

            }
            else
            {
                printf(" %d ",matriz[i][j]);
            }

        }
        printf("\n \n");

    }
}
    
asked by juanmabeach 18.05.2018 в 05:35
source

1 answer

1

The error seems to be in the function multiplicaciondematrices, concretely in the sheath for iterating with the variable u in the line that initializes the final matrix [i] [k].

for(u=0; u<COLUMNS2; u++)
{
  matrizfinal[i][k]=0; // Aquí
  for(k=0; k<COLUMNAS10; k++)
  {
    matrizfinal[i][k]+=(matriz[i][u]*matrizx10[u][k]);
  }
}

In the first iteration of the cycle, the variable k is initialized to 0. However, in the next iteration, k has the value that caused it to leave the internal for, that is, 10. Therefore, the line mentioned is equal to

matrizfinal[i][10] = 0;

Which is an access outside the range of the matrix (the maximum is 9) while you leave without initializing the box you really intended. How are you leaving trash in the final matrix the results are wrong.

A possible restructuring of your cycles that would correct your problem is the following:

    for(int u=0; u < COLUMNAS10; u++)
    {
        matrizfinal[i][u] = 0;

        for(int k=0; k < ROW2; k++)
        {
            matrizfinal[i][u] += (matriz[i][k]*matrizx10[k][u]);
        }
    }
    
answered by 18.05.2018 / 09:53
source