Can someone compile this for me?

I'm trying to make a function that can calculate a number raised to a power, but my console isn't loading so I can't see what it's doing. Can anyone check it for me? Also, I need to test all the possible outcomes, my code doesn't test decimals, any suggestions?
Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86


#include <iostream>

using namespace std;

double mypow (float, int);

int main()
{
	int size = 10;
	int test[size];
	int base;
	
	for (base = -2; base < 4; base += 2)
	{
		for (int expon = -2; expon < 4; expon += 2)
		{
			cout << base<< "  " << expon << " " << mypow(base, expon) << endl;
		}
	}
	
	cout << endl;
	
	return 0;
}

double mypow (float num, int raisedTo)
{
    int product;
    
    if (raisedTo < 0)
    {
    	 for (num = num; num <= raisedTo; num++)
          {
               product = 1 / (num * num);
               
              
          }  
    
    }
        
    else if (raisedTo == 0)
    {
         product = 1;
         return product;
    }

    while (num > 0)
    {
          
          for (num = num; num <= raisedTo; num++)
          {
               product = num * num;
               return product;
          }  
    }

    while (num <= -1)
    {
       if(raisedTo % 2 == 0)
       {
          for (num = num; num <= raisedTo; num++)
          {
              product = num * num;
              return product;
          }
       }
       else 
           for (num = num; num <= raisedTo; num++)
           {
               product = num * num * -1;
               return product;
           }
       
       
    }
    
    
	
}




Last edited on
That site didn't work, I don't know what's going
I changed my code a bit, not sure which is better, my outputs aren't working correctly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

double power(float, int);

int main(int argc, char** argv) {
	
   for (int base = -2; base < 4; base +=2)
   {
   		for (int exp = -2; exp < 4; exp += 2 )
   		{
   			cout << base << " " << exp << " " << power (base, exp) << endl;
   		}
   }

	return 0;
}
double power(float base, int exp)
{
	  float product=base;
	  
	  if (exp == 0)
	  {
	  	return 1;
	  }
	  
	  else if (exp < 0)
	  {
	  	for (int num = exp; num > 1; num--) 
	  {
	  	product *= base;
	  }
	  return 1 / product; 	
	  }
      
      for (int num = exp; num > 1; num--) 
	  {
	  	product *= base;
	  }
      return product;
}
Last edited on
Try http://www.compileonline.com/ for an online compiler.

zero raised to any power should be zero. Also, your for loop conditions need to be different for negative numbers. It's probably best to just rewrite your code to use the absolute value of the exponent, that way you only need to write one loop.
Actually, 00 is an indeterminate form, but in many cases, it's treated as 1 just for simplicity/laziness/consistency.
Also, zero raised to a negative power is undefined (division by zero).

As far as your actual program goes, you just need to change this for loop:
29
30
31
32
for (int num = exp; num > 1; num--) 
{
    product *= base;
}

Since exp is less than 0, this should be
for (int num = -exp; num > 1; num--)
instead.
Topic archived. No new replies allowed.