Using structs (see lecture notes) create a library of functions to deal with com
ID: 647228 • Letter: U
Question
Using structs (see lecture notes) create a library of functions to deal with complex numbers. You will need to define the following functions:
CMPLX create_complex (double, double);
/* create a complex number from a real and imaginary part */
CMPLX add (CMPLX, CMPLX);
/* Add two complex numbers */
CMPLX subtract (CMPLX, CMPLX);
/* Subtract the first number from the second */
CMPLX multiply (CMPLX, CMPLX);
/* Multiply two complex numbers */
void print_complex (CMPLX);
/* Print out a complex number in a reasonable format */
float magnitude (CMPLX);
/* Return the magnitude of the complex number */
In all these examples, CMPLX is the typedef name you used for your complex number. Hint - obviously the first thing you must do is correctly set up a struct with a typedef for a complex variable. You can use two floats, one for the real and one for the imaginary part. Remember, once you've set up the typedef, then CMPLX behaves just like a built in type such as an int, you can return it from functions, use it as a variable type and an argument in a function.
i) Test your library using these functions - for example, you should be able to write:
CMPLX a,b;
a= create_complex (10.1,5.3);
b= create_complex (5.2,6.3);
printf ("a = ");
print_complex(a);
printf (" ");
printf ("b= ");
print_complex(b);
printf (" ");
printf ("a + b = ");
print_complex(add_complex(a,b));
printf (" ");
ii) Print some example output from your library. Show that it can cope with addition, subtraction and multiplication. Show that it prints reasonable answers, even when the real or imaginary parts of a complex number are zero. Produce output to show how your program deals with the following (you can use copy and paste to put output from a window into a document):
(7+2i) + (6 - 2i) =
(7+2i) - (6 - 2i) =
(3.1+2i) (-3.1+5i) =
7i(-2+6i) =
((2 + 4i) - (3.5 + 2i)) (7 + 11i) =
S
Explanation / Answer
import java.io.*;
class CMPLX
{
float a,b;
void init(float j,float k)
{
a=j;
b=k;
}
void CMPLXADD(CMPLX com1,CMPLX com2)
{
a=com1.a+com2.a;
b=com1.b+com2.b;
}
static CMPLX CMPLXSUB(CMPLX com1,CMPLX com2)
{
CMPLX c=new CMPLX();
c.a=com1.a-com2.a;
c.b=com1.b-com2.b;
return c;
}
static CMPLX CMPLXMUL(CMPLX com1,CMPLX com2)
{
CMPLX c=new CMPLX();
c.a=((com1.a)*(com2.a))-((com1.b)*(com2.b));
c.b=((com1.a)*(com2.b))+((com1.b)*(com2.a));
return c;
}
static CMPLX CMPLXDIV(CMPLX com1,CMPLX com2)
{
CMPLX c=new CMPLX();
c.a=(((com1.a)*(com2.a))+((com1.b)*(com2.b)))/(((com2.a)*(com2.a))-((com2.b)*(com2.b)));
c.b=(-((com1.a)*(com2.b))+((com1.b)*(com2.a)))/(((com2.a)*(com2.a))-((com2.b)*(com2.b)));
return c;
}
void MAGNITUDE(CMPLX com1)
{
double magnitude = Math.sqrt(Math.pow(com1.a, 2) + Math.pow(com1.b, 2));
System.out.println(magnitude);
}
}
class HelloWorld
{
public static void main(String args[]) throws IOException
{
while(true)
{
System.out.println(" 1.CMPLXADD 2.CMPLXSUB 3.CMPLXMUL 4.CMPLXDIV 5.CMPLXMAG 6.EXIT");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i;
i=Integer.parseInt(br.readLine());
if(i==6)
System.exit(1);
CMPLX c1=new CMPLX();
float a,b;
System.out.println("enter first complex number");
a=Float.parseFloat(br.readLine());
b=Float.parseFloat(br.readLine());
c1.init(a,b);
System.out.println("enter second complex number");
CMPLX c2=new CMPLX();
a=Float.parseFloat(br.readLine());
b=Float.parseFloat(br.readLine());
c2.init(a,b);
CMPLX c3=new CMPLX();
switch(i)
{
case 1:
c3.CMPLXADD(c1,c2);
System.out.println("result:"+c3.a+"+"+c3.b+"i");
break;
case 2:
c3=CMPLX.CMPLXSUB(c1,c2);
System.out.println("resultr:"+c3.a+"+"+c3.b+"i");
break;
case 3:
c3=CMPLX.CMPLXMUL(c1,c2);
System.out.println("result:"+c3.a+"+"+c3.b+"i");
break;
case 4:
c3=CMPLX.CMPLXDIV(c1,c2);
System.out.println("result:"+c3.a+"+"+c3.b+"i");
break;
case 5:
c3.MAGNITUDE(c1);
break;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.