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.
I am in this electrical engineering project course in which an assignment is asking me this...
I am in this electrical engineering project course in which an assignment is asking me this and I cannot make heads or tails of what it means by this... Discuss the tradeoffs involved between using patents and trade secrets to protect intellectual property. Please be as descriptive as possible, THANKS
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
EENG 1910: Project I – Introduction to Electrical Engineering Assignment-8 1. Create a MATLAB function that...
EENG 1910: Project I – Introduction to Electrical Engineering Assignment-8 1. Create a MATLAB function that will take as inputs the radius (r) and height (h) of a cone and calculate its volume. The formula to compute the volume of a cone is as follows. 1 ? = 3 ??2ℎ Write a descriptive comment on the use of the function so that the user by typing help nameofyourfunction has all the necessary information to use your function. In each of...
Develop a python program to convert two decimal numbers (A and B) to binary numbers. You...
Develop a python program to convert two decimal numbers (A and B) to binary numbers. You should generate B complement signal (flip all the bits of Input B,
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT