Dumb question about complex arrays in c++

In summary: Thanks!In summary, Warren is trying to learn C++ and is having difficulty with complex arrays. He decides to use one of the simpler arrays instead and is able to figure it out.
  • #1
Dr Transport
Science Advisor
Insights Author
Gold Member
2,606
800
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.
 
Technology news on Phys.org
  • #2
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:
  • #3
Not quite AKG. You forgot the star:

Compex *array = new Complex[n];

- Warren
 
  • #4
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.
 
  • #5
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.
 
  • #6
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
 
  • #7
I was afraid of that. I'll have to mess around with it...

Thanks

dt
 
  • #8
Could always make a vector of vectors; that's relatively painless.
 
  • #9
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.
 

1. What is an array in C++?

An array in C++ is a data structure that allows you to store a collection of values of the same data type in a contiguous memory location.

2. How do I declare and initialize an array in C++?

To declare and initialize an array in C++, you use square brackets after the data type to indicate that it is an array, followed by the name of the array and its size in square brackets. For example: int myArray[5];

3. How do I access elements in an array in C++?

You can access elements in an array in C++ by using its index, which starts at 0. For example, myArray[0] will access the first element in the array, myArray[1] will access the second element, and so on.

4. Can I change the size of an array in C++?

No, the size of an array in C++ is fixed at the time of declaration and cannot be changed.

5. How do I use loops to iterate through an array in C++?

You can use a for loop or a while loop to iterate through an array in C++. The loop will start at index 0 and continue until the last index of the array, using the array's size as the limit. Within the loop, you can access each element of the array using its index.

Similar threads

  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
20
Views
3K
  • Programming and Computer Science
Replies
1
Views
944
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
22
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
3K
Back
Top