Solved

# Matrix Programming with C++

Posted on 2005-05-06

I have this Program in C++ that multiplies a Matrix by a vector. I can get it to work if i want to enter the elements of the matrix each time i run the program. But i dont want that. I want to define the elements of the matrix permanently in the code. So i dont have to enter it each time i test run the code. How can i do this?

Thank You !

Here is the code...

#include <iostream>

using namespace std;

#define MAX 5

void mulmatvec(int m, int n, double a[MAX][MAX], double x[MAX], double b[MAX]){

int i,j;

for(i = 0; i < m; i++){

b[i] = 0.;

for(j = 0; j < n; j++)

b[i] += a[i][j]*x[j];

}

return;

}

int main(){

int i,j,m,n;

double a[MAX][MAX], x[MAX], b[MAX];

cout << "Enter the number of rows in the matrix \n";

cin >> m;

cout << "Enter the number of columns in the matrix \n";

cin >> n;

cout << "Enter the matrix by rows\n";

for(i = 0; i < m; i++)

for(j = 0; j < n; j++)

cin >> a[i][j];

cout << "Enter the vector\n";

for(j = 0; j < n; j++)

cin >> x[j];

mulmatvec(m,n,a,x,b);

cout << "\nA*x = \n";

for(i = 0; i < m; i++)

cout << b[i] << "\n";

}