Nov 29, 2009 at 12:54am Nov 29, 2009 at 12:54am UTC
Hello People...HAPPY BELATED THANKSGIVING...I'm having problems trying to get this Complex program to do certain functions...Here is my problems...
* Suppose that each complex number represent a point on XY-plane*Add one function to the complex class that calculate the distance from one point to another point* double distance( Complex a, Complex b );*Add the prototype in header file and function definition in source file*Write a driver program(contain main()function) that ask user to enter three complex numbers a,b,c( real and img part for each number)*Decide if the three point form a right triangle*Decide if the three points are on a same circle centered at (0,0)*Print a message that states if the three numbers will form a right triangle or on one same circle or neither...This is what I have 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 78 79 80 81 82 83 84 85 86 87 88
#include <iostream> // Directive for iostream library.
using namespace std; //Namespace is a collection of identifiers in the standard library.
class Complex // Type Object
{
double real;
double img;
public : // Constructors
Complex(double a=0, double b=0);
void set_real(double r);
void set_img(double g);
double get_real();
double get_img();
void print();
Complex plus(Complex c);
Complex minus(Complex c);
Complex mul(Complex c);
Complex div(Complex c);
};
#include "Complex.h"
Complex::Complex(double r, double g)
{
real = r;
img = g;
}
void Complex::set_real(double r) {real = r;} // Complex Member Functions
void Complex::set_img(double g) {img = g;}
double Complex::get_real() {return real;}
double Complex::get_img() {return img;}
void Complex::print()
{
cout<< real<<"+" <<img<<"i" ;
cout<< "(" <<real<<", " <<img<<")" ;
}
Complex Complex::plus(Complex c) // Complex Operator
{
Complex x;
x.real = real + c.real;
x.img = img + c.img;
return x;
}
Complex Complex::minus(Complex c) // Complex Operator
{
Complex x;
x.real = real - c.real;
x.img = img - c.img;
return x;
}
Complex Complex::mul(Complex c) // Complex Operator
{
Complex x;
x.real = real * c.real -img * c.img;
x.img = img * c.real + real * c.img;
return x;
}
Complex Complex::div(Complex c) // Complex Operator
{
Complex x;
double temp = c.real * c.real + c.img * c.img;
x.real = (real * c.real + img * c.img) / temp;
x.img = (img * c.real - real * c.img) / temp;
return x;
}
#include "Complex.h"
int main()
{
Complex total;
Complex *ptr;
double r, g;
int num, i;
cout << "\n Enter how many numbers of complex numbers\n" ;
cin >> num;
ptr = new Complex[num];
for ( i = 0; i < num; i++)
{
cout <<"Enter the pair of numbers for " <<i+1<<"th number\n" ;
cin>>r>>g;
ptr[i].set_real (r);
ptr[i].set_img (g);
total = total = total.plus (ptr[i]);
}
cout<<"The total is " ;
total.print();
cout<<endl;
return 0;
}
Last edited on Nov 29, 2009 at 3:54am Nov 29, 2009 at 3:54am UTC
Nov 29, 2009 at 3:49pm Nov 29, 2009 at 3:49pm UTC
Your arithmetic requires code like:
Complex c1, c2;
//...
c1.plus(c2);
Whereas, you might want to write
Complex c3 = c1 + c2;
So you really should have operators +, - and so on.
Those are points of style and may not be important here, but something to think about non the less.
They form a right-angle triangle if one angle of the formed triangle is 90 degrees.
They lie on the same radius if the distance to the centre (0,0) is the same. Pythagoras' theorem comes to mind.
Nov 29, 2009 at 5:00pm Nov 29, 2009 at 5:00pm UTC
thanks that's great but how can I apply this when the input from the user will be a set of numbers whereas I must take and then calculate whether it's a right triangle or on one same circle (0,0) or neither???
Nov 29, 2009 at 5:43pm Nov 29, 2009 at 5:43pm UTC
Four different complex numbers
a,b,c,d
lie on a cirlce if and only if
( (a-d)/(b-d) ) * ((b-c)/(a-c))
is a real number (i.e. if its imaginary part equals zero).
Hope this helps!
[Edit:] Or they lie on a line (line = circle of infinite radius). Three complex points a,b,c lie on a line when
(a-b)/(a-c)
is a real number.
Last edited on Nov 29, 2009 at 5:51pm Nov 29, 2009 at 5:51pm UTC