Advertisement
AnindyaBiswas

ComplexArray

Jul 27th, 2022
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include<math.h>
  4. struct ComplexNo
  5. {
  6.     double real;
  7.     double img;
  8. };
  9. typedef struct ComplexNo ComplexNo;
  10.  
  11. struct ComplexArray
  12. {
  13.     ComplexNo **arr;
  14.     int size;
  15. };
  16. typedef struct ComplexArray ComplexArray;
  17.  
  18. ComplexNo prod(ComplexNo z1, ComplexNo z2)
  19. {
  20.     ComplexNo prod;
  21.     prod.real = (z1.real*z2.real - z1.img*z2.img);
  22.     prod.img = (z1.real*z2.img + z1.img*z2.real);
  23.  
  24.     return prod;
  25. }
  26.  
  27. void Function(ComplexArray *a, ComplexNo *ref)
  28. {
  29.     ComplexNo var = *(a->arr[0]);
  30.     for(int i=1;i<a->size;i++)
  31.     {
  32.         var = prod(var, *(a->arr[i]));
  33.     }
  34.     printf("\nProd = %f + %fi", var.real, var.img);
  35.     printf("\nRef = %f + %fi", ref->real, ref->img);
  36.  
  37.     double prod_mag = sqrt(var.real*var.real + var.img*var.img);
  38.     double ref_mag = sqrt(ref->real*ref->real + ref->img*ref->img);
  39.    
  40.     printf("\n prod mag = %f\n ref mag = %f\n\n", prod_mag, ref_mag);
  41.  
  42.     if(fabs(prod_mag-ref_mag) < 0.001)
  43.         printf("Close Enough");
  44.  
  45.     else if(prod_mag-ref_mag > 0.001)
  46.         printf("PRoduct greater");
  47.  
  48.     else
  49.         printf("Ref greater");
  50.    
  51. }
  52.  
  53. void main()
  54. {
  55.     ComplexArray a;
  56.     a.size = 3;
  57.     a.arr = (ComplexNo**)malloc(sizeof(ComplexNo*)*a.size);
  58.     ComplexNo ref;
  59.     ref.img = 5;
  60.     ref.real = 4;
  61.     for(int i=0;i<a.size;i++)
  62.     {
  63.         a.arr[i] = (ComplexNo*)malloc(sizeof(ComplexNo));
  64.         a.arr[i]->real = i;
  65.         a.arr[i]->img = i+1;
  66.     }
  67.  
  68.       for(int i=0;i<3;i++)
  69.     {
  70.         printf("%lf + %lfi\n", a.arr[i]->real, a.arr[i]->img);
  71.     }
  72.  
  73.     Function(&a, &ref);
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement