# Matrix Programming with C++

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";
}

###### Who is Participating?

Commented:
if the matrix size is always the same, you just have to do :
double a[5][5] = {a,b,c,d,e,f,g,h,i,j,k,l,m,n,...};

Elements a, b, c, ... fill the table by incrementing last dimension first.
ie a is a[0][0], b is a[0][1], f is a[1][0]... and so on.
0

Commented:
Inside the main() change the code as

int main(){
int i,j,m=5,n=5;

double a[MAX][MAX]=
{ 1,2,3,4,5,
6,7,8,9,10,
11,12,13,14,15,
16,17,18,19,20,
21,22,23,24,25};
double x[MAX]={3,6,9,2,4};
double b[MAX];

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

cout << "\nA*x = \n";
for(i = 0; i < m; i++)
cout << b[i] << "\n";
}

-Mahesh
0

Commented:
otherway for declear a[MAX][MAX]

double a[MAX][MAX]=
{ {1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15},
{16,17,18,19,20},
{21,22,23,24,25}};
-Mahesh
0

Author Commented:
How can i give you some points smpoojary, I had to give Greybird
because he answered first and i used his first. But yours helped a lot in understanding it, is there a way i can share points?
0

Commented:
Yes possible, But you have to ask administrator to update. While giving points you can distribute the points also,
With warm regards
-Mahesh
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.