/** * TABLEOFPOWERS.C * * Exercise solution 2.4. (C By Dissection Chap. 4, ex. 2) * * Computes a table of powers. * 25-Feb-03 by Dimon **/ /** * INCLUDE statements **/ #include #include /** * DEFINE statements **/ #define NUMROWS 25 /** * Function declarations **/ int square(int x); int cube(int x); int quartic(int x); int quintic(int x); /** * The main function **/ int main(void) { /* Declaration of variables */ int i = 1; int status = 0; /* Display table header */ printf("A Table of Powers\n"); printf("-----------------\n"); printf("\n"); printf("Integer Square Cube Quartic Quintic\n"); printf("------- ------ ---- ------- -------\n"); /* A FOR loop calculates and prints out the results in the table */ for(i = 1; i < NUMROWS; i++) { printf("%7d ",i); printf("%7d ",square(i)); printf("%7d ",cube(i)); printf("%7d ",quartic(i)); printf("%7d ",quintic(i)); printf("\n"); } /* End of FOR loop */ /* Terminate program */ return status; } /* END OF MAIN */ /** * Function definitions **/ /** * Returns the square (x^2) of the argument. **/ int square(int x) { return x*x; } /** * Returns the cube (x^3) of the argument. **/ int cube(int x) { return square(x)*x; } /** * Returns the quartic (x^4) of the argument. **/ int quartic(int x) { return square(square(x)); } /** * Returns the quintic (x^5) of the argument. **/ int quintic(int x) { return square(x)*cube(x); }