complex number to power

In my program I'm supposed to take a complex number to a power, however, my program only works when a complex number is taken to the second power and nothing beyond that. How can I fix my ExpC function?
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
  #include <iostream>
#include "complex_1.hpp"
int main()
{
    char choice;
    complex a,b,c;
    int n, m;
    cout << endl;
    cout << "Enter a complex number pair such as (5.1,2.3i): ";
    a.InputC(cin);
    cout << "Enter a complex number pair such as (5.1,2.3i): ";
    b.InputC(cin);
    cout << "Enter a choice: (a)dd, (s)ub,(m)ult,(d)ivide, or (e)xponent: ";
    cin >> choice;
    switch (choice)
    {
        case 'a': c= a.AddC(b);
            
            break;
        case 's': c= a.SubC(b);
            
            break;
        case 'm': c= a.MultC(b);
            
            break;
        case 'd': c= a.DivC(b);
            
            break;
        case 'e' :
        cout << "Which complex pair do you want to be taken to a power?\nEnter a choice 1 or 2:" << endl;
        cin >> m;
        cout << "Enter an integer for the compelx number to be taken a power to " << endl;
        cin >> n;
        if(m==1)
          c=a.ExpC(a,n);
        if(m==2)
           c=b.ExpC(b,n);
        else 
           cout << "Entry error";

            break;
        default : cout << "Entry error";
    }
    cout << endl;
    cout << "Result is: ";
    c.OutputC(cout);
    cout << endl;
    return 0;
}


complex_1.cpp
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
#include<iostream>
#include "complex_1.hpp"

complex complex::AddC(complex x)
{
    complex temp;
    temp.real= real +x.real;
    temp.imag= imag + x.imag;
    return(temp);
}

complex complex::SubC(complex x)
{
    complex temp;
    temp.real= real -x.real;
    temp.imag= imag - x.imag;
    return(temp);
}

complex complex::MultC(complex x)
{
    complex temp;
    temp.real= (real * x.real)-(imag * x.imag);
    temp.imag= (imag * x.real)+(real * x.imag);
    return(temp);
}
complex complex::DivC(complex x)
{
    complex temp;
    temp.real = (real*x.real + imag*x.imag)/(x.real*x.real+x.imag*x.imag);
    temp.imag = (imag*x.real - real*x.imag)/(x.real*x.real + x.imag*x.imag);
    return(temp);
}
 complex complex:: ExpC ( complex x, int n)
 {
        for (int i=1; i<n; i++)
            x = x.MultC( x);
    return x;
 }
void complex:: InputC(istream &in)
{
    char i,paren,comma;
    in >> paren>> real>> comma>> imag >>i>> paren;
}

void complex:: OutputC(ostream &out)
{
    cout << '(' << real << ',' << imag << "i)" << endl;
}



complex_1_hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#ifndef complex_1_hpp
#define complex_1_hpp

#include<iostream>
#include <stdio.h>
using namespace std;

class complex
{
public:
    void InputC(istream &in);
    void OutputC(ostream &out);
    complex AddC(complex x);
    complex SubC(complex x);
    complex MultC(complex x);
    complex DivC(complex x);
    complex ExpC(complex x, int n);
private:
    float real, imag;
};

#endif /* complex_1_hpp */ 
Lets say that n==4.
The x^4 can be expressed as x*x*x*x, doesn't it?

In your loop:
i=1, a=x*x
i=2, b=a*a
i=3, c=b*b

The result is c.
c == b*b == (a*a)*(a*a) == (x*x*x*x)*(x*x*x*x)

In other words, result equals x^8.


You should mimic:
1
2
3
result=1;
for ( size_t i=0; i<n; ++i )
  result = result * x;
Topic archived. No new replies allowed.