//------------------------------------------------------------------- // sqroots.cpp // // This program calculates and displays a mathematical table of // square-roots to four-place accuracy of the positive integers // up to one hundred. It calls a subroutine named 'int2root()' // that accepts a positive integer and the address of a buffer, // returning the formatted table-entry in that supplied buffer. // // compile using: g++ sqroots.cpp -o sqroots // execute using: ./sqroots // // programmer: ALLAN CRUSE // written on: 06 APR 2008 //------------------------------------------------------------------- #include // for printf() #include // for sqrt() char root[ 20 ] = {0}; void int2root( unsigned int x, char *buf ) { if ( x > 100 ) return; else sprintf( buf, " %3d %.4f ", x, sqrt( x ) ); } int main( int argc, char **argv ) { printf( "\n%50s\n", "TABLE OF SQUARE ROOTS" ); for (int i = 1; i <= 20; i++) { printf( "\n" ); for (int j = 0; j < 100; j+=20) { int2root( i+j, root ); printf( "%s", root ); } } printf( "\n\n" ); }