0

i want to use this method to multiply a matrix by a vector introducing my own cin>> for rows and colums for the matrix and for the vector data as well, this is my multiplying method.

int multiply()
{
    int a[3][3]= //MY OWN INPUT
    int b[]={1,2,3}; //MY OWN INPUT
    int c[3]; //SOLUTION

    for (int i=0;i<3;i++)
    {
         c[i]=0;
    }

    for (int i=0;i<3;i++)
    {
        for (int j=0;j<3;j++)
        {
            c[i]+=(a[i][j]*b[j]);
        }
    }

    for (int i=0;i<3;i++){
        cout<<c[i]<<"  "<<endl;
    }
    system ("pause>0");

}

I suppose that for the matrix, reading the input would be something like this,

void leerMatriz( int **M, int fil, int col )
{
    cout << "\nRellenar la matriz:\n";
    for( int i = 0; i < fil; i++ ){
        for( int j = 0; j < col; j++ ){
            cout << "Matriz[" << i << "][" << j << "]: ";
            cin >> M[i][j];
        }
    }
}
int main()
{
    int filA, colA, filB, colB;



    cout << "\nDimension de la matriz A:";
    cout << "\nFilas de la matriz: "; cin >> filA;
    cout << "Columnas de la matriz: "; cin >> colA;

    int** A = new int*[filA];
    for( int i = 0; i < filA; i++ )
        A[i] = new int[colA];

    leerMatriz( A, filA, colA );

    cout << "\nDimension de la matriz B:";
    cout << "\nFilas de la matriz: "; cin >> filB;
    cout << "Columnas de la matriz: "; cin >> colB;

    int** B = new int*[filB];
    for( int i = 0; i < filB; i++ )
        B[i] = new int[colB];

    leerMatriz( B, filB, colB );

but what about the vector? are there any eassier way?

  • You succeded in reading a matrix. So what is your difficulty in reading a vector? Please also note that you must provide the matrix, the vector and the sizes to the `multiply` function – Damien Jan 22 '20 at 14:51

1 Answers1

0

There are a few problems with the code you have shared with us but here are some hints for writing this :

1) Avoid using raw pointers as your are doing in your main, may be take a look at something like this A proper way to create a matrix in c++ where a class is defined instead. That class could have a 'getInput' function for example that would capture input from the user. You could write something similar is for the vector as well.

2) As I'm sure you know, in matrix vector product, size matters. Make sure your vector size corresponds to the number of rows of your matrix. And make sure sizes are initialized on your program.

3) When taking multi element input as the values of a list through the standard input it is important to define a separator character. (the vector [0,1,2,3,4] could be written as "0,1,2,3,4") Then you would have to parse that string into values. Or simply get your input character by character.

4) Your multiply fonction needs to be "adaptable" according to your input right? So it has to take some parameters as input.

In c++ it's easy to get lost with non-mathematical question and it's easy to do "bad" code, go easy, start with a multiplication of two vectors may be?

Luisjomen2a
  • 241
  • 2
  • 16