#include %26lt;iostream%26gt;
using namespace std;
void matrix_Multi(const int*matA,const int*matB,const int*matC,int m,int n,int l);
void printArray2(double*,int %26amp;nRow,int %26amp;ncol);
void readArray2( double *, int %26amp;nRow, int %26amp;nCol );
int main()
{
double m,n,l,i,j;
char x;
double *ArrayA,*ArrayB,*ArrayC;
char choice;
do{
std::cout %26lt;%26lt; "Matrix multiplication (A*B):" %26lt;%26lt; std::endl;
std::cout %26lt;%26lt; "Enter the dimension of matrix" %26lt;%26lt; std::endl;
std::cout %26lt;%26lt; "A:";
cin%26gt;%26gt;m;
cout%26lt;%26lt;m;
cin%26gt;%26gt;x;
cout%26lt;%26lt;x;
cin%26gt;%26gt;n;
cout%26lt;%26lt;n%26lt;%26lt; std::endl;
ArrayA=new double*m*n;
readArray2(ArrayA,m,n);
std::cout %26lt;%26lt; "Enter the dimension of matrix" %26lt;%26lt; std::endl;
std::cout %26lt;%26lt; "B:"%26lt;%26lt;n%26lt;%26lt;"x ";
cin%26gt;%26gt;l;
cout%26lt;%26lt;l;
ArrayB=new double*n*l;
readArray2(ArrayB,n,l);
ArrayC=new double*m*l;
matrix_Multi(ArrayA,ArrayB, ArrayC,m,n,l);
printArray2(ArrayC,m,l);
std::cout%26lt;%26lt;"Process another matrix multiplication or Quit(P/Q):";
cin%26gt;%26gt;choice;
cout%26lt;%26lt;choice;
}while(choice=='p'||choice=='P');
return 0;
}
void readArray2( double *Array, int %26amp;nRow, int %26amp;nCol)
{
int i,j;
cout%26lt;%26lt;"Enter elements of A row by row ("%26lt;%26lt;nCol%26lt;%26lt;" elements per row):"%26lt;%26lt;endl;
for(i=0;i%26lt;nRow;i++){
cout%26lt;%26lt;"Row "%26lt;%26lt;i+1%26lt;%26lt;" "\n";
for(j=0;j%26lt;nCol;j++){
cin%26gt;%26gt;Array[i*nCol+j];
}
while(getchar()!='\n');
}
}
void printArray2(double *Array,int %26amp;nRow,int %26amp;ncol)
{
int i,j;
for(i=0;i%26lt;nRow;i++){
for(j=0;j%26lt;nCol;j++){
cout%26lt;%26lt;Array[i*nCol+j]%26lt;%26lt;"\t";
}
cout%26lt;%26lt;"\n";
}
}
void matrix_Multi(const int *matA, const int *matB, int *matC, int m, int n, int l)
{
int i,cm,cl;
for(cm=0;cm%26lt;m;cm++){
for(cl=0;cl%26lt;l;cl++){
matC[cm*l+cl]=0;
for(i=0;i%26lt;n;i++){
matC[cm*l+cl]+=matA[cm*n+i]*matB[i*l+cl]...
}
}
}
}
CAN ANYBODY CHECK MY C++ PROGRAM WHAT's wrong with it?
I don't know how you got your sample run because the code you posted have a lot of compilations errors.
I fixed them, but since some of the line of your code got cutted by YA I can't really check the result.
Anyway here it is :
#include "stdafx.h"
#include %26lt;iostream%26gt;
using namespace std;
void readArray2( double* Array, int%26amp; nRow, int%26amp; nCol);
void matrix_Multi(const double *matA, const double *matB,
double *matC, int m, int n, int l);
void printArray2(double *Array,int %26amp;nRow,int %26amp;ncol);
int main()
{
int a1,a2;
int b2;
char x;
double *ArrayA,*ArrayB,*ArrayC;
char choice;
do
{
std::cout %26lt;%26lt; "Matrix multiplication (A*B):" %26lt;%26lt; std::endl;
std::cout %26lt;%26lt; "Enter the dimension of matrix" %26lt;%26lt; std::endl;
std::cout %26lt;%26lt; "A:";
cin%26gt;%26gt;a1;
cout%26lt;%26lt;a1;
cin%26gt;%26gt;x;
cout%26lt;%26lt;x;
cin%26gt;%26gt;a2;
cout %26lt;%26lt;a2 %26lt;%26lt; std::endl;
ArrayA =new double[a1*a2];
readArray2(ArrayA,a1,a2);
std::cout %26lt;%26lt; "Enter the dimension of matrix" %26lt;%26lt; std::endl;
std::cout %26lt;%26lt; "B:"%26lt;%26lt; a1 %26lt;%26lt;"x ";
cin%26gt;%26gt; b2;
cout%26lt;%26lt; b2;
ArrayB=new double[a2*b2];
readArray2(ArrayB,a2,b2);
ArrayC=new double[a1*b2];
matrix_Multi(ArrayA,ArrayB, ArrayC,a1,a2,b2);
printArray2(ArrayC,a2,b2);
std::cout%26lt;%26lt;"Process another matrix multiplication or Quit(P/Q):";
cin%26gt;%26gt;choice;
cout%26lt;%26lt;choice;
} while (choice=='p'|| choice=='P');
return 0;
}
void readArray2( double *Array, int %26amp;nRow, int %26amp;nCol)
{
int i,j;
cout%26lt;%26lt;"Enter elements of A row by row ("
%26lt;%26lt;nCol%26lt;%26lt;" elements per row):"%26lt;%26lt;endl;
for(i=0;i%26lt;nRow;i++)
{
cout %26lt;%26lt; "Row " %26lt;%26lt; i+1 %26lt;%26lt; "\n";
for(j=0;j%26lt;nCol;j++)
{
cin%26gt;%26gt;Array[i*nCol+j];
}
while(getchar()!='\n');
}
}
void printArray2(double *Array,int %26amp;nRow,int %26amp;ncol)
{
int i,j;
for(i=0;i%26lt;nRow;i++)
{
for(j=0;j%26lt;ncol;j++)
{
cout%26lt;%26lt;Array[i*ncol+j]%26lt;%26lt;"\t";
}
cout%26lt;%26lt;"\n";
}
}
void matrix_Multi(const double *matA, const double *matB,
double *matC, int m, int n, int l)
{
int i,cm,cl;
for(cm=0;cm%26lt;m;cm++)
{
for(cl=0;cl%26lt;l;cl++)
{
matC[cm*l+cl]=0;
for(i=0;i%26lt;n;i++)
{
matC[cm*l+cl] += matA[cm*n+i] ; // end of line missing
}
}
}
}
Reply:what is it doing or showing that tells you that something is wrong? are you getting a compiling error, runtime, or just an incorrect answer?
artificial flowers
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment