Advertisement
Aminpro

[ECP 1016]Tutorial 10, Question 2(All)

Sep 2nd, 2012
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.29 KB | None | 0 0
  1. //https://www.facebook.com/AminproPastebin
  2.  
  3. #include <stdio.h>
  4.  
  5. struct Complex{
  6.     float real;
  7.     float imag;
  8. };
  9.  
  10. //Function Prototypes
  11. struct Complex complex_add(struct Complex, struct Complex);
  12. void complex_mul(struct Complex, struct Complex, struct Complex *);
  13.  
  14.  
  15. int main(void){
  16. struct Complex z1, z2, add, mul;
  17.  
  18.  
  19. //user input
  20. printf("First complex number...\n");
  21. printf("Enter the real part: \n");
  22. scanf("%f", &z1.real);
  23. printf("Enter the imaginary part: \n");
  24. scanf("%f", &z1.imag);
  25.  
  26. printf("Second complex number...\n");
  27. printf("Enter the real part: \n");
  28. scanf("%f", &z2.real);
  29. printf("Enter the imaginary part: \n");
  30. scanf("%f", &z2.imag);
  31.  
  32. //calling function
  33. add = complex_add(z1, z2);
  34. complex_mul(z1, z2, &mul);
  35.  
  36. //print
  37. printf("addition result: %.2f%+.2fi\n", add.real, add.imag);
  38. printf("Multiplication result: %.2f%+.2fi\n", mul.real, mul.imag);
  39.  
  40.  
  41. return 0;
  42.     }
  43.  
  44.  
  45. //function definition
  46.  
  47. struct Complex complex_add(struct Complex n1, struct Complex n2){
  48. struct Complex result;
  49.  
  50. result.real = n1.real + n2.real;
  51. result.imag = n1.imag + n2.imag;
  52. return result;
  53. }
  54.  
  55. void complex_mul(struct Complex n1, struct Complex n2,
  56. struct Complex *result){
  57.  
  58. result -> real = (n1.real*n2.real) - (n1.imag*n2.imag);
  59. result -> imag = (n1.real*n2.imag) + (n1.imag*n2.real);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement