A square matrix with complex numbers

This is a discussion on A square matrix with complex numbers within the C Programming forums, part of the General Programming Boards category; Alright, I've been working on complex numbers, and I have the structure for it... Code: struct cmplx { double r; ...

  1. #1
    Registered User
    Join Date
    May 2010
    Location
    Isla Vista
    Posts
    34

    A square matrix with complex numbers

    Alright, I've been working on complex numbers, and I have the structure for it...
    Code:
    struct cmplx {
    	double r;
    	double i;
    };
    Now, if I wanted to make a matrix with cmplx numbers, could I just...
    Code:
    int main(){
    cmplx a,b,c,d, A[2][2];
    a.r=0
    a.i=0
    b.r=0
    b.i=0
    c.r=0
    c.i=0
    d.r=0
    d.i=0
    A[0][0]=a;
    A[0][1]=b;
    A[1][0]=c;
    A[1][1]=d;
    I'm guessing this is how to do it, but I don't think it will print as number+number*i.
    Any ideas to do this? Any ideas to get a simple format for printing out complex numbers?

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You will need to write your own print function.
    Btw, C99 supports complex number!
    Code:
    #include <complex.h>
    
    int main(void)
    {
      complex double c = 3 + 4*I;
      complex double z = 10 + 5.0 * I;
      z *= c ;    // complex arith ! 
      z = cexp(z);
      printf("%g %g\n",creal(z),cimag(z) );
      return 0;
    }
    Last edited by Bayint Naung; 05-26-2010 at 09:44 PM.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Isla Vista
    Posts
    34
    like....
    Code:
    void printcmplxmtrx(A[2][2]){
    printf("A[1][1]=%lf+%lf*i", a.r, a.i);
    .
    .
    .
    }
    ?

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Isla Vista
    Posts
    34
    microsoft visual studio express 2010 doesn't have complex.h

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    The way you declared it, you'd need the word "struct" preceding the actual definitions of your variables. You should look up how to use "typedef".

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Isla Vista
    Posts
    34
    My struct is ok, atleast with just complex numbers. I can't figure out how to get my array's to hold complex numbers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. complex numbers in c++ (and c)
    By DavidP in forum C++ Programming
    Replies: 6
    Last Post: 05-09-2010, 12:33 PM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Very handy matrix functions - part 1
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 05-20-2004, 10:38 AM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM
  5. Need Help with using complex numbers
    By Rich in forum C++ Programming
    Replies: 2
    Last Post: 09-09-2001, 04:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21