Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,990 questions

51,935 answers

573 users

How to multiply matrix by vector in C++

2 Answers

0 votes
#include <iostream>

#define COLS 3
#define ROWS 2
 
void matrix_x_vector(int matrix[][COLS], int vector[], int multiplied_array[]) {
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            multiplied_array[i] += matrix[i][j] * vector[j];
        }
    }
}
 
 
int main()
{
    int vector[COLS] = {3, 4, 3};
    int matrix[ROWS][COLS] = {
            {0, 3, 5},
            {5, 6, 2}};
    int multiplied_array[2] = {0};
 
 
    matrix_x_vector(matrix, vector, multiplied_array);
 
    for (int i = 0; i < ROWS; i++)
        std::cout << multiplied_array[i] << ' ';
}
 
 
 
/*
run:
 
27 45 
 
*/

 



answered Mar 23, 2022 by avibootz
0 votes
#include <iostream>
#include <vector>

#define COLS 3
#define ROWS 2
 
void matrix_x_vector(int matrix[][COLS], const std::vector<int> &v, int multiplied_array[]) {
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            multiplied_array[i] += matrix[i][j] * v[j];
        }
    }
}
 
 
int main()
{
    std::vector<int> v = {3, 4, 3};
    int matrix[ROWS][COLS] = {
            {0, 3, 5},
            {5, 6, 2}};
    int multiplied_array[2] = {0};
 
 
    matrix_x_vector(matrix, v, multiplied_array);
 
    for (int i = 0; i < ROWS; i++)
        std::cout << multiplied_array[i] << ' ';
}
 
 
 
 
/*
run:
 
27 45 
 
*/

 



answered Mar 23, 2022 by avibootz

Related questions

1 answer 130 views
130 views asked Mar 23, 2022 by avibootz
1 answer 109 views
109 views asked Mar 28, 2022 by avibootz
1 answer 128 views
128 views asked Mar 27, 2022 by avibootz
1 answer 76 views
76 views asked Mar 24, 2022 by avibootz
1 answer 87 views
2 answers 156 views
...