0

Without using the complex.h header file, suppose I want to create my own header file where I will define a variable argument function,

  1. taking value 0 if I did not insert any value from the keyboard,
  2. becoming a real number if I input only one value,
  3. and a complex number if I input two values.

How can such a complex number be implemented? I have been thinking about the "i" symbol for the imaginary part. How can it appear? Is there any nice way to write a complex number?

Also I need to define addition in the complex field. How can that be done?

2 Answers2

4

You should use a structure to represent it:

struct complex{
   int real;
   int imaginary;
};

now you can create an instance:

struct complex num;

and set its fields:

num.real = 3; //real part is now set to 3
num.imaginary = 5; //imaginary part is now set to 5
Utkan Gezer
  • 2,901
  • 1
  • 12
  • 28
antonpuz
  • 2,947
  • 4
  • 21
  • 45
3

Why not create a struct with two fields for the Re and Im parts?

The i-notation is only a representation.

Then you can write functions that take two variables of the strcut type you created and return the added numbers as the same struct type.

  • Sorry but I am a beginner at C. By "two fields" do you mean two components? And, if so, how can the final representation happen so that the final answer also is a complex number? – Landon Carter Jan 06 '15 at 14:52
  • See Anton.P's answer. He described a struct with the two fields real and imaginary. Now you can create a function: complex function(complex a, complex b) { ... } – Ramon Hofer Jan 06 '15 at 15:33