A vector of vector of double example using Cvec2.h ... (Note: needs to include file Cvec2.h above)
/* test2_Cvec2.h.c */ /* 2012-03-29 */
/* http://developers-heaven.net/forum/index.php/topic,46.0.html */
/* testing a Cvec of Cvec ... (A MATRIX) ... using the new file "Cvec2.h" ... */
#define debug_dwCvec2_H 1 /* change 1 to 0 to turn off debugging here ... */
#include "Cvec2.h"
typedef struct myRec
{
double d;
} Rec ;
void destroyRec( void* el ) /* re. function pointer */
{
/* NO dynamic memory at this level to destruct ... */
#if debug_dwCvec2_H
puts( "destroyRec called ..." );
#endif
}
typedef struct myVecOfRec
{
Cvec cv ;
} CvecOfRec ;
void destroyCvecOfRec( void* el ) /* re. function pointer */
{
Cvec* cv = (Cvec*) el;
#if debug_dwCvec2_H
printf( "\ndestroyCvecOfRec called to destroy Cvec* cv = %p\n", cv );
#endif
clearCvec( cv, destroyRec );
}
int getCharReply( const char* msg );
int getValidInt( const char* prompt );
double getValidDbl( const char* prompt );
void inputCvecNumElem(Cvec *cv, int num );
void showRec( const void* el );
void showCvec( const Cvec* cv );
void showCvecOfCvec( Cvec* cv );
void testCvec();
void inputMatrix( Cvec* cvcv, int nrows, int ncols );
void getSum( Cvec* cvcv, Cvec* cvcv2, Cvec* cvcv3 );
void testMatrix();
double getValAt( const Cvec* cv, int i );
void setValAt( Cvec* cv, int i, double val );
Cvec* getCvecAt( Cvec* cv, int i );
int main() /* ****************** BEGIN MAIN ********************************* */
{
testCvec();
putchar( '\n' );
testMatrix();
fputs( "\nPress 'Enter' to continue/exit ... ", stdout); fflush( stdout );
getchar(); /* keep 'Window' open until 'Enter' key is pressed ... */
return 0;
} /* **************************** END MAIN ********************************** */
int getCharReply( const char* msg )
{
int reply;
fputs( msg, stdout ); fflush( stdout );
reply = getchar();
if( reply != '\n' ) while( getchar() != '\n' ) ; /* 'flush' stdin */
return toupper( reply );
}
int getValidInt( const char* prompt )
{
for( ; ; ) /* an example of a C/C++ forever loop ... until 'return' */
{
int numGood, testInt;
fputs( prompt, stdout ); fflush( stdout );
numGood = fscanf( stdin, "%d", &testInt );
while( getchar() != '\n' ); /* 'flush' stdin ... as we go ... */
if( numGood == 1 ) return testInt;
/*else ...*/
puts( "Invalid input! Integers only please ..." );
}
}
double getValidDbl( const char* prompt )
{
int numGood;
for( ; ; ) /* an example of a C/C++ forever loop ... until 'return' */
{
double test;
fputs( prompt, stdout ); fflush( stdout );
numGood = fscanf( stdin, "%lf", &test );
while( getchar() != '\n' ); /* 'flush' stdin ... as we go ... */
if( numGood == 1 ) return test;
/*else ...*/
puts( "Invalid input! Integers only please ..." );
}
}
void inputCvecNumElem( Cvec* cv, int num )
{
int i;
reserveCvec( cv, num, destroyRec );
for( i = 0; i < num; ++ i )
{
Rec r; /*1234567890123456789*/
char prompt[32] = "Enter Cvec element ";
sprintf( prompt+19, "%02d: ", i+1 );
r.d = getValidDbl( prompt );
push_backCvec( cv, &r, destroyRec );
}
}
void showRec( const void* el )
{
const Rec* rc = (const Rec*) el;
printf( "%f", rc->d );
}
void showCvec( const Cvec* cv )
{
int i;
for( i = 0; i < cv->size; ++ i )
{
showRec( cv->ary + i*cv->elen ); putchar( ' ' );
}
}
void showCvecOfCvec( Cvec* cv )
{
int i;
for( i = 0; i < cv->size; ++i )
{
printf( "Row %2d: ", i+1 );
showCvec( cv->ary + i*cv->elen );
putchar( '\n' );
}
}
void testCvec()
{
Cvec cv;
initCvec( &cv, sizeof(Rec) );
do
{
int i, num = getValidInt( "Enter number of elements for this vector: " );
inputCvecNumElem( &cv, num );
showCvec( &cv ); putchar( '\n' );
for( i = 0; i < cv.size; ++i )
{
printf( "v(%d) = %f ", i+1, getValAt( &cv, i ) );
}
putchar( '\n' );
clearCvec( &cv, destroyRec );
}
while( getCharReply( "More (y/n) ? " ) != 'N' );
}
void inputMatrix( Cvec* cvcv, int nrows, int ncols )
{
int i;
reserveCvec( cvcv, nrows, destroyCvecOfRec );
for( i = 0; i < nrows; ++i )
{
Cvec cv;
initCvec( &cv, sizeof(Rec) );
reserveCvec( &cv, ncols, destroyRec );
printf( "For row %d:\n", i+1 );
inputCvecNumElem( &cv, ncols );
push_backCvec( cvcv, &cv, destroyCvecOfRec );
}
}
void initialMatrix( Cvec* cvcv, int nrows, int ncols )
{
int i, j;
reserveCvec( cvcv, nrows, destroyCvecOfRec );
for( i = 0; i < nrows; ++i )
{
Cvec cv;
Rec r;
r.d = 0.0;
initCvec( &cv, sizeof(Rec) );
reserveCvec( &cv, ncols, destroyRec );
for( j = 0; j < ncols; ++j ) push_backCvec( &cv, &r, destroyRec );
push_backCvec( cvcv, &cv, destroyCvecOfRec );
}
}
double getValAt( const Cvec* cv, int i )
{
return *(double*)(cv->ary + i*cv->elen);
}
void setValAt( Cvec* cv, int i, double val )
{
*(double*)(cv->ary + i*cv->elen) = val;
}
Cvec* getCvecAt( Cvec* cv, int i )
{
return cv->ary + i*cv->elen ;
}
void getSum( Cvec* cvcv, Cvec* cvcv2, Cvec* cvcv3 )
{
int i, j;
for( i = 0; i < cvcv->size; ++i )
{
Cvec* inner = getCvecAt( cvcv, i );
Cvec* inner2 = getCvecAt( cvcv2, i );
Cvec* inner3 = getCvecAt( cvcv3, i );
for( j = 0; j < inner->size; ++ j )
{
setValAt( inner3, j, getValAt(inner, j) + getValAt(inner2, j) );
}
}
}
void testMatrix()
{
int nrows, ncols;
Cvec cvcv, cvcv2, cvcv3;
initCvec( &cvcv, sizeof(CvecOfRec) );
initCvec( &cvcv2, sizeof(CvecOfRec) );
initCvec( &cvcv3, sizeof(CvecOfRec) );
nrows = getValidInt( "Enter number of rows in matrix: " );
ncols = getValidInt( "Enter number of cols in matrix: " );
puts( "\nGet matrix 1 ..." );
inputMatrix( &cvcv, nrows, ncols );
puts( "\nGet matrix 2 ..." );
inputMatrix( &cvcv2, nrows, ncols );
puts( "\nMatrix 1:" );
showCvecOfCvec( &cvcv );
puts( " + Matrix 2:" );
showCvecOfCvec( &cvcv2 );
initialMatrix(&cvcv3, nrows, ncols );
getSum( &cvcv, &cvcv2, &cvcv3 );
puts( "= Matrix 3:" );
showCvecOfCvec( &cvcv3 );
getCharReply( "Press 'Enter' to continue ..." );
clearCvec( &cvcv3, destroyCvecOfRec );
clearCvec( &cvcv2, destroyCvecOfRec );
clearCvec( &cvcv, destroyCvecOfRec );
}
A list of list of double example ... (Note: need to include file Clist2.h above ... and ... note how we create the new memory for each node and record in that node ... 'as we go' ... in the program.)
/* test2_Clist2.h.c */ /* 2012-03-29 */
/* http://developers-heaven.net/forum/index.php/topic,46.0.html */
/* testing a Clist of Clist... (A MATRIX) ... using the new file "Clist2.h" ... */
#define debug_dwClist2_H 1 /* change 1 to 0 to turn off debugging here ... */
#include "Clist2.h"
typedef struct myRec
{
double d;
} Rec ;
void destroyRec( void* el ) /* re. function pointer */
{
/* NO dynamic memory at this level to destruct ... */
#if debug_dwClist2_H
puts( "destroyRec called ..." );
#endif
}
typedef struct myListOfRec
{
Clist cl ;
} ClistOfRec ;
void destroyClistOfRec( void* el ) /* re. function pointer */
{
Clist* cl = (Clist*) el;
#if debug_dwClist2_H
printf( "\ndestroyClistOfRec called to destroy Clist* cl = %p\n", cl );
#endif
clearClist( cl, destroyRec );
}
int getCharReply( const char* msg );
int getValidInt( const char* prompt );
double getValidDbl( const char* prompt );
void inputClistNumElem(Clist *cl, int num );
void showRec( const void* el );
void showClist( const Clist* cl );
void showClistOfClist( Clist* cl );
void testClist();
void initialMatrix( Clist* clcl, int nrows, int ncols );
void inputMatrix( Clist* clcl, int nrows, int ncols );
void getSum( Clist* clcl, Clist* clcl2, Clist* clcl3 );
void testMatrix();
double getValAt( const Clist* cl, int i );
void setValAt( Clist* cl, int i, double val );
Clist* getClistAt( Clist* cl, int i );
int main() /* ****************** BEGIN MAIN ********************************* */
{
testClist();
putchar( '\n' );
testMatrix();
fputs( "\nPress 'Enter' to continue/exit ... ", stdout); fflush( stdout );
getchar(); /* keep 'Window' open until 'Enter' key is pressed ... */
return 0;
} /* **************************** END MAIN ********************************** */
int getCharReply( const char* msg )
{
int reply;
fputs( msg, stdout ); fflush( stdout );
reply = getchar();
if( reply != '\n' ) while( getchar() != '\n' ) ; /* 'flush' stdin */
return toupper( reply );
}
int getValidInt( const char* prompt )
{
for( ; ; ) /* an example of a C/C++ forever loop ... until 'return' */
{
int numGood, testInt;
fputs( prompt, stdout ); fflush( stdout );
numGood = fscanf( stdin, "%d", &testInt );
while( getchar() != '\n' ); /* 'flush' stdin ... as we go ... */
if( numGood == 1 ) return testInt;
/*else ...*/
puts( "Invalid input! Integers only please ..." );
}
}
double getValidDbl( const char* prompt )
{
int numGood;
for( ; ; ) /* an example of a C/C++ forever loop ... until 'return' */
{
double test;
fputs( prompt, stdout ); fflush( stdout );
numGood = fscanf( stdin, "%lf", &test );
while( getchar() != '\n' ); /* 'flush' stdin ... as we go ... */
if( numGood == 1 ) return test;
/*else ...*/
puts( "Invalid input! Integers only please ..." );
}
}
void inputClistNumElem( Clist* cl, int num )
{
int i;
for( i = 0; i < num; ++ i )
{
Rec* tmp = (Rec*)newMem( sizeof(Rec) );
Node* p = (Node*)newMem( sizeof(Node) );
/* 12345678901234567890 */
char prompt[32] = "Enter Clist element ";
sprintf( prompt+20, "%02d: ", i+1 );
tmp->d = getValidDbl( prompt );
p->data = tmp;
push_backClist( cl, p );
}
}
void showRec( const void* el )
{
const Rec* rc = (const Rec*) el;
printf( "%f", rc->d );
}
void showClist( const Clist* cl )
{
Node* i;
for( i = cl->start; i != NULL; i = i->next )
{
showRec( i->data ); putchar( ' ' );
}
}
void showClistOfClist( Clist* cl )
{
Node* i;
int j = 0;
for( i = cl->start; i != NULL; i = i->next )
{
printf( "Row %2d: ", ++j );
showClist( i->data );
putchar( '\n' );
}
}
void testClist()
{
Clist cl;
initClist( &cl );
do
{
int i, num = getValidInt( "Enter number of elements for this list: " );
inputClistNumElem( &cl, num );
showClist( &cl ); putchar( '\n' );
for( i = 0; i < cl.size; ++i )
{
printf( "v(%d) = %f ", i+1, getValAt( &cl, i ) );
}
putchar( '\n' );
clearClist( &cl, destroyRec );
}
while( getCharReply( "More (y/n) ? " ) != 'N' );
}
void inputMatrix( Clist* clcl, int nrows, int ncols )
{
int i;
for( i = 0; i < nrows; ++i )
{
Clist* cl = (Clist*)newMem( sizeof(Clist));
Node* p = (Node*)newMem( sizeof(Node) );
initClist( cl );
printf( "For row %d:\n", i+1 );
inputClistNumElem( cl, ncols );
p->data = cl;
push_backClist( clcl, p );
}
}
void initialMatrix( Clist* clcl, int nrows, int ncols )
{
int i, j;
for( i = 0; i < nrows; ++i )
{
Clist* cl = (Clist*)newMem( sizeof(Clist));
Node* pi = (Node*)newMem( sizeof(Node) );
initClist( cl );
for( j = 0; j < ncols; ++j )
{
Rec* rc = (Rec*)newMem( sizeof(Rec));
Node* pj = (Node*)newMem( sizeof(Node) );
rc->d = 0.0;
pj->data = rc;
push_backClist( cl, pj );
}
pi->data = cl;
push_backClist( clcl, pi );
}
}
double getValAt( const Clist* cl, int i )
{
if( i >= 0 && i < cl->size )
{
int j;
Node* cur;
for( j = 0, cur = cl->start; cur != NULL; ++j, cur = cur->next )
if( j == i ) return ((Rec*)(cur->data))->d;
}
/* else */
printf( "Index %d out of valid range 0 to %d, returning 0.0", i, cl->size );
return 0.0;
}
void setValAt( Clist* cl, int i, double val )
{
if( i >= 0 && i < cl->size )
{
int j;
Node* cur;
for( j = 0, cur = cl->start; cur != NULL; ++j, cur = cur->next )
{
if( j == i ) { ((Rec*)(cur->data))->d = val; return; }
}
}
/* else */
printf( "Index %d out of valid range 0 to %d, returning 0.0", i, cl->size );
}
Clist* getClistAt( Clist* cl, int i )
{
if( i < cl->size )
{
int j;
Node* cur;
for( j = 0, cur = cl->start; cur != NULL; ++j, cur = cur->next )
{
if( j == i ) return (Clist*)(cur->data);
}
}
/* else */
printf( "Index %d out of valid range 0 to %d, returning NULL", i, cl->size );
return NULL ;
}
void getSum( Clist* clcl, Clist* clcl2, Clist* clcl3 )
{
int i, j;
for( i = 0; i < clcl->size; ++i )
{
Clist* inner = getClistAt( clcl, i );
Clist* inner2 = getClistAt( clcl2, i );
Clist* inner3 = getClistAt( clcl3, i );
for( j = 0; j < inner->size; ++ j )
{
setValAt( inner3, j, getValAt(inner, j) + getValAt(inner2, j) );
}
}
}
void testMatrix()
{
int nrows, ncols;
Clist clcl, clcl2, clcl3;
initClist( &clcl );
initClist( &clcl2 );
initClist( &clcl3 );
nrows = getValidInt( "Enter number of rows in matrix: " );
ncols = getValidInt( "Enter number of cols in matrix: " );
puts( "\nGet matrix 1 ..." );
inputMatrix( &clcl, nrows, ncols );
puts( "\nGet matrix 2 ..." );
inputMatrix( &clcl2, nrows, ncols );
puts( "\nMatrix 1:" );
showClistOfClist( &clcl );
puts( " + Matrix 2:" );
showClistOfClist( &clcl2 );
initialMatrix(&clcl3, nrows, ncols );
getSum( &clcl, &clcl2, &clcl3 );
puts( "= Matrix 3:" );
showClistOfClist( &clcl3 );
getCharReply( "Press 'Enter' to continue ..." );
clearClist( &clcl3, destroyClistOfRec );
clearClist( &clcl2, destroyClistOfRec );
clearClist( &clcl, destroyClistOfRec );
}