C ONLY PLEASE Write a program that uses functions and structure to perform the f
ID: 3865084 • Letter: C
Question
C ONLY PLEASE Write a program that uses functions and structure to perform the following operations:
a. Reading and writing complex number
b. Addition and Multiplication of 2 complex numbers Complex Numbers Ex: 3+2i (real part is 3 and imaginary part is 2) Program must:
• Define structure having one real (r) and one imaginary (i) member.
• Create two structure variables and read the real and imaginary parts of two complex numbers (e.g. c1, c2).
• Compute the sum of complex numbers by adding real part with real and imaginary with imaginary. Store this result in another struct variable. o (c3. R = c1. R + c2. R and c3. I = c1. I + c2. I)
• Compute the product of complex numbers and store results into another struct variable, by using the formula: c3. R = (c1. R * c2. R) – (c1. I * c2. I) c3. I = (c1. R * c2. I) – (c1. I * c2. R) • Display the complex numbers. Complex numbers can be visually displayed like this: (r, i).
Explanation / Answer
#include <stdio.h>
struct ComplexNumber //structure
{
int real;
int imag;
};
struct ComplexNumber Sum(struct ComplexNumber c1,struct ComplexNumber c2) //sum function
{
struct ComplexNumber c;
c.real = c1.real + c2.real;
c.imag = c1.imag + c2.imag;
return c;
}
struct ComplexNumber Product(struct ComplexNumber c1,struct ComplexNumber c2)//product function
{
struct ComplexNumber c;
c.real = (c1.real * c2.real) - (c1.imag * c2.imag);
c.imag = (c1.real * c2.imag) - (c1.imag * c2.real);
return c;
}
void display(struct ComplexNumber c) //display function
{
printf("(%d,%di)",c.real,c.imag);
}
int main()
{
struct ComplexNumber c1,c2; //structure variables
printf(" Enter the complex number cn1 : ");
scanf("%d %d",&c1.real,&c1.imag);
printf(" Enter the complex number cn2 : ");
scanf("%d %d",&c2.real,&c2.imag);
printf(" Sum of complex numbers : ");
display(Sum(c1,c2));
printf(" Product of complex numbers : ");
display(Product(c1,c2));
return 0;
}
Output:
Enter the complex number cn1 : 3 2
Enter the complex number cn1 : 4 1
Sum of complex numbers : (7,3i)
Product of complex numbers : (10,-5i)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.