Thread: dynamic array of structs and accessing members of a struct

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    7

    dynamic array of structs and accessing members of a struct

    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.
    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;
    
    
    }
    I sear to god this isn't homework . . . . . .

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If you want to access all allocated structs, have your function receive a pointer to struct. Then you can move through the structs as if it were an array.

    A shorthand way of accessing an element via a pointer to struct:

    Code:
    comp->Re = value;
    Don't cast the return value of malloc: FAQ > Casting malloc - Cprogramming.com

    f2() and f3() are declared as returning float, but you're returning double. I would suggest just using double instead of float overall.

    Quote Originally Posted by Default99 View Post
    I sear to god this isn't homework . . . . . .
    Homework questions are fine as long as the user shows effort and does not simply ask for a handout.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    7
    thanks a lot man ,really helped me out. One more question though: is there a way i can print the number N which shows the position of that struct in the array? (the struct that describes the number with the largest magnitude)

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Yes, just keep track of the largest value while looping through the structs. If a larger magnitude is found, save this value (for subsequent comparisons) along with the corresponding value of N.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    7
    Hmm... I'm definitely doing something wrong here, The f3 function is supposed to return the index of the largest magnitude out of the array of magnitudes ,but when i enter a random N at the beginning it always gives back the N i entered. Also , am I giving the f3 function all the elements of the struct array ( comp ) or just the 1st one???
    Here's the modifications i've made:
    Code:
    int f3(struct Complex comp,unsigned int N)   // Finds largest magnitude, returns index{
        double m[N];
        int i;
        for (i=0; i<N; i++)
        {
            m[i] = f2(comp);        // this is where it needs something else i think
            if(m[0] < m[i])
                m[0] = m[i];
        }
    
    
        return i;
    }
    int main ()
    {
        unsigned int N,i;
        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 = (double)rand()/RAND_MAX*2.0-1.0;   // Randomly generating the real and imaginary parts (from -1 to 1)
           (*comp).Im = (double)rand()/RAND_MAX*2.0-1.0;      
       i = f3(*comp,N);      // Every time im using comp in a function does it take only its first element or all its elements?
       printf("\n\nThe complex number with the largest magnitude :\n");
        f1(*comp);   
        printf("\nIts assigned number in the array:\n  N = %d",i);
        free(comp);
    
    
    return 0;
    
    
    }
    Thanks for your time

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Start with a simple practice program to test passing a struct pointer to a function and traversing the list. Something like this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct stest
    {
        int x;
    };
    
    void stest_load(struct stest *st, int n);
    void stest_print(struct stest *st, int n);
    
    int main(void)
    {
        struct stest *s;
        int n = 10;
    
        if((s = malloc( sizeof(*s) * n)) == NULL)
            return 1;
    
        stest_load(s, n);
        stest_print(s, n);
    
        free(s);
        return 0;
    }
    
    void stest_load(struct stest *st, int n)
    {
        int i;
    
        for(i = 0; i < n; i++)
            st[i].x = i + 1;
    }
    
    void stest_print(struct stest *st, int n)
    {
        int i;
    
        for(i = 0; i < n; i++)
            printf("%d\n", st[i].x);
    }
    You don't need the "double" array, just a single variable that holds the largest current value (and another variable if you want to track the corresponding index).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to Qsort an array of structs by one of its members?
    By Vespasian in forum C++ Programming
    Replies: 3
    Last Post: 03-31-2013, 01:36 PM
  2. Accessing array members
    By kkk in forum C Programming
    Replies: 1
    Last Post: 06-05-2011, 01:56 PM
  3. Having trouble accessing members of a struct
    By Swarvy in forum C++ Programming
    Replies: 5
    Last Post: 10-03-2009, 08:47 AM
  4. accessing members inside of a private struct
    By MyglyMP2 in forum C++ Programming
    Replies: 9
    Last Post: 04-26-2007, 10:06 PM
  5. accessing members in nested structs
    By amidstTheAshes in forum C Programming
    Replies: 2
    Last Post: 03-23-2005, 02:00 PM

Tags for this Thread