?
Solved

Matrix Programming with C++

Posted on 2005-05-06
5
Medium Priority
?
327 Views
Last Modified: 2013-11-17
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";
}

0
Comment
Question by:mrperfect75000
  • 3
5 Comments
 
LVL 5

Accepted Solution

by:
Greybird earned 500 total points
ID: 13943488
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
 
LVL 4

Expert Comment

by:smpoojary
ID: 13943973
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
 
LVL 4

Expert Comment

by:smpoojary
ID: 13944011
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 Comment

by:mrperfect75000
ID: 13946562
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
 
LVL 4

Expert Comment

by:smpoojary
ID: 13956827
Yes possible, But you have to ask administrator to update. While giving points you can distribute the points also,
With warm regards
-Mahesh
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deletiā€¦
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question