# Complex number, imaginary part usage question

• (3 Pages)
• 1
• 2
• 3

## 31 Replies - 3995 Views - Last Post: 03 June 2012 - 06:47 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=270590&amp;s=effae493d5e78b319c4989082b4acf4c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 erkant

Reputation: 2
• Posts: 108
• Joined: 26-October 10

# Complex number, imaginary part usage question

Posted 12 March 2012 - 01:41 PM

I have no previous experience in using the complex.h library, and using the imaginary part. Therefore, I need some help with it. In the below code for some of the index elements I want to add the imaginary part "j". Can someone help me with it?

```#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <complex.h>

#undef I
#define j _Imaginary_I

void rbits(short* buf, int nbits)
{
int i;

srand(time(NULL));

for(i = 0; i < nbits; i++)
*(buf + i) = (rand() % 2);
}

int main()
{
int bits, i;

short* input;

input = malloc(sizeof(int) * bits);

if(input == NULL)
{
fprintf(stderr, "Couldn't allocate that much memory!");
return 1;
}

printf("Please enter the number of bits: ");
scanf("%d", &bits);

rbits(input, bits);

for(i = 0; i < bits; i += 2)
{
if(input[i] == 0 && input[i+1] == 0)
{
input[i] = -1;
input[i+1] = -j;
}
else if(input[i] == 0 && input[i+1] == 1)
{
input[i] = -1;
input[i+1] = +j;
}
else if(input[i] == 1 && input[i+1] == 0)
{
input[i] = 1;
input[i+1] = -j;
}
else
{
input[i] = 1;
input[i+1] = +j;
}
}

for(i = 0; i < bits; i++)
printf("%d ", input[i]);
printf("\n");

return 0;
}

```

My method is not working, while when I run it I get the following errors:

C:\Users\Erkan\Desktop\asdsada.c|44|error: '_Imaginary_I' undeclared (first use in this function)|
C:\Users\Erkan\Desktop\asdsada.c|44|note: each undeclared identifier is reported only once for each function it appears in|
||=== Build finished: 2 errors, 0 warnings ===|

If someone can offer me a better and working method for imaginary part, I would be glad.

This post has been edited by erkant: 12 March 2012 - 01:46 PM

Is This A Good Question/Topic? 0

## Replies To: Complex number, imaginary part usage question

### #2 simeesta

Reputation: 221
• Posts: 594
• Joined: 04-August 09

## Re: Complex number, imaginary part usage question

Posted 12 March 2012 - 02:30 PM

The first thing I noticed was you're using bits on line 26 before you assign it a value. The error appears to be you using j instead of I as an imaginary type. Also why does input have type short and not complex?

Heres an example of using complex numbers in C.

### #3 vividexstance

• D.I.C Lover

Reputation: 783
• Posts: 2,767
• Joined: 31-December 10

## Re: Complex number, imaginary part usage question

Posted 12 March 2012 - 02:35 PM

This was all covered in a different thread, the OP just didn't make the changes and reposted.

### #4 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: Complex number, imaginary part usage question

Posted 12 March 2012 - 03:09 PM

Sticking to the complex number aspect of the question, use of complex.h is explained pretty clearly here.

Don't undefine I unless you have a specific need to use the uppercase letter 'I' for some other purpose in your program. Otherwise, you want I because that's a quick way to get the constant value 0 + 1i.

Also, doing this
```#define j _Imaginary_I
```

doesn't do anything but replace the macro _Imaginary_I (which is equivalent to the macro I) that's provided in complex.h with your own macro j. What good is that? You've just provided a non-standard name for the complex type. You still have to declare a variable in your program if you want to use it.

Here's a quick example of how to use the complex numbers (notice, no #define):

```#include <stdio.h>
#include <complex.h>

int main() {
complex double cplx = 0I;
complex double cplx2 = -5I + 2.4;
complex double cplx3 = 4.0 + 2.0i;
printf( "%f %c %fi\n", creal(cplx),   cimag(cplx2) >= 0 ? '+' : '-', \
cimag(cplx2) >= 0? cimag(cplx2) : -cimag(cplx2));
printf( "%f %c %fi\n", creal(cplx2),  cimag(cplx2) >= 0 ? '+' : '-', \
cimag(cplx2) >= 0? cimag(cplx2) : -cimag(cplx2));
printf( "%f %c %fi\n", creal(cplx3),   cimag(cplx3) >= 0 ? '+' : '-', \
cimag(cplx3) >= 0? cimag(cplx3) : -cimag(cplx3));
return 0;
}

```

Notice the 5I. That's just a quick and easy way of getting the constant 0 + 5i.

And isn't that cute -- it even allows implied multiplication (5I).

(Don't worry too much about the ternary expressions in my printf. That's there just because I didn't want to print something as ugly as 2.4 + -5i)

This post has been edited by r.stiltskin: 12 March 2012 - 06:48 PM
Reason for edit:: See next post.

### #5 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: Complex number, imaginary part usage question

Posted 12 March 2012 - 05:37 PM

Cleaned up some sloppiness in my previous post. I is 0 + 1i not 0 + 0i

Also, there's nothing in the standard to say that an imaginary number is automatically initialized, so I corrected line 5 to initialize it to 0 + 0i. And I cleaned up the ternary expressions so 0i won't become -0i. And finally, added another way to initialize a complex variable on line 7.

### #6 erkant

Reputation: 2
• Posts: 108
• Joined: 26-October 10

## Re: Complex number, imaginary part usage question

Posted 13 March 2012 - 09:03 AM

Thanks for everyone for useful information. You were very helpful.

### #7 vividexstance

• D.I.C Lover

Reputation: 783
• Posts: 2,767
• Joined: 31-December 10

## Re: Complex number, imaginary part usage question

Posted 13 March 2012 - 10:06 AM

Erkant, alot of what is wrong with your code is the same as whats wrong in the code you posted in another thread. There were a few posts in that thread that explained what was wrong. Before you make another thread, maybe check on threads you've already created.

Also, what wasn't helpful about my post above? I simply pointed out that if you checked your other thread, you would've seen some posts telling you what was wrong and how to fix it.

### #8 erkant

Reputation: 2
• Posts: 108
• Joined: 26-October 10

## Re: Complex number, imaginary part usage question

Posted 13 March 2012 - 10:48 AM

I regularly check the threads that I open. I also feel sometimes like I'm spamming, but I'm highly into programming especially C language and I want to learn a lot of thing about it, and code as much as I can. Therefore, when I don't know something I try to look up it online, if I can't find it I post it here. I always try to post other questions. In my last thread I had problem with memory allocation, some pointer and arrays. But this one was different, maybe code was similar but my problem was completely different, I needed help about complex numbers in C. Thanks once more to everyone who helped me.

### #9 vividexstance

• D.I.C Lover

Reputation: 783
• Posts: 2,767
• Joined: 31-December 10

## Re: Complex number, imaginary part usage question

Posted 13 March 2012 - 11:06 AM

Yes but your code is still wrong. I told you how to fix it, but you didn't and then posted similar code (with the same things wrong with it) and ask a different question. If you want to learn C and are posting here for advice, then when people do make suggestions, you don't make the proper changes to your code. IMO, if you are serious about learning C, you would have made those changes and the code you posted above would've only had a problem with complex numbers.

Like I said in the other thread, your program might compile fine, but the input/output arrays are being allocated using an int variable that hasn't been initialized yet. You need to move the allocation part of your code to after you ask the user for the number of bits. Then use that number to allocate the input/output arrays.

I also mentioned something about the random number generator in the other thread. You seed it every time the rbits() function is called, this is wrong, you only want to seed it once in main(). The call to srand(time(NULL)); should me moved to the beginning of main(). Last thing, you should make sure you free the memory that you dynamically allocated using malloc because you can wind up with memory leaks. Again, this was all covered in the other thread you posted.

This post has been edited by vividexstance: 13 March 2012 - 11:09 AM

### #10 erkant

Reputation: 2
• Posts: 108
• Joined: 26-October 10

## Re: Complex number, imaginary part usage question

Posted 13 March 2012 - 12:17 PM

This post has been edited by erkant: 13 March 2012 - 12:18 PM

### #11 vividexstance

• D.I.C Lover

Reputation: 783
• Posts: 2,767
• Joined: 31-December 10

## Re: Complex number, imaginary part usage question

Posted 13 March 2012 - 12:24 PM

Quote

In my original code I have already asked the user to enter the number of bits, and I have scanned it, and used it for allocating the arrays.

You do not use that number scanned from the user to allocate your memory. My point was that you still have things wrong with your code and you're trying to add more to it. Why not first fix what's wrong with your code and then add things? Here is what I'm talking about where you allocate the memory with malloc() before you ask for the number of bits:
```int main()
{
int bits, i;

short* input;

input = malloc(sizeof(int) * bits);

if(input == NULL)
{
fprintf(stderr, "Couldn't allocate that much memory!");
return 1;
}

printf("Please enter the number of bits: ");
scanf("%d", &bits);

rbits(input, bits);
```

I bet your program runs fine, but that's because 'bits' most likely contains a very large garbage number. So you're declaring a huge array when you really want an array to hold a certain number of bits. Then you don't even free the memory for that huge array. To see what I'm talking about, print out bits in between where you declare it and where you use it in the call to malloc.

### #12 erkant

Reputation: 2
• Posts: 108
• Joined: 26-October 10

## Re: Complex number, imaginary part usage question

Posted 13 March 2012 - 12:28 PM

erkant, on 13 March 2012 - 12:17 PM, said:

In my original code, which I haven't posted here I have already asked the user to enter the number of bits, and I have scanned it, and used it for allocating memory for arrays (so don't worry about it).

If you read it correctly, I have written "which I haven't posted here". I just posted a short snapshot here, just regarding my question. The code and bits are all fine and working of my computer.

I already have this in my original code:

```    printf("Please enter the number of bits: ");
scanf("%d", &bits);

```

This post has been edited by erkant: 13 March 2012 - 12:30 PM

### #13 vividexstance

• D.I.C Lover

Reputation: 783
• Posts: 2,767
• Joined: 31-December 10

## Re: Complex number, imaginary part usage question

Posted 13 March 2012 - 12:33 PM

Here is a program to test what I was talking about:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
srand(time(NULL));		/* Seed the random number generator ONLY ONCE! */

int bits;
short *input, *output;

printf("bits == %d\n", bits);
printf("sizeof(short) * bits == %d\n", sizeof(short) * bits);

input = malloc(sizeof(short) * bits);

if(input == NULL)
{
fprintf(stderr, "Couldn't allocate that much memory!");
return 1;
}

output = malloc(sizeof(short) * bits);

if(output == NULL)
{
fprintf(stderr, "Couldn't allocate that much memory!");
return 1;
}

printf("Please enter the number of bits: ");
scanf("%d", &bits);

return 0;
}

```

And the output of the printf calls is:

Quote

./erkant
bits == 5983317
sizeof(short) * bits == 11966634
Please enter the number of bits:

On the second run the output was different:

Quote

bits == 14883925
sizeof(short) * bits == 29767850
Please enter the number of bits:

And that's because you're still using bits in malloc() before it has been initialized. Move the part where you input bits to be above the malloc call.

This post has been edited by vividexstance: 13 March 2012 - 12:35 PM

### #14 erkant

Reputation: 2
• Posts: 108
• Joined: 26-October 10

## Re: Complex number, imaginary part usage question

Posted 13 March 2012 - 12:39 PM

Dear vividexstance, thank you very much for all your information, I completely understand it, and I already knew it before. And thank you very much for all your efforts, but there is just a small part from my original code to see that I have gotten the value of bits before using it:

```int main(int argc, char *argv[])
{
short seed[8];
int bits;
char mode[5];

printf("How many bits do you want to transmit?: ");
scanf("%d", &bits);

short* input;
short* output;
short* coder_output;

input = malloc(sizeof(short) * bits);

output = malloc(sizeof(short) * bits);

coder_output = malloc(sizeof(short) * (bits * 2));

if(coder_output == NULL)
{
fprintf(stderr, "Couldn't allocate that much memory!");
return 1;
}

srand(time(NULL));
rbits(seed, 8);
rbits(input, bits);

printf("Which modulation type to you want to use? (type BPSK or QPSK): ");
scanf("%s", mode);

// and it continues...

```

This post has been edited by erkant: 13 March 2012 - 12:40 PM

### #15 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: Complex number, imaginary part usage question

Posted 13 March 2012 - 12:44 PM

@erkant: It must be obvious by now that after someone has taken the trouble to examine your code and offer suggestions to correct it, they are likely to feel disrespected if you repost the same or similar code without using or acknowledging those suggestions, even if the reason for the re-posting is essentially unrelated to the original question.

@vividexstance: I understand how irritating it is to see your efforts seemingly ignored, but I'm sure no disrespect was intended and, to be fair, the question posed in this thread was clearly stated and really did not relate to the previous topic. I, for one, didn't even look at the rest of the code since the question was exclusively about complex numbers.

And, to both of you, this thread has wandered well off its original topic so maybe a new thread would be in order.

This post has been edited by r.stiltskin: 13 March 2012 - 12:44 PM