Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include<math.h>
- struct ComplexNo
- {
- double real;
- double img;
- };
- typedef struct ComplexNo ComplexNo;
- struct ComplexArray
- {
- ComplexNo **arr;
- int size;
- };
- typedef struct ComplexArray ComplexArray;
- ComplexNo prod(ComplexNo z1, ComplexNo z2)
- {
- ComplexNo prod;
- prod.real = (z1.real*z2.real - z1.img*z2.img);
- prod.img = (z1.real*z2.img + z1.img*z2.real);
- return prod;
- }
- void Function(ComplexArray *a, ComplexNo *ref)
- {
- ComplexNo var = *(a->arr[0]);
- for(int i=1;i<a->size;i++)
- {
- var = prod(var, *(a->arr[i]));
- }
- printf("\nProd = %f + %fi", var.real, var.img);
- printf("\nRef = %f + %fi", ref->real, ref->img);
- double prod_mag = sqrt(var.real*var.real + var.img*var.img);
- double ref_mag = sqrt(ref->real*ref->real + ref->img*ref->img);
- printf("\n prod mag = %f\n ref mag = %f\n\n", prod_mag, ref_mag);
- if(fabs(prod_mag-ref_mag) < 0.001)
- printf("Close Enough");
- else if(prod_mag-ref_mag > 0.001)
- printf("PRoduct greater");
- else
- printf("Ref greater");
- }
- void main()
- {
- ComplexArray a;
- a.size = 3;
- a.arr = (ComplexNo**)malloc(sizeof(ComplexNo*)*a.size);
- ComplexNo ref;
- ref.img = 5;
- ref.real = 4;
- for(int i=0;i<a.size;i++)
- {
- a.arr[i] = (ComplexNo*)malloc(sizeof(ComplexNo));
- a.arr[i]->real = i;
- a.arr[i]->img = i+1;
- }
- for(int i=0;i<3;i++)
- {
- printf("%lf + %lfi\n", a.arr[i]->real, a.arr[i]->img);
- }
- Function(&a, &ref);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement