mercredi 22 janvier 2020

Multiply a matrix by a vector with my own input

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?

Aucun commentaire:

Enregistrer un commentaire