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.

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.

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..

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];
#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;
}

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

Comments
And now you know - oh, you left with only one post - how utterly pointless of you

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.

Comments
Why are you posting on a 4-month-old thread?

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.

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.

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

Comments
I'll send it by postal mail. Just wait for it... And don't bump old threads

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

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

#include <iostream>
#include <string>
using namespace std;

class MyMatrixClass
{
    public:
        MyMatrixClass(int y, int x);
        void DisplayMatrix();
        void CreateMatrix();
        void resultCell(int x, int y, double z);
    protected:
    private:
        string Name;
        int rows;
        int columns;
    public:
        double cell[100][100];
};

int main()
{
    cout << "This program is designed for matrix multiplication\n\n"
    << "Currently you are goign to create the matrix class\n"
    << "Enter the number of Rows you want in your First matrix Object\n\n: ";
    int NoOfRows;
    cin >> NoOfRows;
    cout << "\n\nEnter the number of Columns you want in your First matrix Object\n\n: ";
    int NoOfColumns;
    cin >> NoOfColumns;
MyMatrixClass first(NoOfRows,NoOfColumns);
first.DisplayMatrix();
first.CreateMatrix();
cout << "\n\n Matrix Creation sucessful now get ready for\n"
<<"\nCreating your second Matrix.\n"
<<"As per the matrix multiplication rule\n"
<<"Setting Number of rows of Second Matrix Equal to No of Columns of First Matrix\n"
<<"First Matrix (No of Columns) = Second Matrix (No of Rows)\n";
int NoOfColumns2;
cout << "\n\nEnter the number of columns for the second Matrix\n:";
cin >> NoOfColumns2;
MyMatrixClass second(NoOfColumns,NoOfColumns2);
second.DisplayMatrix();
second.CreateMatrix();
MyMatrixClass ResultMatrix(NoOfRows,NoOfColumns2);
ResultMatrix.DisplayMatrix();

double Temporary;

    for (int index5 = 0; index5 < NoOfRows; index5++){

        for (int index6 = 0; index6 < NoOfColumns2; index6++){
            Temporary = 0;
                for (int index7 = 0; index7 < NoOfColumns; index7++){
                    Temporary += first.cell[index5][index7] * second.cell[index7][index6];
                }
                //cout << Temporary << endl;
            ResultMatrix.resultCell(index5,index6,Temporary);
        }
    }
cout << "\n\n\n\n\n\n\nMatrix multiplication is comlpeted";
first.DisplayMatrix();
cout << "\n\n Multiplied by\n\n";
second.DisplayMatrix();
cout << "\n\nComes to ";
ResultMatrix.DisplayMatrix();

cout << "\n\n This program is created by Sudarshan Kondgekar. \nI hope you enjoyed the program.\n";

}

using namespace std;

MyMatrixClass::MyMatrixClass(int y, int x)
:rows(y), columns(x)
{
cout << "\nYour Matrix is having \n"
<< rows <<" No of Rows \n"
<< columns << " No of columns \n";
string N;
cout << "Enter the name for your Matrix \n:";
cin >> N;
Name = N;

    for (int index1 = 0; index1 < rows ; index1++){

        for (int index2 = 0; index2 < columns ; index2++){
            cell[index1][index2]=0;
        }

    }

}

void MyMatrixClass::DisplayMatrix(){

cout << "\n\nMatrix " << Name << endl;

    for (int index3 = 0; index3 < rows ; index3++){

        for (int index4 = 0; index4 < columns ; index4++){
            cout << MyMatrixClass::cell[index3][index4] << "\t";
        }
        cout << "\n";

    }


}

void MyMatrixClass::CreateMatrix(){

cout << "\nNow you are going to enter the values inside this matrix\n"
<< "The values will be automatically updated as soon as you press enter\n"
<< "Let's Start\n\n Remember the value can be any number but \n it should not be a string\n Press ctrl+c to close this program";
for (int index = 0; index < rows ; index++){

        for (int index2 = 0; index2 < columns ; index2++){
            cout << "\n\nEnter the cell[" << index << "][" << index2 << "]\n :";
            cin >> MyMatrixClass::cell[index][index2];
            MyMatrixClass::DisplayMatrix();

        }
        cout << "\n";

    }


}

void MyMatrixClass::resultCell(int x,int y,double z){

MyMatrixClass::cell[x][y] = z;

}

I Hope the source code is not long

Edited 4 Years Ago by skondgekar

This question has already been answered. Start a new discussion instead.