Array of structs

This is a discussion on Array of structs within the C Programming forums, part of the General Programming Boards category; 4 1 5 2 -4 -3 9 8 2 The first integer, call it n, is the number of complex ...

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    Array of structs

    4
    1 5
    2 -4
    -3 9
    8 2

    The first integer, call it n, is the number of
    complex numbers to follow. The n complex numbers
    are then listed. Your program must dynamically
    allocate enough memory to store the array of n
    complex numbers. Complex numbers must be stored
    in a structure. (So you have an array of
    structures.) Then your program should loop through
    the array from end to front, and print out the
    complex numbers in it. E.g., your output should
    look like
    8 + 2i
    -3 + 9i
    2 + -4i
    1 + 5i
    on stdout.

    I have no idea how to allocate memory for something like this, if the stuff is inputted, how do u know how big the struct or array is to put the numbers in, can someone demonstrate the code for that part?

    Thanks

  2. #2
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    just to mention, that the info is being read from a file

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    struct test
    {
      int r, i;
    };
    
    /* Read n from the file */
    struct test *t = malloc ( n * sizeof *t );
    for ( i = 0; i < n; i++ ) {
      /* Fill the array up to t[n - 1] */
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct
    {
     int x;
     int y;
    } COMPLEX_NUMBER;
    
    int main (void)
    {
     int i, counter;
     FILE *fp;
     COMPLEX_NUMBER *pCN;
    
     fp = fopen ("datafile", "r");
    
     fscanf (fp, "%d", &counter);
    
     pCN = malloc (counter * sizeof (COMPLEX_NUMBER));
    
     for (i = 0; i < counter; i++)
      fscanf (fp, "%d%d", &(pCN + i)->x, &(pCN + i)->y);
    
     for (i = counter - 1; i >= 0; i--)
      printf ("%5d%5d\n", (pCN + i)->x, (pCN + i)->y);
    
     fclose (fp);
    
     return 0;
    }
    Last edited by PutoAmo; 04-18-2002 at 12:30 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by PutoAmo
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct
    {
     int x;
     int y;
    } COMPLEX_NUMBER;
    
    int main (void)
    {
     int i, counter;
     FILE *fp;
     COMPLEX_NUMBER *pCN;
    
     fp = fopen ("datafile", "r");
    
     fscanf (fp, "%d", &counter);
    
     pCN = malloc (counter * sizeof (COMPLEX_NUMBER));
    
     for (i = 0; i < counter; i++)
      fscanf (fp, "%d%d", &(pCN + i)->x, &(pCN + i)->y);
    
     for (i = counter - 1; i >= 0; i--)
      printf ("%5d%5d\n", (pCN + i)->x, (pCN + i)->y);
    
     fclose (fp);
    
     return 0;
    }

    I have never understood why the hell people do everyone's entire homework project for them... The only time I do this, the code is an "alternate" way of doing it, that either (a) they wouldn't understand, (b) their teacher wouldn't accept the code for a grade, (c) the teacher would know that there's no way in hell the student wrote this code...

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Replies: 41
    Last Post: 07-04-2004, 04:23 PM
  4. array of structs initialization - PLZ help...
    By Vanya in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2002, 08:10 PM
  5. Pointer to Array of Structs
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-06-2002, 08:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21