To Convert the Code to C is pretty simple, not much to do actually.
C++
Code:
#include <stdafx.h> // dont know why I need this but .Net 2003 compiler complaining without it
#include <iostream>
using namespace std;
const int _M1ROWS = 2;
const int _M1COLS = 3;
const int _M2ROWS = 3;
const int _M2COLS = 2;
int main()
{
//load the first Matrix
int Matrix1[_M1ROWS][_M1COLS] = {1,2,3,
4,5,6};
//load the second matrix
int Matrix2[_M2ROWS][_M2COLS] = {1,2,
3,4,
5,6};
//Declare the resultant Matrix
//remember the result matris is always the
//number of rows in the first X the number of cols in the second
int rMatrix [_M1ROWS][_M2COLS];
//declare our loop controls
int rowM1, rowColIndx, colM2;
//loop through all the rows in matrix 1
for(rowM1=0;rowM1<_M1ROWS;rowM1++)
{
//loop through all the cols in matrix 2
for(colM2=0;colM2<_M2COLS;colM2++)
{
//loop through all items in the row to col match and multiply
int calResult = 0;
for(rowColIndx=0;rowColIndx<_M1COLS;rowColIndx++)
{
calResult = calResult +(Matrix1[rowM1][rowColIndx] * Matrix2[rowColIndx][colM2]);
}
rMatrix[rowM1][colM2]=calResult;
}
}
//I am just printing the result matrix here...
cout<<rMatrix[0][0]<<",";
cout<<rMatrix[0][1]<<endl;
cout<<rMatrix[1][0]<<",";
cout<<rMatrix[1][1];
int x;
cin>>x; // this does nothing just pauses to see results
}
C Code
Code:
#include <iostream.h>
const int _M1ROWS = 2;
const int _M1COLS = 3;
const int _M2ROWS = 3;
const int _M2COLS = 2;
int main()
{
//load the first Matrix
int Matrix1[_M1ROWS][_M1COLS] = {1,2,3,
4,5,6};
//load the second matrix
int Matrix2[_M2ROWS][_M2COLS] = {1,2,
3,4,
5,6};
//Declare the resultant Matrix
//remember the result matris is always the
//number of rows in the first X the number of cols in the second
int rMatrix [_M1ROWS][_M2COLS];
//declare our loop controls
int rowM1, rowColIndx, colM2;
//loop through all the rows in matrix 1
for(rowM1=0;rowM1<_M1ROWS;rowM1++)
{
//loop through all the cols in matrix 2
for(colM2=0;colM2<_M2COLS;colM2++)
{
//loop through all items in the row to col match and multiply
int calResult = 0;
for(rowColIndx=0;rowColIndx<_M1COLS;rowColIndx++)
{
calResult = calResult +(Matrix1[rowM1][rowColIndx] * Matrix2[rowColIndx][colM2]);
}
rMatrix[rowM1][colM2]=calResult;
}
}
//I am just printing the result matrix here...
printf("%d,",rMatrix[0][0]);
printf("%d\n",rMatrix[0][1]);
printf("%d,",rMatrix[1][0]);
printf("%d",rMatrix[1][1]);
int x;
scanf("%d",x);// this does nothing just pauses to see results
}
No difference really
(1)
#include <iostream>
using namespace std;
changes to
#include <iostream.h>
(2)
removed #include <stdafx.h> because it is not a part of C (I think)
(3)
uses printf and scanf instead of cin>> and cout<<