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
}

You need to give better details than "strange numbers". What's your input? What's your output? What were you expecting? What part of the program do we need to look at?

input = scanf("%f %d", &base, &exp);

scanf() returns an int type, however input is of float type. Not that you use input for any reason.

float pos_power(float base, int exponent)

pos_power returns a float type, and result = pos_power(base, exp); is expecting it, however you do not make any provision for that in your function. Not that you use result for any reason. main() should implicitly be written as int main accepting either (void) , (int argc, char *argv[]) , or (int argc, char *argv[], char *env[]) if you need any of those parameters passed to it.
The return of main could be: return 0 ; return EXIT_SUCCESS or return EXIT_FAILURE , if you make use of the stdlib.h header file for the latest two.

You need to give better details than "strange numbers". What's your input? What's your output? What were you expecting? What part of the program do we need to look at?

Here's what I received with an input of 2 and 3. I was hoping I'd receive an 8.0

Please enter a base and positive exponent
Base, exponent: 2 3
Enter pos_power: base = 2.000000 exponent= 3
Exit pos_power: result = -0.000124

What is value equal to when you enter into your function? The answer is any random bytes that happened to fall into that region all interpreted as a float. I know you've seen this issue before in other threads.

Initialize the value variable in the function pos_power. This will solve the problem. Check the code below

float pos_power(float base, int exponent)
/* Given: A base and exponent
 * Return: Value of base raised to positive exponent
*/
{ 
   float value=0;                        // initialize the value variable
   int i = 1;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.