Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > array of complex numbers in MSVC++ 6
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 21st April 22:08
eric johnson
External User
 
Posts: 1
Default array of complex numbers in MSVC++ 6



I am an admitted C/C++ hack. My question involves defining and assigning
values to an array of complex nmbers, using Visual C++ 6.0.

What I'm trying to do is read in a data file, assigning the data to an
array (that part works). Then I want to create arrays of complex

numbers from the data I've read in.

Here's an example of what I'm doing:

#include <math.h>
#include <stdio.h>
#include <fstream.h>
#include <iostream>
#include <complex>

int main()
{

const int NumRows = 101;
const int NumCols = 9;
double Spar[NumRows][NumCols];

// deleted code for reading data from a file, will set up a few dummy
values for this example

Spar[0][0] = 0.1;
Spar[0][1] = 0.2;
Spar[0][2] = 0.3;
Spar[0][3] = 0.4;
Spar[0][4] = 0.5;
Spar[0][5] = 0.6;
Spar[0][6] = 0.7;
Spar[0][7] = 0.8;
Spar[0][8] = 0.9;

// define a complex array
std::complex<double> s11[NumRows];

// assign values to the first element
s11[0] = (Spar[0][1], Spar[0][2]);

//define and assign a complex number
std::complex<double> s11_n(Spar[0][1], Spar[0][2]);

// print out the array value
std::cout << s11[0] << std::endl;

cout << s11[0].real() << endl;
cout << s11[0].imag() << endl;

// print out the other complex number
cout << s11_n.real() << endl;
cout << s11_n.imag() << endl;

return 0;
}

This compiles and runs, but the output is this:
(0.3,0)
0.3
0
0.2
0.3

What appears to be happening is the second complex value (non array) is
correct. The complex number should be 0.2 +0.3i. For the array,

the imaginary part (0.3) is going into the real portion, and a zero is
going into imaginary portion. Am I missing something obvious here?

Thanks for any help with this.

Eric
ericmj@mindspring.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
  Reply With Quote


  sponsored links


2 21st April 22:09
bo persson
External User
 
Posts: 1
Default array of complex numbers in MSVC++ 6



"Eric Johnson" <ericmj@mindspring.com> skrev i meddelandet
news:Xns93AF4C5EDB302ericmjmindspringcom@198.99.14 6.10...

assigning

an


dummy


This should probably be:

s11[0] = std::complex<double>(Spar[0][1], Spar[0][2]);


is

is

No, but something non-obvious. :-)

You have run into the dreaded comma operator

f(x, y) evaluates x and y and passes them to f (whatever that is)

(x, y) just evaluates x and y, then returns y

Bo Persson
bop2@telia.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666