How to multiply a matrix by a vector

-1

Good evening I am finishing a program for the faculty and I am missing a part of the code to multiply a matrix by a vector, in this case it would be the multiplication of the quantity vector [i] [j] * prices [j]. (In the code is an attempt at multiplication but it does not work), if someone can give me some help, I would be very grateful, thank you very much. Here I leave the code walking.

#include<iostream>
using namespace std;

int main()
{
    int cantidad[3][5], precios[5], recaudacion[5], i, j, total;
    string nombresArticulos[5];

    //ingreso e imprime los nombres de los articulos
    i=0;
    while(i<5)
    {
        cout<<"Ingrese los nombres de los articulos:"<<endl;
        cin>>nombresArticulos[i];
        i++;
    }

    for(i=0; i<5; i++)
    {
        cout<<nombresArticulos[i]<<endl;
    }

    //guarda los precios de cada articulo
    for(j=0; j<5;j++)
    {
        cout<<"Ingrese precios de cada articulo"<<endl;
        cin>>precios[5];

    }

    //ingreso de la cantidad de articulos vendidos por cada vendedor
    cout<<"Ingrese la cantidad de articulos vendidos por cada vendedor: "<<endl;
    for(i=0; i<3; i++)
    {
        for(j=0; j<5; j++)
        {
            cout<<"Vendedor: "<<i+1<<" "<<"Articulo: "<<j+1<<endl;
            cin>>cantidad[i][j];
        }

    }

    for(i=0; i<3; i++)
    {
         for(j=0; j<5; j++)
        {
            //cout<<"Vendedor: "<<i+1<<" "<<"Articulo: "<<j+1<<endl;
            cout<<cantidad[i][j]<<endl;
        }

    }

    //cantidad de dinero recaudado por cada vendedor
    for(i=0; i<3; i++)
    {
        for(j=0; j<5; j++)
        {
            recaudacion[j]=cantidad[i][j]*precios[j];
        }

    }

    for(j=0; j<5; j++)
    {
        cout<<recaudacion[j]<<endl;
    }

}
    
asked by Cpp 30.09.2018 в 06:17
source

1 answer

0

You're supposed to be calculating the money raised by each vendor. And since in your program you define three sellers, the vector resultado must have 3 elements ... not 5.

int recaudacion[3];

Well, at this point, your exercise is solved by applying the theory of matrix multiplication. That is, given a 3x5 matrix and a 5x1 matrix, we will obtain a 3x1 matrix. Matrix multiplication theory here .

When multiplying matrices, in each cell of the result matrix not a single product is stored, but a sum of products ... it is important not to forget to initialize the variables:

for(int i=0; i<3; i++)
{
  recaudacion[i] = 0;
  for(int j=0; j<5; j++)
  {
    recaudacion[i] += cantidad[i][j]*precios[j];
  }
}

And, please, do not declare all the variables at the start of the function. Try to reduce your life to the fullest. You will get more readable programs with fewer errors.

    
answered by 01.10.2018 в 08:22