power-compare.c - Et program der tæller antallet af multiplikationer i de to potens funktioner. Lektion 8 - slide 15 : 27Program 1

```#include <stdio.h>

double sqr(double);
int even (int);
int odd (int);

int mult_no_1, mult_no_2;

double power1(double number, int pow){
double result;

if (pow == 0)
result = 1.0;
else if (pow > 0){
result = number * power1(number, pow - 1);
mult_no_1++;
}
else
result = 1.0 / power1(number, -pow);

return result;
}

double power2(double number, int pow){
double result;

if (pow == 0)
result = 1.0;
else if (pow > 0 && even(pow)){
result = sqr(power2(number,pow/2));
mult_no_2++;
}
else if (pow > 0 && odd(pow)){
result = number * power2(number, pow - 1);
mult_no_2++;
}
else
result = 1.0 / power2(number, -pow);

return result;
}

int main(void) {
int i;
double res1, res2;

for(i = 1; i < 500; i += 5){
mult_no_1 = 0; mult_no_2 = 0;
res1 = power1(1.01,i);
res2 = power2(1.01,i);
printf("power1(1.01,%i) = %7.5f (%i multiplications)\n"
"power2(1.01,%i) = %7.5f (%i multiplications). \n\n",
i, res1, mult_no_1,
i, res2, mult_no_2);
}

return 0;
}

double sqr(double d){
return d*d;}

int even (int i){
return i % 2 == 0;}

int odd (int i){
return i % 2 != 0;}```