Question

In: Computer Science

There is an engineering project that uses complex numbers, electrical circuits. you need to develop a...

There is an engineering project that uses complex numbers,
electrical circuits. you need to develop a user-defined structure type and a set of operations that will make complex arithmetic virtually as straightforward as arithmetic on C’s built-in numeric types. You will need to define functions for complex I/O as well as for the basic arithmetic operations (addition, subtraction, and for finding the absolute value of a complex number).

Hint: the complex number a + bi has a real part a and an imaginary part b, where the symbol i represents 1.

|a + bi| = '( a)2+(b)2

PROGRAM: C

Solutions

Expert Solution

#include <stdio.h>
#include <stdlib.h>

struct complex
{
  int real, img;
};

int main()
{
  int choice, x, y, z;
  struct complex a, b, c;

  while(1)
  {
  printf("Press 1 to add two complex numbers.\n");
  printf("Press 2 to subtract two complex numbers.\n");
  printf("Press 3 to multiply two complex numbers.\n");
  printf("Press 4 to divide two complex numbers.\n");
  printf("Press 5 to exit.\n");
  printf("Enter your choice\n");
  scanf("%d", &choice);

  if (choice == 5)
  exit(0);

  if (choice >= 1 && choice <= 4)
  {
  printf("Enter a and b where a + ib is the first complex number.");
  printf("\na = ");
  scanf("%d", &a.real);
  printf("b = ");
  scanf("%d", &a.img);
  printf("Enter c and d where c + id is the second complex number.");
  printf("\nc = ");
  scanf("%d", &b.real);
  printf("d = ");
  scanf("%d", &b.img);
  }
  if (choice == 1)
  {
c.real = a.real + b.real;
c.img = a.img + b.img;

  if (c.img >= 0)
  printf("Sum of the complex numbers = %d + %di", c.real, c.img);
  else
  printf("Sum of the complex numbers = %d %di", c.real, c.img);
  }
  else if (choice == 2)
  {
c.real = a.real - b.real;
c.img = a.img - b.img;

  if (c.img >= 0)
  printf("Difference of the complex numbers = %d + %di", c.real, c.img);
  else
  printf("Difference of the complex numbers = %d %di", c.real, c.img);
  }
  else if (choice == 3)
  {
c.real = a.real*b.real - a.img*b.img;
c.img = a.img*b.real + a.real*b.img;

  if (c.img >= 0)
  printf("Multiplication of the complex numbers = %d + %di", c.real, c.img);
  else
  printf("Multiplication of the complex numbers = %d %di", c.real, c.img);
  }
  else if (choice == 4)
  {
  if (b.real == 0 && b.img == 0)
  printf("Division by 0 + 0i isn't allowed.");
  else
  {
x = a.real*b.real + a.img*b.img;
y = a.img*b.real - a.real*b.img;
z = b.real*b.real + b.img*b.img;

  if (x%z == 0 && y%z == 0)
  {
  if (y/z >= 0)
  printf("Division of the complex numbers = %d + %di", x/z, y/z);
  else
  printf("Division of the complex numbers = %d %di", x/z, y/z);
  }
  else if (x%z == 0 && y%z != 0)
  {
  if (y/z >= 0)
  printf("Division of two complex numbers = %d + %d/%di", x/z, y, z);
  else
  printf("Division of two complex numbers = %d %d/%di", x/z, y, z);
  }
  else if (x%z != 0 && y%z == 0)
  {
  if (y/z >= 0)
  printf("Division of two complex numbers = %d/%d + %di", x, z, y/z);
  else
  printf("Division of two complex numbers = %d %d/%di", x, z, y/z);
  }
  else
  {
  if (y/z >= 0)
  printf("Division of two complex numbers = %d/%d + %d/%di",x, z, y, z);
  else
  printf("Division of two complex numbers = %d/%d %d/%di", x, z, y, z);
  }
  }
  }
  else
  printf("Invalid choice.");

  printf("\nPress any key to enter choice again...\n");
  }
}


Related Solutions

Prepare a two-page project proposal dealing with electrical engineering technology. You should research the field of...
Prepare a two-page project proposal dealing with electrical engineering technology. You should research the field of electrical engineering technology and come up with a project idea that is relevant and appropriate.
How do an Electrical Engineering student need Calculus II ? Find an Application that relates to...
How do an Electrical Engineering student need Calculus II ? Find an Application that relates to Electrical Engineering?
I need some ideas for software engineering project
I need some ideas for software engineering project
Whats final project ideas for a electrical engineering major. Include all your work from your final...
Whats final project ideas for a electrical engineering major. Include all your work from your final project.
To develop or acquire a real estate project, a sponsor may need to attract an equity...
To develop or acquire a real estate project, a sponsor may need to attract an equity partner since lenders generally will not finance 100% of a project's cost. Though there are many ways to compensate a financial partner for putting equity in a deal, a common scenario would be: a. Simple percentage of net cash flow from property b. Preferred return on investor capital, a negotiated share of residual cash flow and priority distributions on sale c. Upfront cash fee...
I need new ideas for chemical engineering project, new ideas plz , thanks
I need new ideas for chemical engineering project, new ideas plz , thanks
1. Suppose you are a project manager using waterfall approach on a large and complex project....
1. Suppose you are a project manager using waterfall approach on a large and complex project. Your manager has just read the latest article in Computerworld that advocates replacing waterfall with Extreme Programming (EP) and comes to your office requesting you to switch. What would you say? 2. Imagine you were an analyst developing a new EIS (Executive Information System) intended to provide key strategic information from existing corporate database to senior executives to help in their decision making. What...
What numbers on a drawing allows you to indicate a specific location on an engineering drawing?...
What numbers on a drawing allows you to indicate a specific location on an engineering drawing? Notes Dimensions Zone Scales When you’re drawing an orthographic projection with AutoCAD, which of the following layers will not be active (turned on) when the drawing is printed? Cutting Hidden Construction Dimension Which of the following AutoCAD features allows you to access and quickly execute a menu of commands (such as MOVE, COPY, and STRETCH) by right-clicking on a small box that appears on...
What do you think are the key items and issues that need to be implemented to manage a system engineering organization created for a project implementation?
  Systems engineering organizations are often created for just one project. What do you think are the key items and issues that need to be implemented to manage a system engineering organization created for a project implementation? Consider the following in your response: 1.The major aspects of organizing and staffing a team.2.Processes that are needed 3.How the project is staffed 4.Where personnel will be located 5.How personnel will be trained 6.Tools that will be needed
I need the six numbers for phase_2 separated by space. This is called Bomblab Project. Breakpoint...
I need the six numbers for phase_2 separated by space. This is called Bomblab Project. Breakpoint 2, 0x0000000000400f49 in phase_2 () (gdb) disas Dump of assembler code for function phase_2: => 0x0000000000400f49 <+0>: push %rbp 0x0000000000400f4a <+1>: push %rbx 0x0000000000400f4b <+2>: sub $0x28,%rsp 0x0000000000400f4f <+6>: mov %fs:0x28,%rax 0x0000000000400f58 <+15>: mov %rax,0x18(%rsp) 0x0000000000400f5d <+20>: xor %eax,%eax 0x0000000000400f5f <+22>: mov %rsp,%rsi 0x0000000000400f62 <+25>: callq 0x40173e <read_six_numbers> 0x0000000000400f67 <+30>: cmpl $0x0,(%rsp) 0x0000000000400f6b <+34>: jns 0x400f72 <phase_2+41> 0x0000000000400f6d <+36>: callq 0x401708 <explode_bomb> 0x0000000000400f72 <+41>:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT