well i was supposed to make a c++ program which takes 2 matrices and multiples them(cross product thingy) to form a matrix 3 with those values.
so basically my program works for 2x2 - 3x3 ... but fails at high numbers for some unknown reason O.O ....
i've already submitted it so hopefully the professor only tries a 2x2 xD ... but still i wanna know where my mistake was !
Code: Select all
#include<iostream>
#include<string>
using namespace std;
int const max_rows=100;
int const max_columns=100;
void crossproduct(int array1[][max_columns],int array2[][max_columns],int array3[][max_columns], int row, int column);
void main()
{
int matrix1[max_rows][max_columns];
int matrix2[max_rows][max_columns];
int matrix3[max_rows][max_columns];
int row1;
int row2;
int row3;
int column1;
int column2;
int column3;
do
{
cout << "please enter the number of rows for matrix 1" << endl;
cin >> row1;
if(row1 >=100)
cout << "please enter a number less than 100" << endl;
}while(row1>=100);
do
{
cout << "please enter the number of columns for matrix 1" << endl;
cin >> column1;
if(column1 >=100)
cout << "please enter a number less than 100" << endl;
}while(column1>=100);
for(int i=0; i<row1; i++)
{
for(int j=0; j<column1; j++)
{
cout << "please enter the value of matrix1 [" << i+1 << "] [" << j+1 << "]" <<endl;
cin >> matrix1[i][j];
}
}
cout << "the number of rows for matrix 2 will equal the number of columns in matrix 1" << endl;
row2=column1;
do
{
cout << "please enter the number of columns for matrix 2" << endl;
cin >> column2;
if(column2 >= 100)
cout << "please enter a number less than 100" << endl;
}while(column2>=100);
for(int i=0; i<row2; i++)
{
for(int j=0; j<column2; j++)
{
cout << "please enter the values of matrix2 [" << i+1 << "] [" << j+1 << "]" <<endl;
cin >> matrix2[i][j];
}
}
row3 = row1;
column3 = column2;
crossproduct(matrix1,matrix2,matrix3,row3,column3);
for(int i=0; i<row3; i++)
{
for(int j=0; j<column3; j++)
{
cout << " the cross product matrix has the values [" << i+1 << "][" << j+1 << "] = " << matrix3[i][j] << endl ;
}
}
system ("pause");
}
void crossproduct(int array1[][max_columns],int array2[][max_columns],int array3[][max_columns], int row, int column)
{
int product;
int total=0;
for(int i=0; i< row; i++)
{
for(int j=0; j<column;j++)
{
for(int h=0; h<column;h++)
{
product = array1[i][h] * array2[h][i];
total = total+ product;
}
array3[i][j] =total;
total=0;
}
}
}