How can i access some members of a struct, which is contained in a dynamic array of structs?
For example : From a N sized dynamic array of structs which contain the real and imaginary parts of a complex number ,find the number with the largest magnitude and print its real and imaginary parts
My problem is : i'm not sure if im using the f1 f2 f3 functions for all the N structs or just the first one.
I dont know how to access the parts of the number with the largest magnitude in main.
I sear to god this isn't homework . . . . . .Code:#include <stdio.h> #include <math.h> #include <stdlib.h> #include <time.h> struct Complex { float Re; float Im; }; void f1(struct Complex comp) // Prints 'Imaginary' and Real parts { printf("Your complex number is:\n\n %f + i x %f", comp.Re, comp.Im); } float f2(struct Complex comp) // Finds magnitude { double z; z = sqrt((comp.Re*comp.Re)+(comp.Im*comp.Im)); return z; } float f3(struct Complex comp,unsigned int N) // Finds largest magnitude { double z[N]; int i; for (i=0; i<N; i++) { z[i] = f2(comp); if(z[0] < z[i]) z[0] = z[i]; } return z[0]; } int main () { unsigned int N; float magnitude; struct Complex *comp; printf("Enter N:\n"); scanf("%d",&N); comp = (struct Complex*)malloc(N*sizeof(struct Complex)); if (comp == NULL) { printf("Error! Out of memory"); return 1; } srand ( time ( NULL)); (*comp).Re = (float)rand()/RAND_MAX*2.0-1.0; // Randomly generating the real and imaginary parts (from -1 to 1) (*comp).Im = (float)rand()/RAND_MAX*2.0-1.0; magnitude = f3(*comp,N); printf("\n\nLargest complex number magnitude:\n %f",magnitude); return 0; }