I am trying to find the value of a given base and exponent, with the exponent being an integer greater or equal to 0. However, I am getting strange numbers at output. Could someone please take a look at my code ?
/* File: driver3.c
* Date: February 28, 2010
*/
/* This program calculates the value of a given base and exponent */
/* Function declaration */
float pos_power(float base, int exponent);
#include <stdio.h>
#define DEBUG
main()
{
/* Declarations */
float base; /* Given base */
int exp; /* Given exponent */
float input; /* Input of base and exponent */
float result;
/* Program intro */
printf("Please enter a base and positive exponent\n");
/* Obtain input */
printf("Base, exponent: ");
input = scanf("%f %d", &base, &exp);
if (exp >= 0)
{
result = pos_power(base, exp);
}
else
{
printf("Please enter an exponent greater than 0");
}
} /* End main */
float pos_power(float base, int exponent)
/* Given: A base and exponent
* Return: Value of base raised to positive exponent
*/
{ float value;
int i = 1;
#ifdef DEBUG
printf("Enter pos_power: base = %f exponent= %d\n", base, exponent);
#endif
while (i <= exponent)
{
value = base * value;
i = i + 1;
}
#ifdef DEBUG
printf("Exit pos_power: result = %f\n", value);
#endif
}