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
    I'm not sure "interesting" is on-topic. It elicits opinion. – bitmask Jan 06 '15 at 14:48
  • 1
    See http://stackoverflow.com/a/9860772/35499 – Dean Jan 06 '15 at 14:53
  • I think I mentioned I cannot use the complex.h header file. The task is to CREATE my own header file. – Landon Carter Jan 06 '15 at 14:54
  • 3
    Could this be relevant? http://stackoverflow.com/questions/5093566 – Reti43 Jan 06 '15 at 14:56
  • I think this will be of immense help. Thank you Reti43 – Landon Carter Jan 06 '15 at 14:58
  • Just want do you mean by "variable argument function"? C does not have overloaded functions, and variadic functions require *your function code* to know how many arguments there are (example, *printf* knows from its format string), so you probably want 3 functions. Or one function taking a string, which it parses? – hyde Jan 19 '15 at 20:20

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
  • 3,009
  • 2
  • 16
  • 29
antonpuz
  • 3,256
  • 4
  • 25
  • 48
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