Figuring out pseudocode and fixing methods for complex number program

The assignment is as follows:

"Create a class for working with complex numbers. Only 2 private float data members are needed, the real part of the complex number and the imaginary part of the complex number. The following methods should be in your class:

a.) A default constructor that uses default arguments in case no initializers are included in the main.
b.) Add 2 complex numbers and store sum
c.) Subtract 2 complex numbers and store difference
d.) Multiply 2 complex numbers and store product
e.) Print a complex number in the form of a+bi OR a-bi where a is the real part of the complex number and b is the imaginary part of the complex number
f.) Change a complex number to its square

Your main should instantiate two complex numbers and call each of the class methods. The 2 complex numbers should be printed, along with the sum, difference, product, and the squares. **PLEASE DO NOT OVERLOAD THE OPERATORS FOR THIS PROGRAM.**"


Here's what I have done so far:

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
#include<iostream>
#include<iomanip>
using namespace std;

class Complex
{
public:
	Complex(float = 0.0, float = 0.0); //default constructor that uses default arg. in case no init. are in main
	void getComplex(); //get real and imaginary numbers from keyboard
	void sum(Complex, Complex); //method to add two complex numbers together
	void diff(Complex, Complex); //method to find the difference of two complex numbers
	void prod(Complex, Complex); //method to find the product of two complex numbers
	void square(Complex, Complex); //method to change each complex number to its square
	void printComplex(); //print sum, diff, prod, square and "a+bi" form 

private: 
	float real; //float data member for real number (to be entered in by user)
	float imaginary; //float data member for imaginary number (to be entered in by user)
};

Complex::Complex(float r, float i)
{	
	real = r;
	imaginary = i;
}

void Complex::getComplex()
{
	cout << "Enter real number: ";
	cin >> real;
	cout << "Enter imaginary number: ";
	cin >> imaginary;
}

void Complex::sum(Complex r, Complex i)
{
	sum = r + i;
}

void Complex::diff(Complex r, Complex i)
{
	diff = r - i;
}

void Complex::prod(Complex r, Complex i)
{
	prod = r * i;
}

void Complex::square(Complex r, Complex i)
{
	square = r*r;
	square = i*i;
}

void Complex::printComplex()
{
	cout << sum;
	cout << diff;
	cout << prod;
	cout << square;
	cout << "Form is: " << real << "+" << imaginary << endl;
}


int main()
{
	Complex c;
	c.getComplex();
	c.sum(Complex r, Complex i);
	c.diff(Complex r, Complex i);
	c.prod(Complex r, Complex i);
	c.square(Complex r, Complex i);
	c.printComplex();

	return 0;
}



Am I following the instructions properly? Am I missing anything? Any hints in the right direction would be very appreciated...I have errors underneath all the calculation methods: i.e. "sum = r + i;" has a redline under "sum" and the "+" but not the "r" and the "i" ...same error is under diff, prod and square. In addition, I have errors in the main underneath all the "Complex r" instances. But not the "Complex i" instances...I have NO idea what goes in there to satisfy the compiler. Unfortunately, the error messages in C++ are garbage compared to Python and Java so I'm tearing my hair out. I would have posted this on StackOverflow but I know it will get marked as "duplicate" "too broad" and downvoted into oblivion by some user who's so coked out on the gamification that he doesn't want to help anything that's not unsolvable.

Thank you guys for helping me out!
Last edited on
be careful, complex is a type in c++ already. It shouldn't be a problem, but it can be if you accidentally tap into the existing stuff.

you can't add just anything. You can only add things for which + has been defined, and your class isn't one of those things (yet, your teacher will get to this later).

void Complex::sum(Complex r, Complex i)
{
sum = r + i;
}

should be

Complex Complex::sum(Complex a, Complex b) //r and I are confusing...
//you must fix the header also.
{
Complex s;
s.real = a.real + b.real;
s.imaginary = a.imaginary+b.imaginary;
return s;
}

... fixing multiple issues.
1) R & I parameters indicate you thought they were the parts of the class. The whole class comes in here, so r&i are poor names.
2) sum for a function name and a variable are confusing. Don't re-use names unless you do it with a purpose (there are times for it).
3) you were trying to add complex, which does not define a + operation.
4) you didn't add the 2 parts of the complex, which is the correct math for adding complex values.
5) you treated a class member variable like a global. Just return the value from the function, its prettier to write

a = sum(b,c);
than what you had, which leads to
sum(b,c);
a = ??//where do you get the result from, b.variable? c.variable? Either?


Do you understand what I am saying here? You are on the right track, but these fixes I made are *critical* to understanding this assignment and are KEY to being able to use classes in code.

See if you can get just sum to work. Then build up. Start small, get it working, grow it.

c++ predates java and python by decades. There are some oddities due to its age, but its more powerful in a lot of ways too. It takes time to learn the error messages but once you do, they are very helpful for fixing the issues. Java is more or less a copy of c++ with a lot of the good stuff taken out... the syntax for objects isn't that different... if you know java, this shouldn't be that difficult for you.



Last edited on
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
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

class ComplexNum //I decided to play it safe and stick with your recommendation to avoid "complex" as a class name...
{
public:
    ComplexNum(float = 0.0, float = 0.0); //default constructor that uses default arg. in case no init. are in main
    void getComplexNum(); //get real and imaginary numbers from keyboard
    void sum(ComplexNum a, ComplexNum b); //there's something wrong here, but I'm not sure what it is. 
    void printComplexNum(); //print sum, diff, prod, square and "a+bi" form

private: 
    float real; //float data member for real number (to be entered in by user)
    float imaginary; //float data member for imaginary number (to be entered in by user)
};

ComplexNum::ComplexNum(float a, float b) //took your advice and made it easier to read
{   
    real = a;
    imaginary = b;
}

void ComplexNum::getComplexNum()
{
    cout << "Enter real number: ";
    cin >> real;
    cout << "Enter imaginary number: ";
    cin >> imaginary;
}

ComplexNum ComplexNum::sum(ComplexNum a, ComplexNum b) //I'm getting a redline underneath "sum" and it's mentioning line 11 of my program is causing this problem: "declaration is incompatible with void sum(ComplexNum a, ComplexNum b)"
{
    ComplexNum s; //this format should work for diff, prod, square and form easily, it's very straightforward...thank you!
    s.real = a.real + b.real;
    s.imaginary = a.imaginary + b.imaginary;
    return s;
}

void ComplexNum::printComplexNum()
{
    cout << sum;
}


int main()
{
    ComplexNum c;
    c.getComplexNum();
    c.sum(a, b); //was advised on S.O. to use "a,b" here but it's not working...in fact the error it gives me is: "identifier a is undefined" (same error with b, as well). 
    c.printComplexNum();

    return 0;
}


Dear Jonnin, thank you so much for the detailed analysis of my code: you went above and beyond my expectations! Unfortunately, I have not been able to make sum work yet. I'm almost there. I just have 2 redlines left to solve. And I should be good to go from there for the other parts once I understand the logic behind why these errors are occurring. I have redlines on 33 and 51 respectively. 33 is a redline underneath "sum" and it mentions that it is incompatible with the line declared at 11. And finally I have an error on 51, mentioning that both "a" and "b" are undefined. Should I pass "float a, float b" into main? Because that does work, but I don't think it's a permanent solution (or it might be?). Thanks so much for your help!
Last edited on
Let's take a step back. What should c.sum(a,b) actually do?
1. should it add a and b, and set c to the sum?
2. should it add a and b, and return the sum? In that case why is c involved?

Your main program looks like it assumes #1. To make that work, you should change the definition of sum() back to returning void and have it modify this.
void ComplexNum::sum(ComplexNum a, ComplexNum b)
{
this->real = a.real + b.real;
this->imaginary = a.imaginary + b.imaginary;
}

printComplex() won't compile. Remember, in order to print out the value of your own class, you have to print the values of the appropriate members. For now, let's just print out the real and imaginary values:
1
2
3
4
void ComplexNum::printComplexNum()
{
    cout << real << ',' << imaginary;
}

Later when you have some of the code working, you can change this to print it in the format requested.

Your main program is wrong. You don't define a or b. Also why are you getting c from the user? Isn't c supposed to hold the result? It should be :
1
2
3
4
5
6
7
8
9
10
int main()
{
    ComplexNum a,b,c;
    a.getComplexNum(); // get the number a
    b.getComplexNum(); // get the number b
    c.sum(a, b); // add a and b, storing the result in c
    c.printComplexNum(); // print c

    return 0;
}


Before you do prod() and square(), be sure to review how one multiplies complex numbers. You have to be sure to get the math right.

Once you have prod() working, can you code square() using prod()? After all, square() just multiplies a number by itself, why not use prod() for the multiplication?

Thank you for the help! It worked perfectly! You guys put S.O. to shame!
Last edited on
Topic archived. No new replies allowed.