Thread: What is wrong with my code? using structs and fprintf

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    15

    What is wrong with my code? using structs and fprintf

    I am working on a function for my project that formats the output of complex(imaginary) numbers, but when I run it, its not being formatted as i thought it would be.
    the struct being used is:

    Code:
    typedef struct {
      double re;
      double im;
    } complex;
    And I am unable to change the type of re and im, in the struct
    the function i have written for this so far is:

    Code:
    void output_complex(FILE *outf,complex z)
    {
       if(z.im == 0)
       {
          fprintf(outf,"%f", z.re);
       }
       if(z.re == 0)
       {
          if(z.im == 1)
          {
    	 fprintf(outf,"i"); 
          }
          fprintf(outf,"%fi", z.im);
       }
        if(z.im == 1)
       {
          fprintf(outf,"%f + i", z.re);
       }
       if(z.im < 0)
       {
          if(z.im == -1)
          {
    	 fprintf(outf,"%f - i", z.re);
          }
          z.im *= -1;  
          fprintf(outf,"%f - %fi", z.re, z.im);
       }
       fprintf(outf,"%f + %fi", z.re, z.im);
    }
    and there is this program that checks to see if the expected output is the same as the output of the function and this is what I got when I ran it for this function:
    Output Function Errors:
    expected: 1 + 2i
    actual: 1.000000 + 2.000000i
    expected: 3
    actual: 3.0000003.000000 + 0.000000i
    expected: 4i
    actual: 4.000000i0.000000 + 4.000000
    expected: i
    actual:
    expected: -2 + i
    actual: i1.000000i0.000000 + i0.0000
    expected: 4 - 2i
    actual: 0 + 1.000000i
    expected: 3 + 2i
    actual: -2.000000 + i-2.000000 + 1.0
    expected: 2 - i
    actual: 0000i

    what am I doing wrong?

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I don't really understand what it is you are trying to do. Why don't you just print the real and imaginary part.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    She's trying to do that, but having trouble organizing all here different cases (no real part, no imaginary part, etc).
    Last edited by anduril462; 04-06-2011 at 05:55 PM.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Yeah, I noticed that but I posted first.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Subsonics View Post
    Yeah, I noticed that but I posted first.
    $!@%#@...so you did. <whine>Fine, I'll move my post...</whine>

    Quote Originally Posted by ashleyd View Post
    what am I doing wrong?
    Honestly, I'd say you wrote a bunch of code for a problem and solution you don't fully understand. You need to start with a clear understanding of the problem and it's solution. Then, write down the solution on paper, in plain English. Work though your paper solution with these test cases, revising if necessary. Once that's done, start coding. I don't mean to sound insulting here, but are you familiar with using else and else if? Your solution will no doubt require at least one, if not both of those constructs (you can read up on them here and Google for more info).

    It might help to break the problem up into some logical "chunks". Here are some questions to get you going in the right direction:
    • When do I print the real part?
    • When do I print a + or -?
    • When do I print the imaginary part?
    • When do I print a number in the imaginary part? Should this number have an 'i' after it?
    • When do I print just an 'i'?

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    Yeah this is a duplicate by mistake, caused by slow page loading
    so any ideas of what i can do to fix this?
    i am trying to print out an equation, in the form a + bi

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Did you read the link I provided (red text in my previous post, #5)? You need to think through the solution. What answers did you come up with to the questions in my previous post? I'd feel a lot more comfortable giving you a huge hint if I thought you were making a sincere effort to figure this out.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    Sorry about that, I didn't read your last post untill after I posted my last post
    I did read the link you provided me with, and I did edit my code
    my new edited code is:
    Code:
     void output_complex(FILE *outf,complex z)
    {
      if (z.im == 0)
      {
        fprintf(outf,"%lg",z.re);
      }
      else if (z.re == 0)
      {
        if (z.im == 1)
        {
          fprintf(outf,"i");
        }
        else if (z.im == -1)
        {
          fprintf(outf,"-i");
        }
        else
        {
          fprintf(outf,"%lgi",z.im);
        }
      }
      if (z.im == 1)
      {
        fprintf(outf,"%lg + i", z.re);
      }
      if (z.im < 0)
      {
        if (z.im == -1)
        {
          fprintf(outf,"%lg - i", z.re);
        }
        z.im *= -1;
        fprintf(outf,"%lg - %lgi", z.re, z.im);
      }
      fprintf(outf,"%lg + %lgi", z.re, z.im);
    }
    and when I ran this new code i got:
    Output Function Errors:
    expected: 3
    actual: 33 + 0i
    expected: 4i
    actual: 4i0 + 4i
    expected: i
    actual: i0 + i0 + 1i
    expected: -2 + i
    actual: -2 + i-2 + 1i
    expected: 4 - 2i
    actual: 4 - 2i4 + 2i
    expected: 2 - i
    actual: 2 - i2 - 1i2 + 1i
    Last edited by ashleyd; 04-07-2011 at 07:07 AM.

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    Thanks for the advice, I was able to get it working by putting a return; statement after each fprintf statement

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is wrong with my code? using structs and fprintf
    By ashleyd in forum C Programming
    Replies: 2
    Last Post: 04-06-2011, 06:02 PM
  2. problem with printf in array of structs...
    By Huskar in forum C Programming
    Replies: 2
    Last Post: 03-29-2009, 10:13 PM