Hello y'all:
I have been given a problem regarding matrices. Im very new to this but I know that multidimensional arrays cannot be dynamic, so i set mine as follows...oh hell, heres my whole program...i think i have the basics, but i believe im screwing up when it comes to adding the actual numbers, i get an output of zero for everything i choose.
Any help would be appreciated, thanks in advance...
#include <iostream.h>
#include <math.h>
void choices1(int row1,int row2, int col1, int col2);
void add(int row1,int row2,int col1,int col2);
void sub(int row1,int col1,int row2,int col2);
void mult(int row1,int col1,int row2,int col2);
int main()
{
int choice,row1,row2,col1,col2,add1;
cout<<"Please enter the number of rows in matrix 1:"<<endl;
cin>>row1;
cout<<"Please enter the number of columns in matrix 1:"<<endl;
cin>>col1;
cout<<"Please enter the number of rows in matrix 2:"<<endl;
cin>>row2;
cout<<"Please enter the number of columns in matrix 2:"<<endl;
cin>>col2;
cout<<"Matrix 1 is a "<<row1<<" by "<<col1<<" matrix "<<endl;
cout<<"Matrix 2 is a "<<row2<<" by "<<col2<<" matrix "<<endl;
int A[20][20];
int B[20][20];
int C[20][20];
int D[20][20];
cout<<"Please enter the numbers in matrix 1, starting at top left"<<endl;
cout<<"and moving to the right"<<endl;
for (int i=0;i<row1;i++)
for (int j=0;j<col1;j++)
cin>>A[i][j];
cout<<"Please enter the numbers in matrix 2, starting at top left"<<endl;
cout<<"and moving to the right"<<endl;
for (int k=0;k<row1;k++)
for (int l=0;l<col1;l++)
cin>>A[k][l];
choices1(row1,row2,col1,col2);
cin>>choice;
if(choice=1)
add(row1,col1,row2,col2);
cout<<C[row1][col1];
choices1(row1,col1,row2,col2);
cin>>choice;
if(choice=2)
sub(row1,col1,row2,col2);
cout<<C[row1][col1];
choices1(row1,col1,row2,col2);
cin>>choice;
if(choice=3)
mult(row1,col1,row2,col2);
cout<<D[row2][col1];
choices1(row1,col1,row2,col2);
cin>>choice;
return 0;
}
void choices1(int row1,int row2, int col1, int col2)
{
if (row1==row2 && col1==col2 && col1==row2)
{
cout<<"Addition, Subtraction, and Multiplication are available"<<endl;
cout<<"Enter 1 for Addition"<<endl;
cout<<"Enter 2 for Subtraction"<<endl;
cout<<"Enter 3 for Multiplication"<<endl;
}
else if (row1==row2 && col1==col2 && col1!=row2)
{
cout<<"Only Addition and Subtraction are available"<<endl;
cout<<"Enter 1 for Addition"<<endl;
cout<<"Enter 2 for Subtraction"<<endl;
}
else if (row1==row2 && col1==row2)
{
cout<<"Only Multiplication is Available"<<endl;
cout<<"Enter 3 for Multiplication"<<endl;
}
else
cout<<"Try Again"<<endl;
}
void add(int row1,int row2,int col1,int col2)
{
int A[row1][col1];
int B[row2][col2];
int C[row1][col1];
for (int i=0;i<row1;i++)
for (int j=0;j<col1;j++)
{
C[i][j]=A[i][j]+B[i][j];
}
}
void sub(int row1,int col1,int row2,int col2)
{
int A[row1][col1];
int B[row2][col2];
int C[row1][col1];
for (int i=0;i<row1;i++)
for (int j=0;j<col1;j++)
{
C[i][j]=A[i][j]-B[i][j];
}
}
void mult(int row1,int col1,int row2,int col2)
{
int sum;
int A[row1][col1];
int B[row2][col2];
int D[row2][col1];
for(int i=0;i<row2;i++)
for(int j=0;j<col2;j++)
{
sum=0;
for(int k=0;k<col1;k++)
sum+=A[i][k]*B[k][j];
sum=D[i][j];
}
}