Matrix-Vector Multiplication using friend function


Implementation of Matrix-Vector Multiplication using friend function

Aim:

To write C++ program to define matrix and vector class, to use function with default argument and to do matrix-vector multiplication using friend function.

Program:

#include<iostream.h>

#include<conio.h>

class vector; //class declaration – needed, because this class referred in the friend function before it is defined .

class matrix

{

int m[3][3];

public:

void getmatrix(void);

void dismatrix(void);

friend void multiply(matrix &, vector &);

};

class vector

{

int v[10];

public:

//default argument is 3

void getvector(int n=3);

void disvector(void);

friend void multiply(matrix &, vector &);

};

void vector::getvector(int n)

{

int i;

cout<<“\nEnter elements for vector one by one…\n”;

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

cin>>v[i];

}

void vector::disvector()

{

int i;

cout<<“\nThe vector elements are…\n”;

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

cout<<v[i]<<“\t”;

}

void matrix::getmatrix()

{

int i,j;

cout<<“\nEnter the matrix…\n”;

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

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

cin>>m[i][j];

}

void matrix::dismatrix()

{

int i, j;

cout<<“\nEntered matrix is…\n”;

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

{

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

cout<<m[i][j]<<“\t”;

cout<<“\n”;

}

}

void multiply(matrix &m1, vector &v1)

{

int ans[3], i, j;

cout<<“\nThe resultant matrix…\n”;

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

{

ans[i]=0;

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

ans[i]+=m1.m[i][j] * v1.v[j];

cout<<ans[i]<<“\t”;

}

}

void main()

{

matrix m1;

vector v1;

clrscr();

m1.getmatrix();

m1.dismatrix();

v1.getvector(); //no argument here.  so, default value will be assigned

v1.disvector();

multiply(m1,v1);

getch();

}

About these ads

10 Comments

  1. It would be better if the friend function returns a vector as a result to the main function.

    • if you think so then convert program…
      i just want to give the program as simple as possible.
      Try yourself and convert the program as you said. If you understand the program it is very easy.

  2. The interface and implementation can be separated.
    Turbo C++ or Borland C++ is not a good compiler. Nobody uses it in the industry.
    Any Linux/Unix and Microsoft VC++ are good compilers.

    • I agree with that…
      i don’t want to confuse the average and below average students by doing that.
      If i include namespace and other new stuffs in the program, definitely it will confuse the students those who are still working with Turbo C++ and Borland C++.
      Thanks for ur comment

  3. Add actual size of matrices also

    • I didn’t understand your comment…
      In this program, 3 is the default size for the matrix. The program can be modified to use any size of matrix and vector.
      The program can be modified to create the matrix dynamically. But, I think that is too much for the experiment given in the syllabus of Anna University.

  4. You have pass by reference while passing objects which is a good thing.

  5. can u explain me why we are declaring as (matrix &,vector &)… wat is the function performed by &…..the program was very simple .. the ly doubt was this….

  6. Sir,
    The program are simple and easy to execute kindly provide me the program for all the 10 ex cerise in OOPs.Please SIr.


Sorry, the comment form is closed at this time.

Comments RSS

Follow

Get every new post delivered to your Inbox.

Join 73 other followers