mrperfect75000
asked on
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";
}
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";
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
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?
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?
Yes possible, But you have to ask administrator to update. While giving points you can distribute the points also,
With warm regards
-Mahesh
With warm regards
-Mahesh
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