1

I'm new at C and I am working on a program to find the difference, product, and division of complex numbers. At the difference, it works but at the product and the division, the answer is 0+0i.

The code is:


    #include <stdio.h>

struct complex {
float re; // The real part
float im; // The imaginary part
}complex1, complex2, sum;

// to find the difference
struct complex diff(struct complex z1, struct complex z2) {
    // difference 
sum.re = complex1.re - complex2.re;
sum.im = complex1.im - complex2.im;
//show the difference
printf("The difference is (%f, %fi)\n", sum.re, sum.im);
return sum;
}

// to find the product
struct complex product(struct complex z1, struct complex z2) {
    // product 
int x, y, a, b;
x = complex1.re * complex2.re;
y = complex1.im * complex2.im;
a = complex1.re * complex2.im;
b = complex2.re * complex1.im;
//show the product
printf("The product is (%f, %fi)\n", (x - y), (a + b));
return sum;
}

// to find the division
struct complex division(struct complex z1, struct complex z2) {
    // division 
int x, y, a, b;
x = (complex1.re * complex2.re) + (complex1.im * complex2.im);
y = (complex2.re * complex2.re) + (complex2.im * complex2.im);
a = (complex2.re * complex1.im) - (complex1.re * complex2.im); 
b = (complex2.re * complex2.re) + (complex2.im * complex2.im);
//show the division
printf("The division is (%f, %fi)\n", (x / y), (a / b));
return sum;
}


int main(){
printf( "Enter the first complex number \n");
printf( "Enter the real part " ); scanf( "%f", &complex1.re );
printf( "Enter the imaginary part " ); scanf( "%f", &complex1.im );

printf( "Enter the second complex number \n");
printf( "Enter the real part " ); scanf( "%f", &complex2.re);
printf( "Enter the imaginary part " ); scanf( "%f", &complex2.im );

diff(complex1, complex2);
product(complex1, complex2);
division(complex1, complex2);
printf("\nThanks for using!!!!");
return 0;
}

The result that I always get

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
GamerToonz
  • 51
  • 4
  • Please don't post pictures of text, post text as text. – Jabberwocky Dec 31 '20 at 13:00
  • 3
    Your local temporary variables are `int`s. Arithmetic operations on them will produce `int`s. You try to print them with the `%f` format, which is for `float` and `double`. – M Oehm Dec 31 '20 at 13:02
  • Probably `int x, y, a, b;` -> `float x, y, a, b;` – Jabberwocky Dec 31 '20 at 13:02
  • 4
    Consider using [C99 ``](http://port70.net/~nsz/c/c99/n1256.html#7.3) or even [C11's](http://port70.net/~nsz/c/c11/n1570.html#7.3) – pmg Dec 31 '20 at 13:02
  • 1
    (In general, your code would benefit from some reorganization: Have the functions `diff`, `product` and `division` return complex numbers and provide a separate function that prints complex numbers. At the moment, `product` and `division` return the global `sum`, which hasn't been changed by these functions.) – M Oehm Dec 31 '20 at 13:07
  • The functions ignore their parameters. That's not good. – Jonathan Leffler Dec 31 '20 at 13:52
  • 2
    I don't understand why it is so common to see examples that do this, and I don't understand why people that are learning C go straight to `scanf`. Don't take your parameters from the input stream, take them as arguments to main. Avoid `scanf` for at least the next 5 years. You will learn the language much more proficiently if you do not waste your time trying to understand the intricacies of the scanf format language. https://stackoverflow.com/questions/2430303/disadvantages-of-scanf http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html – William Pursell Dec 31 '20 at 14:38

0 Answers0