C Tutorial

C Tutorial C Language Environment Setup Execution flow of C program C printf and Scanf C Data type C Token Variable in C Operators in C Comments in C Escape Sequence in C C – Storage Classes C Decision control statement Loop Statement in C Break, continue and goto statement in C Type Casting in C Function in C Recursion in C String in C C Array Pointer in C Dynamic memory allocation C –Structure Nested Structure in C Union in C File Handling in C C pre-processor Static Function In C Sizeof In C Selection Sort In C Scope Of Variables In C Runtime Vs Compile Time In C Random Access Lseek In C Queue Implementation In C Pseudo Code In C Prototype In C Pointer To Pointer In C Pointer Arithmetic In C Passing Array To Function In C Null Character In C Merge Sort In C Macros In C Library Functions In C Memory Leak In C Int In C Goto And Labels In C Fibonacci Series In C Fflush In C Derived Data Types In C Data Types In C Const Vs Volatile In C Character Set In C Character Class Tests In C Calloc In C C Pointers Arrays In C Include In C Clrscr In C C Vs Java String Literals In C Types Of Pointers In C Variables In C Volatile In C Why C Is A Middle Level Language Infix To Postfix Program In C Ceil function in C LCM of two numbers in C Quick sort in C Static in C function pointer as argument in C Top Array Keywords in C Add two numbers using the function in C Armstrong program in C using function Array, Declaring Arrays and Array Initialization Limitations of Inline Function in C Merge and Merge sort with example in C Do-While Loop in C For Loop in C While-Loop in C Difference between while and do-while loop in C Array Of Structures in C Data Structures And Algorithms in C Types Of Structures In C How to Avoid Structure Padding in C Use of Structure in C Do WHILE LOOP in C Programming Examples For Loop in C Programming Examples Entry Control Loop in C Exit control loop in C Infinite loop in C Nested loop in C pow() function in C String Handling functions in C Prime Number code in C Factorial Program in C using For Loop Factorial Program in C Using While Loop Fibonacci Series in C Using For Loop Fibonacci series in C using while loop Prime Number Program in C using for Loop While Loop in C programming examples Built-in functions in C Assert() Function C vs Java Strings Call Back Function in Embedded C Else If Ladder fgets() function Ftell() Function getc() function getch() function gets() function Heap Sort Nested if-else statement Pi() Function Positioning of file Write() function abs() function in C Attributes in C C program to find factorial of a number using Recursion Ferror() in c fopen() function in C Fibonacci series program in C using Recursion Formatted Input and output function in C Snake Game in C User Defined Functions in C Beep() function in C Cbrt() function in C Hook() function in C Isalnum() function in C C Program to find the Roots of a Quadratic Equation C Switch Statements Difference between rand() and srand() function in C Difference between while and for loop in C Doubly Linked list in C Example of Iteration in C How to use atoi() function in C How to use floor() function in C How to use sine() function in C How to use Typedef Struct in C Integer Promotions in C C Program Swap Numbers in cyclic order Using Call by Reference C Program to Find Largest Number Using Dynamic Memory Allocation C Program to Find the Largest Number using Ternary Operator C/C++ Program to Find the Size of int, float, double and char Find the Largest Three Distinct Elements in an Array using C/C++ Loop Questions in C Modulus on Negative Numbers in C Multiplication table program in C using For loop Nested Loops in C Programming Examples C Program for Mean and Median of an Unsorted Array Results of Comparison Operations in C and C++ Reverse a Stack using Recursion in C Simple hash() function in C strcat() Function in C Sum of N numbers in C using For loop Use of free() function in C Write a program that produces different results in C and C++ C Function Argument and Return Values Keywords in C Bank management system in C Calendar application in C Floor() Function in C Free() Function in C How to delete a file in C How to move a text in C Remove an element from an array in C Unformatted input() and output() function in C What are linker and loader in C SJF Scheduling Program in C Socket Programming in C Structure in C Tower of Hanoi in C Union Program in C Variable Declaration in C What is Linked List in C While Loop Syntax in C fork() in C GCD program in C Branching Statements in C Comma Operator in C Control statement in C Double Specifier in C How to create a binary file in C Long int in C Palindrome Number in C Pure Virtual Function in C Run Time Polymorphism in C Types of Array in C Types of Function in C What is a buffer in C What is required in each C Program Associativity of Operators in C Bit Stuffing Program in C Actual and Formal Parameters Addition of two Numbers in C Advantages of function in C Arithmetic Progression Program in C Binomial Coefficient Program in C Difference between Array and List in C Diffie-Hellman Algorithm in C How to convert a number to words in C How to convert a string to hexadecimal in C Difference between If and Switch Statement in C C and C++ Binary Files C program that does not Suspend when Ctrl+Z is Pressed Different ways to Declare the Variable as Constant in C Range of Int in C C Program to find the size of a File FIFO Example in the C Language For loop in C Programming GCD program of two numbers in C GPA Calculator in C How to Calculate Time Complexity in C How to include graphics.h in C How to measure time taken by a function in C How to return a Pointer from a Function in C What is the main in C Addition of Matrix in C Booleans in C C Program for Extended Euclidean algorithms C Program of Fencing the Ground Ceil and Floor in C Compound Interest Program in C Displaying Array in C Distance Vector Routing Protocol Program in c Dos.h Header File in C Language DSA Program in C Explain the two-way selection in C Fee Management System in C File Operations in C Malloc function in C Multiplication Table in C Simple Programs in C Language tolower() Function in C Type Conversion in the C Why does sizeof(x++) not Increment x in C Advantages of Dynamic Memory Allocation in C Armstrong Number in C Assignment Operator Program in C Banker’s Algorithm in C Binary Search in C with Best and Worst Time Complexity Caesar Cipher Program in C Call by Value and Call by Reference in C Conditional Operator in C CRC Program in C Deadlock Detection Program in C Decimal to Binary in C Difference between If Else and Nested If Else in C Difference between Pre-increment and Post-increment in C Difference between Scope and Lifetime in C Evaluation of Arithmetic Expression in C Explain the Increment and Decrement Operators in C Fseek Function in C Functions in C How to Find Square Free Numbers in C Length of an Array Function in C OpenGL in C Projects on C language in 2023 Purpose of a Function Prototype in C Stdio.h in C Two-Dimensional array in C What is String Comparison in C C Compilers for Windows Functions and Recursion in C How to Declare Boolean in C How to Declare Character in C How to Round up a number in C How to use strlen() in C Pointer Declaration in C Algorithm for String Palindrome in C C Program to find ASCII value of a character Constant Pointer in C How to find string length in C using strlen() function Implicit and Explicit in C Indirect Recursion in C Input and Output functions in C isupper() in C Jump Statement in C Lifetime of a Variable in C Linker Error in C Language Numeric Constant in C Size of Pointer in C Square Root in C Language Static and Dynamic Memory allocation String Declaration in C Strong Number in C Symmetric Matrix in C Types of C Tokens What is a Size of Pointer in C What is Increment and Decrement Operator in C 1 2 3 4 Series Program in C Advantages and Disadvantages of C Language C Program for Polynomial Addition C Program to Count the Number of Vowels in a String C Programming Errors and Solutions Compilation Errors in C Complex C Programs Difference between Argument and Parameter in C Difference between char s[] and char *s in C Evaluation of Postfix Expression Using Stack in C Find Leftmost and Rightmost Set Bit of a Number fprintf and fscanf in C Introduction to Dynamic Array in C Print Address in C Realloc function in C Ternary Operators in C Types of Tokens in C with Examples Difference between Static and Dynamic Memory Allocation in C Addition Program in C Array Definition in C Array of Pointers in C Arrow Operator in C Average of Two Numbers in C Binary to Decimal in C Binary to Octal in C BREAK STATEMENT in C C Programming Operators Questions C Programs Asked in Interview Calculator Program in C C Program to Read and Print an Employee's Detail Using Structure Bubble Sort Algorithm in C C Program to Find Area and Perimeter of Circle C Program to Check Whether a Given Number is Even or Odd C in Roman Numerals C Program to Make a Simple Calculator Using Switch Case Insertion Sort Program in C How to take input in string in C GCC Conflicting Types in C Function Definition in C Format Specifier for Hexadecimal in C Flowchart in C Float in C Fizzbuzz Implementation in C Conditional Statement in C Conio.h functions list in C Constants in C Dynamic Array in C Decision Making Statements in C Continue Statement in C Creation of Thread in C DFS Algorithm in C Difference between parameter and arguments in C Dijkstra's Algorithm in C Leap Year Program in C Jump Statements in C Modulus Operator in C Memory Allocation in C Simple Interest Program in C Reverse Array in C Recursive Function in C Queue in C Printing Pascal’s Triangle in C Preprocessor Directives in C Perror() in C Perfect Number in C Programming Language Parameter Passing Techniques in C Pascal Triangle in C Patterm Program in C Affine cipher in C Dereferencing pointer in C Internal static variable vs External static variables in C Difference between exit(0) and exit(1) in C Booth's Algorithm in C Condition Control Statements in C Double Specifier in C Dynamic variables in C How to print alphabets in C How to print char array in c Order of Evaluation in C Order of Operations in C Semantic Error in C Size of String Variable in C SJF PREEMPTIVE SCHEDULING PROGRAM C: Tree in C Arithmetic Progression Program in C Array, Declaring Arrays, and Array Initialization ARRAYS IN C Assert() Function in c Atoi in C Bar3d() function in C Graphics Beep function in c Bigint (BIG INTEGERS) in C with Example Builtin functions of GCC compiler Fibonacci series in C Priority Queue in C 2D ARRAY IN C 7 Best IDEs for C/C++ Developers in 2022 Addition of Two Numbers in C Advantages and Disadvantages of C Language Advantages of Function in C Algorithm for String Palindrome in C What is fgets in C language 2d Shearing Program in C Recursion Questions In C Static Identifier in C Inserting elements in an array using C Language complex.h Header File in C fsetpos()(set file position) in C Comparator function of qsort() in C Print Hex in C strnset() function in C %d in C Butterfly Star pattern in C C form format in Excel Scan Line Polygon Filling in C 8 Queens Problem in C Back Trace in C BSS (Block Started by Symbol) Data Segment in C language Class C in Networking Strings in C Programming What is Circular linked list? What is Loop in C BITWISE OPERATOR IN C LANGUAGE BOMB DIFFUSION PROGRAM IN C LANGUAGE

complex.h Header File in C

Introduction:

The <complex.h> header file in C is part of the C standard library and provides facilities for working with complex numbers. Complex numbers are numbers that have both a real part and an imaginary part, expressed in the form a + bi, where a is the real part, b is the imaginary part, and i is the imaginary unit (the square root of -1).

The <complex.h> header file introduces several functions and macros for performing operations on complex numbers.

Data types:

  • double complex: Has double the precision of a complex number.
  • Float complex: It represents a complex number with a single precision.
  • Long double complex: It displays a complex number with extreme precision.

Functions:

  • Cabs (), cabs (), call (): These functions compute the magnitude (absolute value) of a complex number.
  • cart (), cargo (), cargo (): These functions compute the phase angle (argument) of a complex number.
  • cexp(), cexpf(), cexpl(): These functions compute the exponential function of a complex number.
  • clog(), clogf(), clogl(): These functions compute the natural logarithm of a complex number.
  • cpow(), cpowf(), cpowl(): These functions compute the power of a complex number.
  • cconj(), cconjf(), cconjl(): These functions compute the complex conjugate of a complex number.
  • csqrt(), csqrtf(), csqrtl(): These functions compute the square root of a complex number.

Macros:

  • CREAL(), CIMAG(): These macros extract the real and imaginary parts of a complex number.
  • CMPLX(), CMPLXF(), CMPLXL(): These macros create a complex number from real and imaginary parts.

Program:

#include <stdio.h>

#include <complex.h>

int main() {

// Creating complex numbers

double complex z1 = 1.0 + 2.0 * I;

double complex z2 = 3.0 - 4.0 * I;

// Performing operations

double complex sum = z1 + z2;

double complex product = z1 * z2;

double complex conjugate_z1 = conj(z1); // Fix: use conj() instead of cconj()

double magnitude_z2 = cabs(z2);

double complex sqrt_z1 = csqrt(z1);

// Displaying results

printf("z1: %.2f + %.2fi\n", creal(z1), cimag(z1));

printf("z2: %.2f + %.2fi\n", creal(z2), cimag(z2));

printf("Sum: %.2f + %.2fi\n", creal(sum), cimag(sum));

printf("Product: %.2f + %.2fi\n", creal(product), cimag(product));

printf("Conjugate of z1: %.2f + %.2fi\n", creal(conjugate_z1), cimag(conjugate_z1));

printf("Magnitude of z2: %.2f\n", magnitude_z2);

printf("Square root of z1: %.2f + %.2fi\n", creal(sqrt_z1), cimag(sqrt_z1));

return 0;

}

Output:

z1: 1.00 + 2.00i

z2: 3.00 + -4.00i

Sum: 4.00 + -2.00i

Product: 11.00 + 2.00i

Conjugate of z1: 1.00 + -2.00i

Magnitude of z2: 5.00

Square root of z1: 1.27 + 0.79i

Explanation:

  • Complex Numbers in C:

Complex numbers are represented in C using the <complex.h> header.

Data types like double complex are used to declare complex variables with double precision.

  • Operations on Complex Numbers:

Addition and Subtraction: Complex numbers can be added or subtracted using the + and - operators.

Multiplication: Multiplication of complex numbers is performed using the * operator.

Conjugate: The conjugate of a complex number is obtained by changing the sign of its imaginary part. In the code, conj() is used for this purpose.

Magnitude: The magnitude (absolute value) of a complex number is calculated using the cabs() function.

Square Root: The square root of a complex number is obtained using the csqrt() function.

  • Imaginary Unit (I):

The imaginary unit is represented by I in C, and it is defined as the square root of -1.

  • Displaying Results:

The creal() and cimag() functions are used to extract the real and imaginary parts of complex numbers, respectively.

Results are displayed using printf() statements.

  • Using Standard C Library Functions:

The code utilizes standard C library functions like conj() for calculating the complex conjugate.

  • Compilation and Linking:

When using standard C library functions, there's no need to explicitly link with the math library (-lm) because these functions are part of the standard library.

  • Key Observations:

The application demonstrates how to create complex numbers, carry out fundamental operations, and extract data, including real and imaginary components.

Complex numbers are used to simplify mathematical expressions, including real and imaginary components.

Complexity analysis:

Time Complexity:

The time complexity of the code is primarily determined by the operations performed on complex numbers. In this case, the time complexity is influenced by the mathematical operations such as Addition, Multiplication, and square root calculations. The time complexity of these operations is generally considered constant or O(1) because they involve a fixed number of arithmetic operations.

The printf() statements used for displaying results also contribute to the time complexity, but their impact is typically minimal compared to the complex number operations.

Space Complexity:

The space complexity of the code is determined by the memory required to store variables. In this code, complex numbers (z1, z2, sum, product, conjugate_z1, sqrt_z1) are declared, and each of them requires space proportional to the precision used (e.g., double precision).

The space complexity is also influenced by the usage of additional variables, such as magnitude_z2, which requires space for a double.

Finally, the space complexity of the code is O(1) for a fixed number of complex number variables and additional variables. The space required is constant and does not grow with the input size.

Approach 2: Interactive Complex Number Calculator.

This approach allows users to interactively choose the operations they want to perform on complex numbers.

Program:

#include <stdio.h>

#include <complex.h>

int main() {

double real1, imag1, real2, imag2;

// Input for the first complex number

printf("Enter real and imaginary parts for the first complex number:\n");

scanf("%lf %lf", &real1, &imag1);

// Input for the second complex number

printf("Enter real and imaginary parts for the second complex number:\n");

scanf("%lf %lf", &real2, &imag2);

// Create complex numbers

double complex z1 = real1 + imag1 * I;

double complex z2 = real2 + imag2 * I;

int choice;

printf("Choose an operation:\n");

printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");

scanf("%d", &choice);

switch (choice) {

case 1:

// Addition

printf("Sum: %.2f + %.2fi\n", creal(z1 + z2), cimag(z1 + z2));

break;

case 2:

// Subtraction

printf("Difference: %.2f + %.2fi\n", creal(z1 - z2), cimag(z1 - z2));

break;

case 3:

// Multiplication

printf("Product: %.2f + %.2fi\n", creal(z1 * z2), cimag(z1 * z2));

break;

case 4:

// Division

if (cabs(z2) != 0) {

printf("Quotient: %.2f + %.2fi\n", creal(z1 / z2), cimag(z1 / z2));

} else {

printf("Error: Division by zero.\n");

}

break;

default:

printf("Invalid choice.\n");

}

return 0;

}

Output:

Enter real and imaginary parts for the first complex number:

2.5 3.0

Enter real and imaginary parts for the second complex number:

1.0 -2.0

Choose an operation:

1. Addition

2. Subtraction

3. Multiplication

4. Division

3

Product: 8.50 + -2.00i

Explanation:

  • User Input:

The program starts by prompting the user to enter the real and imaginary parts for two complex numbers.

  • Complex Number Representation:

Complex numbers are represented using the double complex data type in C.

The real and imaginary parts entered by the user are used to create complex numbers.

  • Menu-Driven Interface:

After entering the complex numbers, the program presents a menu-driven interface to the user.

The user can choose from different operations such as Addition, subtraction, Multiplication, and division.

  • Switch Statement:

The program uses a switch statement to execute the specific operation chosen by the user.

  • Operation Execution:

The program performs the corresponding operation on the complex numbers depending on the user's choice.

Operations include Addition, subtraction, Multiplication, and division.

  • Error Handling:

The program checks for potential errors, such as division by zero, when performing the division operation.

If an invalid choice is entered, the program provides feedback to the user.

  • Displaying Results:

The program displays the result of the chosen operation, including both the real and imaginary parts.

  • Interactivity:

This program provides an interactive and dynamic experience, allowing users to input their complex numbers and choose the operations they want to perform.

Complexity analysis:

Time Complexity:

User Input:

The time complexity for user input is typically considered O(1) or constant time. The number of operations remains the same regardless of the size of the input.

Complex Number Creation:

Creating complex numbers involves simple arithmetic operations. The time complexity is considered O(1) for each complex number creation.

Menu-Driven Interface and Operation Execution:

The switch statement for the menu-driven interface and the subsequent execution of the chosen operation are also constant time operations (O(1)).

Error Handling:

Error handling, such as checking for division by zero, involves basic conditional checks, which are O(1).

Finally, The overall time complexity is dominated by O(1) operations, making the time complexity of the program constant.

Space Complexity:

User Input and Complex Number Storage:

The space complexity is influenced by the storage of user input values and the creation of complex numbers. A double precision real and imaginary part represents each complex number. Therefore, the space complexity for storing two complex numbers is O(1).

Menu-Driven Interface and Operation Execution:

This program does not use additional data structures that grow with input size. The space complexity for the menu-driven interface and operation execution is O(1).

Variables:

The program uses a few variables to store user input, complex numbers, and intermediate results. The space complexity for these variables is O(1).

Finally, the space complexity is constant (O(1)) as the program uses a fixed amount of memory regardless of the input size.

Approach 3: Polynomial Operations with Complex Coefficients:

Extend the use of complex numbers to represent coefficients in polynomials. This approach involves creating functions to perform operations like polynomial Addition, Multiplication, and evaluation where the coefficients are complex numbers. This approach is useful in numerical analysis and signal processing, where complex numbers can be involved in polynomial representations.

Program:

#include <stdio.h>

#include <complex.h>

typedef struct {

int degree; // Degree of the polynomial

double complex *coeff; // Coefficients (array of complex numbers)

} Polynomial;

Polynomial initializePolynomial(int degree, double complex *coeff) {

Polynomial poly;

poly.degree = degree;

poly.coeff = coeff;

return poly;

}

Polynomial addPolynomials(Polynomial poly1, Polynomial poly2) {

int maxDegree = (poly1.degree > poly2.degree) ? poly1.degree : poly2.degree;

double complex resultCoeff[maxDegree + 1];

for (int i = 0; i <= maxDegree; ++i) {

double complex coeff1 = (i <= poly1.degree) ? poly1.coeff[i] : 0.0;

double complex coeff2 = (i <= poly2.degree) ? poly2.coeff[i] : 0.0;

resultCoeff[i] = coeff1 + coeff2;

}

return initializePolynomial(maxDegree, resultCoeff);

}

Polynomial multiplyPolynomials(Polynomial poly1, Polynomial poly2) {

int resultDegree = poly1.degree + poly2.degree;

double complex resultCoeff[resultDegree + 1];

for (int i = 0; i <= resultDegree; ++i) {

resultCoeff[i] = 0.0;

for (int j = 0; j <= poly1.degree; ++j) {

for (int k = 0; k <= poly2.degree; ++k) {

if (j + k == i) {

resultCoeff[i] += poly1.coeff[j] * poly2.coeff[k];

}

}

}

}

return initializePolynomial(resultDegree, resultCoeff);

}

double complex evaluatePolynomial(Polynomial poly, double complex z) {

double complex result = 0.0;

for (int i = 0; i <= poly.degree; ++i) {

result += poly.coeff[i] * cpow(z, poly.degree - i);

}

return result;

}

int main() {

double complex coeff1[] = {1.0 + 2.0*I, -2.0 + 1.0*I, 3.0 - 4.0*I};

double complex coeff2[] = {0.0 + 1.0*I, 2.0 + 3.0*I};

Polynomial poly1 = initializePolynomial(2, coeff1);

Polynomial poly2 = initializePolynomial(1, coeff2);

Polynomial sum = addPolynomials(poly1, poly2);

Polynomial product = multiplyPolynomials(poly1, poly2);

// Evaluate the polynomials at z = 2.0 + 1.0*I

double complex z = 2.0 + 1.0*I;

double complex value1 = evaluatePolynomial(poly1, z);

double complex value2 = evaluatePolynomial(poly2, z);

printf("Polynomial 1: ");

for (int i = poly1.degree; i >= 0; --i) {

printf("(%g + %gi)z^%d ", creal(poly1.coeff[i]), cimag(poly1.coeff[i]), i);

if (i > 0) printf("+ ");

}

printf("\n");

printf("Polynomial 2: ");

for (int i = poly2.degree; i >= 0; --i) {

printf("(%g + %gi)z^%d ", creal(poly2.coeff[i]), cimag(poly2.coeff[i]), i);

if (i > 0) printf("+ ");

}

printf("\n");

printf("Sum: ");

for (int i = sum.degree; i >= 0; --i) {

printf("(%g + %gi)z^%d ", creal(sum.coeff[i]), cimag(sum.coeff[i]), i);

if (i > 0) printf("+ ");

}

printf("\n");

printf("Product: ");

for (int i = product.degree; i >= 0; --i) {

printf("(%g + %gi)z^%d ", creal(product.coeff[i]), cimag(product.coeff[i]), i);

if (i > 0) printf("+ ");

}

printf("\n");

printf("Evaluation of Polynomial 1 at z = %g + %gi: %g + %gi\n", creal(z), cimag(z), creal(value1), cimag(value1));

printf("Evaluation of Polynomial 2 at z = %g + %gi: %g + %gi\n", creal(z), cimag(z), creal(value2), cimag(value2));

return 0;

}

Output:

Polynomial 1: (3 + -4i)z^2 + (-2 + 1i)z^1 + (1 + 2i)z^0

Polynomial 2: (2 + 3i)z^1 + (0 + 1i)z^0

Sum: (1.55565e-316 + 1.55565e-316i)z^2 + (6.95324e-310 + 4.2259e+159i)z^1 + (1.01856e-312 + 6.95324e-310i)z^0

Product: (6.92465e-310 + 6.92465e-310i)z^3 + (0 + 6.95324e-310i)z^2 + (0 + 0i)z^1 + (1.55565e-316 + 2.07634e-317i)z^0

Evaluation of Polynomial 1 at z = 2 + 1i: -7 + 6i

Evaluation of Polynomial 2 at z = 2 + 1i: 1 + 5i

Explanation:

  • Polynomial Structure:

A Polynomial structure is defined to represent a polynomial. It includes two members: degree (the degree of the polynomial) and coeff (an array of complex numbers representing coefficients).

  • Polynomial Initialization:

The initializePolynomial function is responsible for initializing a polynomial. It takes the degree and an array of complex coefficients as input and returns a Polynomial structure.

  • Polynomial Addition:

The addPolynomials function forms the addition of two polynomials. It takes two Polynomial structures as input, adds their corresponding coefficients, and returns a new Polynomial representing the sum.

  • Polynomial Multiplication:

The multiplyPolynomials function performs Multiplication of two polynomials. It multiplies each term of the first polynomial with each term of the second polynomial and accumulates the result. The resulting polynomial has a degree equal to the sum of the degrees of the input polynomials.

  • Polynomial Evaluation:

The evaluatePolynomial function evaluates a polynomial for a given complex value z. It uses the Horner's method or a similar approach to efficiently compute the result.

  • Main Function:

The main function demonstrates the usage of the polynomial operations. It initializes two polynomials with complex coefficients, performs Addition and Multiplication, and evaluates the polynomials at a specific complex value z.

  • Example Usage:

The example polynomials used in the main function are created with complex coefficients. The result of Addition, Multiplication, and evaluations are printed for illustrative purposes.

  • Explanation of Outputs:

The final output includes the representation of the input polynomials, the sum and product of the polynomials, and the evaluations of the polynomials at a specified complex value.

  • Potential Applications:

This code is relevant in numerical analysis and signal processing, where complex numbers are commonly used. Representing coefficients as complex numbers allows for more expressive and accurate modeling of mathematical functions, especially in domains where complex values naturally occur.

  • Customization and Further Development:

The code can be extended by adding more operations, handling edge cases, or incorporating additional functionalities based on specific requirements.

Complexity analysis:

Time Complexity:

Polynomial Addition (addPolynomials):

O(max(degree of poly1,degree of poly2))

Polynomial Multiplication (multiplyPolynomials):

O(degree of poly1×degree of poly2)

Polynomial Evaluation (evaluatePolynomial):

O(degree of the polynomial)

Space Complexity:

Polynomial Initialization (initializePolynomial):

O(degree of the polynomial)

Polynomial Addition (addPolynomials):

O(max(degree of poly1,degree of poly2))

Polynomial Multiplication (multiplyPolynomials):

O(degree of poly1×degree of poly2)

Polynomial Evaluation (evaluatePolynomial): O(1)

Approach 4: Complex Number Calculator with Trigonometric Functions:

This approach enhances the interactive complex number calculator by incorporating trigonometric functions such as sine, cosine, and tangent for complex numbers. Users can input complex numbers, choose trigonometric operations, and visualize results in both rectangular and polar forms. This approach can provide insights into the relationship between complex numbers and trigonometry.

Program:

#include <stdio.h>

#include <math.h>

#include <complex.h>

// Function to print a complex number

void printComplex(double complex num) {

printf("(%g + %gi)\n", creal(num), cimag(num));

}

int main() {

double real1, imag1, real2, imag2;

printf("Enter the real and imaginary parts of the first complex number: ");

scanf("%lf %lf", &real1, &imag1);

printf("Enter the real and imaginary parts of the second complex number: ");

scanf("%lf %lf", &real2, &imag2);

// Create complex numbers

double complex z1 = real1 + imag1 * I;

double complex z2 = real2 + imag2 * I;

// Basic operations

double complex sum = z1 + z2;

double complex diff = z1 - z2;

double complex prod = z1 * z2;

double complex quot = z1 / z2;

// Trigonometric functions

double complex sin_z1 = csin(z1);

double complex cos_z1 = ccos(z1);

double complex tan_z1 = ctan(z1);

// Display results

printf("\nResults in Rectangular Form:\n");

printf("Sum: ");

printComplex(sum);

printf("Difference: ");

printComplex(diff);

printf("Product: ");

printComplex(prod);

printf("Quotient: ");

printComplex(quot);

printf("\nResults in Polar Form:\n");

printf("Magnitude of z1: %g\n", cabs(z1));

printf("Phase of z1: %g radians\n", carg(z1));

printf("\nTrigonometric Functions of z1:\n");

printf("sin(z1): ");

printComplex(sin_z1);

printf("cos(z1): ");

printComplex(cos_z1);

printf("tan(z1): ");

printComplex(tan_z1);

return 0;

}

Output:

Enter the real and imaginary parts of the first complex number: 3 4

Enter the real and imaginary parts of the second complex number: 1 -2

Results in Rectangular Form:

Sum: (4 + 2i)

Difference: (2 + 6i)

Product: (11 + -2i)

Quotient: (-1 + 2i)

Results in Polar Form:

Magnitude of z1: 5

Phase of z1: 0.927295 radians

Trigonometric Functions of z1:

sin(z1): (3.85374 + -27.0168i)

cos(z1): (-27.0349 + -3.85115i)

tan(z1): (-0.000187346 + 0.999356i)

Explanation:

  • Complex Number Input:

Users input the real and imaginary parts of two complex numbers.

  • Conversion to Complex Numbers:

The input is converted into complex numbers using the I constant provided by the <complex.h> header.

  • Basic Operations:

The code performs Addition, subtraction, Multiplication, and division of the complex numbers using the standard operators.

  • Trigonometric Functions:

Trigonometric functions (csin, ccos, ctan) are applied to one of the complex numbers.

  • Display Results:

Results are printed in both rectangular and polar forms.

For polar form, the magnitude and phase of a complex number are calculated using cabs and carg functions.

Complexity analysis:

Time Complexity:

Input:

Reading the real and imaginary parts of the complex numbers.

Time complexity: O(1) (constant time).

Conversion to Complex Numbers:

Creating complex numbers from the input.

Time complexity: O(1) (constant time).

Basic Operations:

Addition, subtraction, Multiplication, and division are basic arithmetic operations and have time complexities proportional to the size of the input.

Time complexity: O(1) (constant time).

Trigonometric Functions:

Sine, cosine, and tangent functions are complex mathematical operations, and their time complexities depend on the underlying implementation.

Time complexity: The trigonometric functions are typically implemented using specialized algorithms, and their complexities can be considered O(1) or O(logn), where n is the number of bits in the representation.

Display Results:

Printing results to the console.

Time complexity: O(1) (constant time).

Space Complexity:

Complex Numbers and Variables:

Variables to store real, imaginary parts, and complex numbers.

Space complexity: O(1) (constant space).

Results:

Variables to store results of arithmetic and trigonometric operations.

Space complexity: O(1) (constant space).

Overall:

The space complexity is dominated by the variables used to store input, intermediate results, and output.

Space complexity: O(1) (constant space).