/* This is a very simple program that accepts two arguments and returns the first raised to the second. To compile: gcc -o 02-power 02-power.c -Wall To run: ./02-power C. Andrews 2015-02-17 */ #include #include /* Computes x raised to y using a for loop. */ int power1(int x, int y){ int result = 1; for (int i = 0; i < y; i++){ result = result * x; } return result; } /* Computes x raised to y using a while loop. */ int power2(int x, int y){ int result = 1; while (y > 0){ result = result * x; y--; } return result; } /* Prints out all of the command line arguments, then returns the result of calling the two power functions on the input. */ int main(int argc, char * argv[]){ // iterate over all of the arguments and print them out for (int i =0; i < argc; i++){ // note the substitution characters %d - integer, %s - string printf("%d: %s\n",i, argv[i]); } int x, y; // convert the input strings to integers // atoi is a function found in stdlib x = atoi(argv[1]); y = atoi(argv[2]); printf("%d**%d = %d\n",x, y, power1(x,y)); printf("%d**%d = %d\n",x, y, power2(x,y)); return 0; }