user11324089
user11324089

Reputation:

How to use arrays of complex variables in C++?

I am very new in C++ programming and I need a help about declaring array of complex nubers which are double type. This is my test code:

#include <string.h> 
#include <iostream>

int main()
{
    int i,j;
    const int BrGr = 3;

    complex* Z_gra = new complex[BrGr];

    return 0;
 }

Can I get explanation how to use complex arrays in C++?

Upvotes: 0

Views: 395

Answers (1)

Elvir Crncevic
Elvir Crncevic

Reputation: 485

Have a look at https://en.cppreference.com/w/cpp/numeric/complex

Example code:

#include <iostream>
#include <iomanip>
#include <complex>
#include <cmath>
#include <vector>

int main()
{
    using namespace std::complex_literals;
    std::cout << std::fixed << std::setprecision(1);

    std::complex<double> z1 = 1i * 1i;     // imaginary unit squared
    std::cout << "i * i = " << z1 << '\n';

    std::complex<double> z2 = std::pow(1i, 2); // imaginary unit squared
    std::cout << "pow(i, 2) = " << z2 << '\n';

    double PI = std::acos(-1);
    std::complex<double> z3 = std::exp(1i * PI); // Euler's formula
    std::cout << "exp(i * pi) = " << z3 << '\n';

    std::complex<double> z4 = 1. + 2i, z5 = 1. - 2i; // conjugates
    std::cout << "(1+2i)*(1-2i) = " << z4*z5 << '\n';

    std::vector<std::complex<double>> elements{1. + 2i}; // Array of complex numbers
    return 0;
}

Upvotes: 2

Related Questions