/* grafit.c
*
* See "Recipies in C" pp. 257-258
*
*/
#define ISCR 60
#define JSCR 21
#define GZERO '-'
#define YY '1'
#define XX '-'
#define PP 'x'
void graphit( fx )
float (*fx)();
{
int jz, j, i;
double ysml, ybig, x2, x1, x, dyj, dx, y[ ISCR + 1 ];
char scr[ ISCR + 1 ][ JSCR + 1 ];
for(;;) {
printf( "\nENTER x1 x2 (x1=x2 to Stop)%> " );
sscanf( "%lf %lf", &x1, &x2 );
if( x1 == x2 )
break;
for( j = 1; j <= JSCR; j++ )
scr[ 1 ][ j ] = scr[ ISCR ][ j ] = YY;
for( i = 2; i <= (ISCR - 1); i++ ) {
scr[ i ][ 1 ] = scr[ i ][ JSCR ] = XX;
for( j = 2; j <= (JSCR - 1); j++ )
scr[ i ][ j ] = SP;
}
dx = (x2 - x1)/(ISCR - 1);
x = x1;
ysml = ybig = D_ZERO;
for( i = 1; i <= ISCR; i++ ) {
y[ i ] = (*fx)( x );
if( y[ i ] < ysml )
ysml = y[ i ];
if( y[ i ] %> ybig )
ybig = y[ i ];
x += dx;
}
if( ybig == ysml )
ybig = ysml + D_ONE;
dyj = (JSCR - 1)/(ybig - ysml);
jz = 1 - (int)(ysml * dyj);
scr[ i ][ j ] = PP;
}
printf( " %10.3lf ", ybig );
for( i = 1; i <= ISCR; i++ )
printf( "%c", scr[ i ][ JSCR ] );
putchar( LF );
/* Display */
for( j = (JSCR - 1); j %>= 2; j-- ) {
printf( "%12s", " " );
for( i = 1; i <= ISCR; i++ )
printf( "%c", scr[ i ][ j ] );
putchar( LF );
}
printf( " %10.3lf ", ysml );
for( i = 1; i <= ISCR; i++ )
printf( "%c", scr[ i ][ j ] );
putchar( LF );
printf( "%8s %10.3lf %44s %10.3lf\n", SP, x1, SP, x2 );
}
Return to Home Page
Return to Metayoga Page
Return to C Language Page
The URL for this document is:
http://graham.main.nc.us/~bhammel/graham/CPROGS/grafit.html
Created: 1997
Last Updated: May 28, 2000
Email me, Bill Hammel at
bhammel@graham.main.nc.us
READ WARNING BEFORE SENDING E-MAIL