Click here to Skip to main content
15,919,132 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: where, in its construction sequence, is the best place to init a combobox list? Pin
Jesse Evans17-Feb-03 6:37
Jesse Evans17-Feb-03 6:37 
QuestionVC7 deployment files? Pin
Jason Henderson13-Feb-03 11:15
Jason Henderson13-Feb-03 11:15 
AnswerRe: VC7 deployment files? Pin
Joaquín M López Muñoz13-Feb-03 11:30
Joaquín M López Muñoz13-Feb-03 11:30 
GeneralRe: VC7 deployment files? Pin
Jason Henderson13-Feb-03 17:08
Jason Henderson13-Feb-03 17:08 
GeneralHelp saving large formatted text to database Pin
ElizabethC13-Feb-03 11:09
ElizabethC13-Feb-03 11:09 
GeneralRe: Help saving large formatted text to database Pin
David Salter13-Feb-03 11:54
David Salter13-Feb-03 11:54 
GeneralRe: Help saving large formatted text to database Pin
Anonymous13-Feb-03 12:21
Anonymous13-Feb-03 12:21 
GeneralHELP! C++ Assignement Operator Problem Pin
Anton A. Loukine13-Feb-03 11:05
Anton A. Loukine13-Feb-03 11:05 
I am trying to implement a class for matrix multiplication:

Here is my header file:

******************************************************8
#include <stdlib.h>

class matrix
{
private:
double **m;
int nRows, nCols;

public:
matrix( int n1 = 0, int n2 = 0 );
~matrix( );

matrix& operator = ( const matrix& m1 );
matrix operator * ( const matrix& m1 );

int GetRows( ) { return nRows; }
int GetCols( ) { return nCols; }

void SetElement( int nRow, int nCol, double fValue );
double GetElement( int nRow, int nCol );
};
*********************************************************8

Here is my implementation file:

*****************************************************
#include "stdafx.h"
#include "matrix.h"


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

matrix::matrix( int n1, int n2 ) {
if( n1 >= 0 && n2 >= 0 ) {
printf("Constructor ...\n");
nRows = n1;
nCols = n2;

if( nRows > 0 ) {
register int nRow;
m = ( double ** ) malloc( nRows * sizeof( double * ) );
for( nRow = 0; nRow < nRows; nRow++ ) {
m[nRow] = ( double * ) malloc( nCols * sizeof( double ) );
}
}
} else {
nRows = 0;
nCols = 0;
}
}


matrix::~matrix( ) {
if( nRows > 0 ) {
while( --nRows > -1 ) {
free( m[nRows] );
}
free( m );
}
}


matrix& matrix::operator = ( const matrix& m1 ) {

printf("Assignment operator ...\n");

// If the dimensions of the matrix are not the same, redefine the matrix
if( nRows != m1.nRows || nCols != m1.nCols ) {

printf("Reshaping!");
if( nRows > 0 ) {
while( --nRows > -1 ) {
free( m[nRows] );
}
free( m );
}

nRows = m1.nRows;
nCols = m1.nCols;

if( nRows > 0 ) {
register int nRow;
m = ( double ** ) malloc( nRows * sizeof( double * ) );
for( nRow = 0; nRow < nRows; nRow++ ) {
m[nRow] = ( double * ) malloc( nCols * sizeof( double ) );
}
}
}

// Copy the elements of the matrix
register int n1, n2;
for( n1 = 0; n1 < nRows; n1++ ) {
for( n2 = 0; n2 < nCols; n2++ ) {

***********************************
* THIS IS WHERE IT FAILS !!!!
***********************************

m[n1][n2] = m1.m[n1][n2];
}
}

return *this;
}


matrix matrix::operator * ( const matrix& m1 ) {
if( nCols == m1.nRows ) {
double fValue;
register int n1, n2, n3;
matrix mResult( nRows, m1.nCols );

for( n1 = 0; n1 < nRows; n1++ ) {
for ( n2 = 0; n2 < m1.nCols; n2++ ) {
fValue = 0.0;
for ( n3 = 0; n3 < nCols; n3++ ) {
fValue += m[n1][n3] * m1.m[n3][n2];
}
mResult.m[n1][n2] = fValue;
}
}

printf("Finished multiplying ...\n");
return mResult;
} else {
printf("ERROR");
// Multiplication is impossible
}
}


void matrix::SetElement( int nRow, int nCol, double fValue ) {
if( nRow >= 0 && nRow <= nRows && nCol >= 0 && nCol <= nCols) {
m[nRow][nCol] = fValue;
}
}

double matrix::GetElement( int nRow, int nCol ) {
if( nRow >= 0 && nRow <= nRows && nCol >= 0 && nCol <= nCols) {
return m[nRow][nCol];
} else {
return 0.0;
}
}

********************************************

Here is my code:

*************************

matrix m1( 3, 2 );
m1.SetElement (0, 0, 5.0);
m1.SetElement (0, 1, 4.0);
m1.SetElement (1, 0, 3.0);
m1.SetElement (1, 1, 2.0);
m1.SetElement (2, 0, 1.0);
m1.SetElement (2, 1, 0.0);

matrix m2( 2, 2 );
m2.SetElement (0, 0, 2.3);
m2.SetElement (0, 1, 3.7);
m2.SetElement (1, 0, 4.5);
m2.SetElement (1, 1, 6.9);

printf("Before multiplication ...\n");

matrix m3( 3, 2 );
m3 = m1 * m2;

printf("After multiplication ...\n");
printf("Element: %12.10f ", m3.GetElement (0, 0));

***********************

I marked the place where it fails above. I can't figure out why. Please help!!

Many thanks
GeneralRe: HELP! C++ Assignement Operator Problem Pin
Joaquín M López Muñoz13-Feb-03 11:22
Joaquín M López Muñoz13-Feb-03 11:22 
GeneralRe: HELP! C++ Assignement Operator Problem Pin
Anton A. Loukine13-Feb-03 11:33
Anton A. Loukine13-Feb-03 11:33 
GeneralRe: HELP! C++ Assignement Operator Problem Pin
Roger Allen13-Feb-03 22:50
Roger Allen13-Feb-03 22:50 
GeneralRe: HELP! C++ Assignement Operator Problem Pin
João Paulo Figueira13-Feb-03 11:57
professionalJoão Paulo Figueira13-Feb-03 11:57 
GeneralHelp!!..with List container Pin
Sal R.13-Feb-03 10:55
Sal R.13-Feb-03 10:55 
GeneralRe: Help!!..with List container Pin
Christian Graus13-Feb-03 11:15
protectorChristian Graus13-Feb-03 11:15 
GeneralRunning a program at startup ! Pin
Hadi Rezaee13-Feb-03 10:06
Hadi Rezaee13-Feb-03 10:06 
GeneralRe: Running a program at startup ! Pin
Navin13-Feb-03 10:27
Navin13-Feb-03 10:27 
GeneralRe: Running a program at startup ! Pin
Hadi Rezaee13-Feb-03 10:34
Hadi Rezaee13-Feb-03 10:34 
GeneralRe: Running a program at startup ! Pin
Scorp1us13-Feb-03 10:27
Scorp1us13-Feb-03 10:27 
GeneralRe: Running a program at startup ! Pin
Hadi Rezaee13-Feb-03 10:35
Hadi Rezaee13-Feb-03 10:35 
GeneralRe: Running a program at startup ! Pin
Anonymous13-Feb-03 16:55
Anonymous13-Feb-03 16:55 
GeneralRe: Running a program at startup ! Pin
Hadi Rezaee13-Feb-03 19:52
Hadi Rezaee13-Feb-03 19:52 
GeneralRe: Running a program at startup ! Pin
palbano13-Feb-03 10:44
palbano13-Feb-03 10:44 
GeneralRe: Running a program at startup ! Pin
Christian Graus13-Feb-03 10:54
protectorChristian Graus13-Feb-03 10:54 
GeneralRe: Running a program at startup ! Pin
Hadi Rezaee13-Feb-03 11:04
Hadi Rezaee13-Feb-03 11:04 
QuestionCan CBitmapButton load bitmap from disk ? Pin
akulkarni13-Feb-03 8:58
akulkarni13-Feb-03 8:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.