954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ Matrix Multiplication

Hey guys, I am incredibly new to c++ and need a little help on a matrix multiplyer program...

#include <iomanip> 
#include <iostream>
using namespace std;
int main()
{

int **mat1;
int **mat2;
int **result;
int row,col;

cout<<"Please enter row/col"<<endl;
cin>>row>>col;

mat1 = new int *[row];
mat2 = new int *[row];
result = new int *[row];
int k,i,j;

for (k=0; i<row; k++)
    mat1[k] = new int[col];
    mat2[k] = new int[col];
    result[k] = new int[col];
    
              for (i=0; i<row; i++)
                  for (j=0; j<col; j++)
                      for (j=0; j<row; j++)
                          for(i=0; i<col; i++)
                          result[i][k] += (mat1 [i][k] * mat2[k][j]);

cout<<setw(4)<<result[i][k];


I know obviously that I need 3 arrays, 1 and 2 to multiply and 3 to store the result. I'm just really bad at reading nested loops... and c++ in general. any help would be appreciated.

Etniespr101
Newbie Poster
9 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

Where do you input values into the arrays, prior to running the multiply code?

It might run as written, but it will surely produce a bunch of garbage output.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

hey thx for the reply. I input values into the the arrays at the beginning where the user inputs. im guessing your saying that my arrays have no value as of right now because i'm not giving it anything... im confused..

Etniespr101
Newbie Poster
9 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

Where do you input values into the arrays, prior to running the multiply code?

It might run as written, but it will surely produce a bunch of garbage output.

i added a little bit...

#include <iomanip> 
#include <iostream>
using namespace std;
int main()
{

int **mat1;
int **mat2;
int **result;
int row,col;

cout<<"Please enter row/col"<<endl;
cin>>row>>col;

mat1 = new int *[row];
mat2 = new int *[row];
result = new int *[row];
int k,i,j;

for (k=0; k<row; k++)
    mat1[k] = new int[col];
    mat2[k] = new int[col];
    result[k] = new int[col];
    
              for (i=0; i<row; i++)
                 for (j=0; j<col; j++)
                    mat1[k][i] = k + i;
                      for (j=0; j<row; j++)
                          mat2[i][k] = j + k;
                                     for(i=0; i<col; i++)
                                     result[i][k] += (mat1 [i][k] * mat2[k][j]);

cout<<setw(4)<<result[i][k];
Etniespr101
Newbie Poster
9 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 
#include <iostream.h>
#include <conio.h>

void getResult(double a[100][100], double b[100][100], double mVal, double pVal, double nVal)
{
		for(int i=0; i<mVal; i++)
   	{
   		for(int j=0; j<pVal; j++)
      	{
      		int res = 0;
      		for(int k=0; k<nVal; k++)
         		res += a[i][k]*b[k][j];
            	cout<<res<<"\t";
      	}
      	cout<<endl;
   	}
}

int main()
{
double a[100][100];
double b[100][100];
double mValue;
double nValue;
double pValue;
char ans;
char response;

do{
cout<<"\t\tM A T R I X   M U L T I P L I C A T I O N\n\n\n";
cout<<"This program will compute the product of two matrices.\n";
cout<<"\nAmxn and Bnxp, \n\nwhere m and p less than or equal to n (m, p <= n )\n";
cout<<"To use the program, just follow what the program asks.";
cout<<"\n\n\nNOTE:\n\tTo type the value or entries of a matrix just follow this; ";
cout<<"\n\n\t  1. You can type like this 1 2 3 4 5 6 . . . n\n";
cout<<"\n\t  2. or you can type like this \n\n\t    a11 a12 . . . a1n\n\t    a21 a22 . . . a2n \n\t    . . .";
cout<<"\n\t    .\n\t    .\n\t    .\n\t    am1 am2 . . . amxn or nxp\n\n";
cout<<"\nMAXIMUM DIMENSIONS = 10 X 10 (RECOMMENDED)\n";
cout<<"\nYOU CAN ALSO USE UP TO 100 X 100 DIMENSIONS\n\n\n";
cout<<"Want to use the program? : \n";
cout<<"type 'y' to continue and press any key to EXIT then press ENTER: ";
cin>>response;

	if((response=='y')||(response=='Y'))
   {
 		cout<<"\n\nEnter the value of m: ";
		cin>>mValue;
      while(mValue<=0)
   	{
      	cout<<"\n\nINCORRECT dimension(s), no negative or zero dimension(s) in a matrix\n";
   		cout<<"\nEnter the value of m greater than "<<mValue<<" : ";
   		cin>>mValue;
   	}
		cout<<"\nEnter the value of p: ";
		cin>>pValue;
      while(pValue<=0)
   	{
      	cout<<"\n\nINCORRECT dimension(s), no negative or zero dimension(s) in a matrix\n";
   		cout<<"\nEnter the value of p greater than "<<pValue<<" : ";
   		cin>>pValue;
   	}
		cout<<"\nEnter the value of n: ";
		cin>>nValue;
      while(nValue<0)
   	{  nValue = 0;
   		cout<<"\nEnter the value of n greater than "<<nValue<<" and less than "<<pValue<<" or "<<mValue<<" : ";
      	cin>>nValue;
   	}
      while((nValue>mValue)||(nValue>pValue))
   	{
         if(mValue>pValue)
         {
 				cout<<"\nERROR:\n\tValue of n must be less than or equal to "<<pValue<<"\n\n";
 				cout<<"Enter the value of n: ";
				cin>>nValue;
         }
         else
         {
         	cout<<"\nERROR:\n\tValue of n must be less than or equal to "<<mValue<<"\n\n";
 				cout<<"Enter the value of n: ";
				cin>>nValue;
         }
 		}


				cout<<"\n\nEnter a matrix with this dimension(s) A "<<mValue<<"x"<<nValue<<": \n";
				cout<<"\n\nType "<<(nValue*mValue)<<" entries for matrix A: \n\n";

            for(int i=0; i<mValue; i++)
   			{
      			for(int j=0; j<nValue; j++)
      				cin>>a[i][j];
   			}

				cout<<"\nThe value of matrix A that you have entered: \n\n";
   			for(int i=0; i<mValue; i++)
   			{
      	  		for(int j=0; j<nValue; j++)
      				cout<<a[i][j]<<"\t";
         			cout<<endl;
   			}
				cout<<"\n\nEnter a matrix with this dimension(s) B "<<nValue<<"x"<<pValue<<": \n";
				cout<<"\n\nType "<<(nValue*pValue)<<" entries for matrix B: \n\n";

				for(int i=0; i<nValue; i++)
   			{
   				for(int j=0; j<pValue; j++)
      				cin>>b[i][j];
   			}
   			cout<<"\nThe value of matrix B that you have entered: \n\n";
   			for(int i=0; i<nValue; i++)
   			{
   				for(int j=0; j<pValue; j++)
    		  		cout<<b[i][j]<<"\t";
   		      	cout<<endl;
   			}
				cout<<"\n\n\nThe PRODUCT is: \n\n";
   			getResult(a,b,mValue,pValue,nValue);
      }
      else
      	break;

   cout<<"\n\nTry again? \n\nType y to try again and press any key to stop then press ENTER. : ";
   cin>>ans;
}while((ans=='y')||(ans=='Y'));
cout<<"\n\n\nThanks for using this program.";
cout<<"\n\nProgrammer:\n\n\tMR. MOHAMMAD FAIDZ UY UBPON";
cout<<"\n\tBS COMPUTER SCIENCE \n\tMindanao State University\n\tCollege of Information Technology";

cin.get();
cin.get();
return 0;
}
mfubpon
Newbie Poster
1 post since Dec 2008
Reputation Points: 10
Solved Threads: 1
 

please use code tags

daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
 

And why are you posting in a thread last posted to in October 2007?

Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
 

Just checking to see if how Quick Reply works.
I'm new to DaniWeb. :P

JProgrammer
Newbie Poster
1 post since Nov 2009
Reputation Points: 6
Solved Threads: 1
 

Hey guys, I am incredibly new to c++ and need a little help on a matrix multiplyer program...

#include <iomanip> 
#include <iostream>
using namespace std;
int main()
{

int **mat1;
int **mat2;
int **result;
int row,col;

cout<<"Please enter row/col"<<endl;
cin>>row>>col;

mat1 = new int *[row];
mat2 = new int *[row];
result = new int *[row];
int k,i,j;

for (k=0; i<row; k++)
    mat1[k] = new int[col];
    mat2[k] = new int[col];
    result[k] = new int[col];
    
              for (i=0; i<row; i++)
                  for (j=0; j<col; j++)
                      for (j=0; j<row; j++)
                          for(i=0; i<col; i++)
                          result[i][k] += (mat1 [i][k] * mat2[k][j]);

cout<<setw(4)<<result[i][k];

I know obviously that I need 3 arrays, 1 and 2 to multiply and 3 to store the result. I'm just really bad at reading nested loops... and c++ in general. any help would be appreciated.

The problem is that you did not close the bracket so you are getting the errors. Also when I test the program I could only use the integers 1 and 0, is that how it should be? Try it and if still have problems, then reply back.

techboy2099
Newbie Poster
2 posts since Mar 2010
Reputation Points: 10
Solved Threads: 1
 

you need to close the code with the bracket and when I ran the program, I was only able to use the integers 1 and 0. Is that how the program should be? try the program and if any problems reply back.

techboy2099
Newbie Poster
2 posts since Mar 2010
Reputation Points: 10
Solved Threads: 1
 
Try it and if still have problems, then reply back.

It's been over 2.5 years. (S)He has probably solved the problem by now.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Where do you input values into the arrays, prior to running the multiply code?

It might run as written, but it will surely produce a bunch of garbage output.


plz send a program in C++ by using array matix multipication

Sardar Noman
Newbie Poster
1 post since May 2010
Reputation Points: 9
Solved Threads: 0
 
plz send a program in C++ by using array matix multipication

You didn't even bother toread the rest of the thread did you?
Closed. :icon_frown:

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You