Help with MultiDimensional Array (I think)

This is a discussion on Help with MultiDimensional Array (I think) within the C Programming forums, part of the General Programming Boards category; I am working on a Binary Heap implementation of a Priority Queue. So far, I have the queue working for ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    7

    Smile Help with MultiDimensional Array (I think)

    I am working on a Binary Heap implementation of a Priority Queue. So far, I have the queue working for a single element. Here is some of the code:
    Code:
    [...headers and constants...]
    
    struct HeapStruct {
      int Capacity;
      int Size;
      ElementType *Elements;
    };
    
    PriorityQueue Initialize(int MaxElements) {
      PriorityQueue H;
    
      if (MaxElements < MinPQSize)
        printf("Priority queue size is too small.\n");
    
      H = malloc(sizeof ( struct HeapStruct));
      if (H == NULL)    printf("Out of space!\n");
    
      /* Allocate the array plus one extra for sentinel */
      H->Elements = malloc((MaxElements + 1) * sizeof ( ElementType));
      if (H->Elements == NULL)
        printf("Out of space!\n");
    
      H->Capacity = MaxElements;
      H->Size = 0;
      H->Elements[ 0 ] = MinData;
    
      return H;
    }
     
    void Insert(ElementType X, PriorityQueue H) {
      int i;
            
      if (IsFull(H)) {
        printf("Priority queue is full.\n");
        return;
      }
      for (i = ++H->Size; H->Elements[ i / 2 ] > X; i /= 2)
        H->Elements[ i ] = H->Elements[ i / 2 ];
      H->Elements[ i ] = X;
    }
    
    ElementType FindMin(PriorityQueue H) {
      if (!IsEmpty(H))
        return H->Elements[ 1 ];
      printf("Priority Queue is Empty.\n");
      return H->Elements[ 0 ];
    }
    
    [...the rest of the priority queue methods...]
    Now, I'm working on a discrete event simulation and I want my queue to store events with the following data:
    Code:
    int id; //To store a process id.
    int event_type; //Could be 1 or 0.
    double event_time; //Stores the time an event occurs.
    double run_time; //Stores the run time of an event.
    The priorities would be based on the event_time variable, so I would always dequeue the event with the smallest time.

    So, that's where I'm stuck. How should I go about modifying my original priority queue implementation to work with the above-mentioned variables and so that it prioritizes based on event_time?

    I feel like it's something very simple to do, but I just don't have enough experience in C to do so. I don't know if I can create an array of objects or something like what I could do in Java or Python. I hope I was clear enough.

    Help is much appreciated.


    PS: BTW, I know that ANSI-C is not object oriented at all. I was just saying that if it were Java, I would simply create an array of objects, but I don't know how to proceed here.
    Last edited by RommelTJ; 11-24-2009 at 09:17 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In C, objects are usually replaced with structs. So an array of your structs might be what you want.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    7
    Quote Originally Posted by Adak View Post
    In C, objects are usually replaced with structs. So an array of your structs might be what you want.
    Can you provide me with some sample code or a link where I can read on how to implement an array of a struct?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Sure, standby - horribly basic, but you get the idea. You define a struct with global scope, (generally a good practice), and then create the array of it, in the main function and send it around to where you want, from there. Of course, any function can then create an instance of that same struct, as needed.

    Code:
    //basics of using array of structs w/o dynamic memory allocation/pointers
    
    #include <stdio.h> 
    #include <math.h>
    
    #define studNum 4
    
    struct student {
      char name[25];
      float mark1;
      float mark2;
      float gpa;
      int c1;
      int c2;
      int no_stud;
    };
    
    float compute(struct student*, int i );
    void input_data(struct student*, int num_stud);
    void printIt(struct student*);
    
    static void force_fpf()  //just forces floating point linkage
    {
      float x, *y; /* Just declares two variables */
      y = &x;      /* Forces linkage of FP formats */
      x = *y;      /* Suppress warning message about x */
    }
    
    
    
    int main(void) {
      int i, num_stud;
      struct student stud[studNum];
    
      for(i = 1; i < studNum; ++i) 
         stud[i].no_stud = i; //stud[0] isn't used
    
      for(i = 1; i < studNum; ++i) {
        input_data(stud, i);
      }
    
      for(i = 1; i < studNum; ++i)
        stud[i].gpa = compute(stud, i);
    
      printIt(stud);
    
      printf("\n\n\t\t\t     press enter when ready");  
      while((i = getchar()) != '\n');    //holds the text window openi
      i = getchar();
      return 0;
    }
    void input_data(struct student *stud, int num)  {
      printf("\n Enter students name [24 letters]: ");
      scanf("%s", stud[num].name) ; //strings need no addresses
      printf("\n Enter students first mark [0-100]: ");
      scanf("%f", &stud[num].mark1); 
      printf("\n Enter students second mark [0-100]: ");
      scanf("%f", &stud[num].mark2); 
      printf("\n Enter students first course score [0-100]: ");
      scanf("%d", &stud[num].c1);
      printf("\n Enter students second course score [0-100]: ");
      scanf("%d", &stud[num].c2);
      printf("\n Enter students ID number [1-32000]: ");
      scanf("%d", &stud[num].no_stud);
    
    }
    float compute(struct student* stud, int i) {
      return ((stud[i].mark1 + stud[i].mark2 + stud[i].c1 + stud[i].c2)/4.00);
    }
    void printIt(struct student* stud) {
     int i;
     for(i = 1; i < studNum; ++i) {
       printf("\n Name: %s  mark1: %.2f  mark2: %.2f", stud[i].name, stud[i].mark1, stud[i].mark2);
       printf("\n GPA: %.2f  C1: %d  C2: %d Student No: %d", stud[i].gpa, stud[i].c1, stud[i].c2, stud[i].no_stud);
     }
    }
    Last edited by Adak; 11-24-2009 at 10:27 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct structname
    {
        ...members...
    };
    
    struct structname astruct;
    struct structname anarrayofstructs[ SIZE ];
    struct structname *aptrtostruct = & astruct;
    ...
    
    astruct.foo = x;
    anarrayofstructs[ y ].foo = z;
    aptrtostruct->foo = x;

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

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    7
    @Adak: Thanks. Your example showed me how to get it working.

    Should I post the working code? It's not very elegant, but I wonder if that would be the polite thing to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By bennywhere in forum C Programming
    Replies: 16
    Last Post: 10-20-2009, 09:00 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. Pointer to multidimensional array
    By ejohns85 in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2009, 11:17 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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