A vector of vector of double example using Cvec2.h ... (Note: needs to include file Cvec2.h above)
/* test2_Cvec2.h.c */ /* 2015-06-16 */
/* 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* at %p\n", (void*)cv );
#endif
clearCvec( cv, destroyRec );
}
int takeInChr( const char* msg );
int takeInInt( const char* prompt );
double takeInDbl( const char* prompt );
void takeInCvecNumElem(Cvec *cv, int num );
void showRec( const Rec* );
void showCvec( const Cvec* );
void showCvecOfCvec( const Cvec* );
void testCvec();
void takeInMatrix( 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*, int );
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 takeInChr( const char* msg )
{
int chr;
fputs( msg, stdout ); fflush( stdout );
chr = getchar();
if( chr != '\n' ) while( getchar() != '\n' ) ; /* 'flush' stdin */
return chr;
}
int takeInInt( const char* prompt )
{
for( ; ; ) /* an example of a C/C++ forever loop ... until 'return' */
{
int val;
fputs( prompt, stdout ); fflush( stdout );
if( fscanf( stdin, "%d", &val ) == 1 && getchar() == '\n' )
return val;
/* else ... if reach here ... */
while( getchar() != '\n' ); /* 'flush' stdin ... as we go ... */
puts( "Invalid input! Integers only please ..." );
}
}
double takeInDbl( const char* prompt )
{
for( ; ; ) /* an example of a C/C++ forever loop ... until 'return' */
{
double val;
fputs( prompt, stdout ); fflush( stdout );
if( fscanf( stdin, "%lf", &val ) == 1 && getchar() == '\n' )
return val;
/* else ... if reach here ... */
while( getchar() != '\n' ); /* 'flush' stdin ... as we go ... */
puts( "Invalid input! Numbers only please ..." );
}
}
void takeInCvecNumElem( Cvec* cv, int num )
{
int i;
Rec rc; /*1234567890123456789*/
char prompt[32] = "Enter Cvec element ";
reserveCvec( cv, num );
for( i = 0; i < num; ++ i )
{
sprintf( prompt+19, "%02d: ", i+1 );
rc.d = takeInDbl( prompt );
push_backCvec( cv, &rc );
}
}
void showRec( const Rec* rc )
{
printf( "%f", rc->d );
}
void showCvec( const Cvec* cv )
{
int i;
for( i = 0; i < cv->size; ++ i )
{
showRec( (const Rec*)cv->ary + i );
putchar( ' ' );
}
}
void showCvecOfCvec( const Cvec* cv )
{
int i;
for( i = 0; i < cv->size; ++i )
{
printf( "Row %2d: ", i+1 );
showCvec( (const Cvec*)cv->ary + i );
putchar( '\n' );
}
}
void testCvec()
{
Cvec cv;
initCvec( &cv, sizeof(Rec) );
do
{
int i,
num = takeInInt( "Enter number of elements for this vector: " );
takeInCvecNumElem( &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( tolower(takeInChr( "More (y/n) ? " ))!= 'n' );
}
void takeInMatrix( Cvec* cvcv, int nrows, int ncols )
{
int i;
reserveCvec( cvcv, nrows );
for( i = 0; i < nrows; ++i )
{
Cvec cv;
initCvec( &cv, sizeof(Rec) );
reserveCvec( &cv, ncols );
printf( "For row %d:\n", i+1 );
takeInCvecNumElem( &cv, ncols );
push_backCvec( cvcv, &cv );
}
}
void initialMatrix( Cvec* cvcv, int nrows, int ncols )
{
int i, j;
reserveCvec( cvcv, nrows );
for( i = 0; i < nrows; ++i )
{
Cvec cv;
Rec r;
r.d = 0.0;
initCvec( &cv, sizeof(Rec) );
reserveCvec( &cv, ncols );
for( j = 0; j < ncols; ++j ) push_backCvec( &cv, &r );
push_backCvec( cvcv, &cv );
}
}
double getValAt( const Cvec* cv, int i )
{
return *((double*) ((const char*)cv->ary + i*cv->elen));
}
void setValAt( Cvec* cv, int i, double val )
{
*(double*)((char*)cv->ary + i*cv->elen) = val;
}
Cvec* getCvecAt( Cvec* cv, int i )
{
return (Cvec*)cv->ary + i ;
}
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 = takeInInt( "Enter number of rows in matrix: " );
ncols = takeInInt( "Enter number of cols in matrix: " );
puts( "\nGet matrix 1 ..." );
takeInMatrix( &cvcv, nrows, ncols );
puts( "\nGet matrix 2 ..." );
takeInMatrix( &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 );
takeInChr( "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 */ /* 2015-06-16 */
/* 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* at %p\n", (void*)cl );
#endif
clearClist( cl, destroyRec );
}
int takeInChr( const char* msg );
int takeInInt( const char* prompt );
double takeInDbl( const char* prompt );
void takeInClistNumElem( Clist *cl, int num );
void showRec( const void* el );
void showClist( const Clist* cl );
void showClistOfClist( const Clist* cl );
void testClist();
void initialMatrix( Clist* clcl, int nrows, int ncols );
void takeInMatrix( 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( const 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 takeInChr( const char* msg )
{
int reply;
fputs( msg, stdout ); fflush( stdout );
reply = getchar();
if( reply != '\n' ) while( getchar() != '\n' ) ; /* 'flush' stdin */
return toupper( reply );
}
int takeInInt( const char* prompt )
{
for( ; ; ) /* an example of a C/C++ forever loop ... until 'return' */
{
int val;
fputs( prompt, stdout ); fflush( stdout );
if( fscanf( stdin, "%d", &val ) == 1 && getchar() == '\n' )
return val;
/* else ... if rech here ... */
while( getchar() != '\n' ); /* 'flush' stdin ... as we go ... */
puts( "Invalid input! Integers only please ..." );
}
}
double takeInDbl( const char* prompt )
{
for( ; ; ) /* an example of a C/C++ forever loop ... until 'return' */
{
double val;
fputs( prompt, stdout ); fflush( stdout );
if( fscanf( stdin, "%lf", &val ) == 1 && getchar() == '\n' )
return val;
/*else ...*/
while( getchar() != '\n' ); /* 'flush' stdin ... as we go ... */
puts( "Invalid input! Numbers only please ..." );
}
}
void takeInClistNumElem( Clist* cl, int num )
{
int i;
for( i = 0; i < num; ++ i )
{
Rec* tmp = (Rec*)newMem( sizeof(Rec) );
pNode p = (pNode)newMem( sizeof(Node) );
/* 12345678901234567890 */
char prompt[32] = "Enter Clist element ";
sprintf( prompt+20, "%02d: ", i+1 );
tmp->d = takeInDbl( 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 )
{
pNode i;
for( i = cl->start; i != NULL; i = i->next )
{
showRec( i->data ); putchar( ' ' );
}
}
void showClistOfClist( const Clist* cl )
{
pNode 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 = takeInInt( "Enter number of elements for this list: " );
takeInClistNumElem( &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( takeInChr( "More (y/n) ? " ) != 'N' );
}
void takeInMatrix( Clist* clcl, int nrows, int ncols )
{
int i;
for( i = 0; i < nrows; ++i )
{
Clist* cl = (Clist*)newMem( sizeof(Clist));
pNode p = (pNode)newMem( sizeof(Node) );
initClist( cl );
printf( "For row %d:\n", i+1 );
takeInClistNumElem( 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));
pNode pi = (pNode)newMem( sizeof(Node) );
initClist( cl );
for( j = 0; j < ncols; ++j )
{
Rec* rc = (Rec*)newMem( sizeof(Rec));
pNode pj = (pNode)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;
pNode 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;
pNode 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( const Clist* cl, int i )
{
if( i < cl->size )
{
int j;
pNode 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 = takeInInt( "Enter number of rows in matrix: " );
ncols = takeInInt( "Enter number of cols in matrix: " );
puts( "\nGet matrix 1 ..." );
takeInMatrix( &clcl, nrows, ncols );
puts( "\nGet matrix 2 ..." );
takeInMatrix( &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 );
takeInChr( "Press 'Enter' to continue ..." );
clearClist( &clcl3, destroyClistOfRec );
clearClist( &clcl2, destroyClistOfRec );
clearClist( &clcl, destroyClistOfRec );
}