#include #include #include void testAll(); // a "forward declaration", so we can call it in main, before we define it. // Return x*y. // int multiply (int x, int y) { return x*y; } // Return a^b. // int power(int a, unsigned int b) { int product = 1; while (b!=0) { product = multiply(product, a); b--; } return product; } int main( int argc, char** argv ) { testAll(); printf("This program verifies whether 5 to the 300th power is bigger than 0.\n"); const unsigned int x = 5; const unsigned int y = 300; char report[5] = "Yes!"; bool overflow; if (power(x,y) >= 0) { printf("%s\n",report); overflow = false; } else { printf( "It's not! Hmmm; overflow?\n" ); overflow = true; } return overflow; // indicate an error, to the shell / caller. } void testMultiply() { printf("testMultiply: "); assert( multiply(0,0) == 0 ); assert( multiply(0,3) == 0 ); assert( multiply(3,0) == 0 ); assert( multiply(1,1) == 1 ); assert( multiply(1,3) == 3 ); assert( multiply(3,1) == 3 ); assert( multiply(3,2) == 6 ); assert( multiply(3,-2) == -6 ); assert( multiply(-3,2) == -6 ); assert( multiply(-3,-2) == 6 ); assert( multiply(200,75) == 15000 ); printf("done\n"); } void testPower() { printf("testPower: "); assert( power(0,0) == 1 ); assert( power(0,1) == 0 ); assert( power(1,0) == 1 ); assert( power(0,3) == 0 ); assert( power(3,0) == 1 ); assert( power(1,1) == 1 ); assert( power(1,3) == 1 ); assert( power(3,1) == 3 ); assert( power( 3,2) == 9 ); assert( power( 3,3) == 27 ); assert( power(-3,2) == 9 ); assert( power(-3,3) == -27 ); assert( power(2,10) == 1024 ); assert( power(10,9) == 1000000000 ); printf("done\n"); } void testAll() { testMultiply(); testPower(); } /** * @author ibarland * @version 2025-Jan-20 * * @license CC-BY -- share/adapt this file freely, but include attribution, thx. * https://creativecommons.org/licenses/by/4.0/ * https://creativecommons.org/licenses/by/4.0/legalcode * Including a link to the *original* file satisifies "appropriate attribution". */