Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Dumb question about complex arrays in c++

  1. Jul 15, 2004 #1

    Dr Transport

    User Avatar
    Science Advisor
    Gold Member

    In trying to learn C++ (I'm an old FORTRAN guy), I am unable to figure out how to define and initialize a complex array. Single complex numbers are straight forward, but going further has me perplexed some.

    Thanks in advance.
     
  2. jcsd
  3. Jul 16, 2004 #2

    AKG

    User Avatar
    Science Advisor
    Homework Helper

    I assume you have a Complex class.

    Complex *array = new Complex [n];

    I think that should work. "new" is a keyword, "array" is the variable name for the array, and "n" would be an integer variable, of course you can put in your own number if you want, e.g. replace "n" with "5".

    EDITED based on chroot's comment.
     
    Last edited: Jul 16, 2004
  4. Jul 16, 2004 #3

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Not quite AKG. You forgot the star:

    Compex *array = new Complex[n];

    - Warren
     
  5. Jul 18, 2004 #4

    Dr Transport

    User Avatar
    Science Advisor
    Gold Member

    Thanks, how about a 2d array???

    complex **array = new complex [n][n]; ????

    and maybe an n-d array, I use tensors in 3-d and 4-d in my old fortran codes.
     
  6. Jul 18, 2004 #5

    dduardo

    User Avatar
    Staff Emeritus

    Using dynamic memory allocation for multi dimensional arrays isn't as trivial as you think. You actually have to create an array of arrays. You'll first need to allocate a single dimensional array. Then using a loop allocate an array within each cell. You can get some pretty nasty code especially with higher dimesions. Believe it or not, this is one thing you probable want to stick with fortran to do.
     
  7. Jul 18, 2004 #6

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    You only need to do as dduardo suggested if you are using non-trivial data types. If you're just making a multi-dimensional array of ints or doubles, etc., you don't need to do anything special besides allocating the memory with the new operator.

    - Warren
     
  8. Jul 18, 2004 #7

    Dr Transport

    User Avatar
    Science Advisor
    Gold Member

    I was afraid of that. I'll have to mess around with it......

    Thanks

    dt
     
  9. Jul 18, 2004 #8

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Could always make a vector of vectors; that's relatively painless.
     
  10. Jul 18, 2004 #9

    Dr Transport

    User Avatar
    Science Advisor
    Gold Member

    I think I got it figured out, as Hurkyl and dduardo, either make a vector of vectors or an array of arrays. After I got a couple of hints from the community, it actually came very quickly.

    On to other things about C++.....maybe I'll succeed in converthing those old codes I have to further some old projects that I can't seem get working on other machines and operating systems.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Dumb question about complex arrays in c++
  1. C++ question (Replies: 8)

  2. C question (Replies: 6)

Loading...