Creating an array of Complex numbers in C++ -


i want create array store complex numbers such first input number of entries n . each entry first real part of number followed complex part .

eg: n=2  1.0 -1.0 0 3  

is representation of numbers 1-i , 3i respectively . tried .

#include<iostream> #include<complex>  typedef std::complex<double> complex;  int main() { int n; std::cin>>n; complex a[n];  for(int i=0;i<n;i++) std::cin>>a[i];  for(int i=0;i<n;i++) std::cout<<a[i]<<" ";  return 0 ; } 

i new c++ programming . please tell me went wrong .

first, variable length arrays introduce cin >> n; complex a[n] not part of c++ standard , might not supported compiler. second, if compiler supports vlas in general, not support create vla of non-pod (plain old data) type std::complex.

to overcome both issues, use std::vector instead of plain array:

std::vector<complex> a(n); 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -