issues with enums

Discussion in 'Mac Programming' started by dukebound85, Nov 14, 2006.

  1. dukebound85 macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #1
    hi all,

    i am having issues with enums so hopefully you guys can help me out

    for example

    i have

    enums verts {id, year, grade};
    enum level {ninth, tenth, eleventh, twelth};

    typedef struct
    {
    char id[SIZE];
    int schoolyear;
    float grades;
    } stat;


    int main()
    {
    stat biology[BIOLOGYSIZE]=
    {
    {"joe", ninth, 3.9}
    ,{"erica", twelth, 4.0}
    ,("sam", tenth, 2.0}
    };

    printstat(biology BIOLOGYSIZE);
    }


    I am having a hard time wrinting the printstat function. I just want to be able to print out the array. any ideas?

    thanks for the help
     
  2. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
  3. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #3
    i have the function prototype as this

    void printstat(struct stat, int);

    then have my function as

    void printstat(struct stat class, int number)
    {
    int i;
    for(i=0;i<number;i++)
    printf("%s %d %.1f\n",class.id,class.schoolyear,class.grades);

    }

    however it says i have incomplete parameters in mt function call when i try to compile it

    anyone have insight on this?

    many thanks!
     
  4. Soulstorm macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #4
    This thing will not compile
    Code:
    enum verts {id, year, grade};
    enum level {ninth, tenth, eleventh, twelth};
    
    typedef struct
    {
    	char id[SIZE];
    	int schoolyear;
    	float grades;
    } stat;
    
    
    int main()
    {
    	stat biology[BIOLOGYSIZE]=
    {
    {"joe", ninth, 3.9}
    	,{"erica", twelth, 4.0}
    	,("sam", tenth, 2.0}
    };
    
    printstat(biology BIOLOGYSIZE);
    }
    It has many syntax errors. You also do not specify some things that you should. For example, where is 'SIZE' defined? What are you trying to do with this exercice?

    Please be more specific as to what you are asking. Also, you might want to consider using
    Code:
     tags, as I have.
     
  5. pilotError macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #5
    code

    You had a bunch of syntax errors, but you have the general idea.

    That particular error is because you used an open parentheses '(' instead of a curly brace '{'
    just before the "sam" entry. You missed a comma in the printstat parameter list, etc.

    I built this on linux, shouldn't be too different on the Mac.

    #include <stdlib.h>

    enum verts {id, year, grade};
    enum level {ninth, tenth, eleventh, twelth};

    #define SIZE 10
    #define BIOLOGYSIZE 3

    typedef struct
    {
    char id[SIZE];
    int schoolyear;
    float grades;
    }STAT;


    void printstat(STAT *, int);

    int main()
    {
    STAT biology[BIOLOGYSIZE]=
    {
    {"joe", ninth, 3.9},
    {"erica", twelth, 4.0},
    {"sam", tenth, 2.0}
    };

    printstat(biology, BIOLOGYSIZE);
    }

    void printstat(STAT *class, int number)
    {
    int i;
    for(i=0;i<number;i++)
    printf("%s %d %.1f\n", class.id,
    class.schoolyear,
    class.grades);
    }
     
  6. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #6
     
  7. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #7
    Couple of ways to do this.

    Option 1:

    Code:
    typedef enum { APPLES = 0, PEACHES, PUMPKIN_PIE } EFoodType;
    
    char * GetEFoodTypeString( EFoodType x )
    {
      switch (x)
      {
        case APPLES:
          return "APPLES";
        case PEACHES:
          return "PEACHES";
        case PUMPKIN_PIE:
          return "PUMPKIN_PIE";
        default:
          return "UNKNOWN";
          break;
       }
    }
    
    Option 2:

    Code:
    typedef enum { APPLES = 0, PEACHES, PUMPKIN_PIE } EFoodType;
    
    char * EFoodTypeStrings[] = { "APPLES", "PEACHES", "PUMPKIN_PIE" };
    
    char * GetEFoodTypeString( EFoodType x )
    {
       return EFoodTypeStrings[x];
    }
    
    Hope that helps.
     
  8. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #8
    thanks that helps alot.

    another question if you dont mind. how would i go about searching the structure by the enum verts fields? trying to use bubble search algorithm

    in my sort function i have this so far but its not working too nicely

    void sort(stat class[],int number,int verts)
    {
    int i;
    int p;

    switch(verts)
    {
    for(p=0;p<number-1;p++)
    switch(columns)
    {
    case id:
    for(i=number-1;i>p;i--)
    {
    if(class[i-1].id>class.id)
    switcheroo(class[],i-1,i);
    }
    }
    break;
    }
    }



    void switcheroo(int class[],int f, int g)
    {
    int var;
    var=class[f];
    class[f]=class[g];
    class[g]=var;
    }
     
  9. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #9
    using switch as a function name is a bad idea, since it's already a language keyword.
     
  10. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #10
    thanks yea good call
    still isnt being happy though
     
  11. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #11
    anybody know how to do a sort on any of the 3 columns in the array struct? I am trying to do the bubble sort algorithm. As in sort by name, year in school and grade?

    many thanks, you all have been super nice with helping me out
     
  12. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #12
    You mean you want to sort by ALL THREE (name, then id, then grade)? Or just one of those three at any given time?

    Sorting by all three keys at once would definitely be trickier. The easiest way would be to define your own compare function, and use that instead of a standard compare using the > operator.

    For example, if you were sorting a set of simple data types, like integers or characters, somewhere in your sort code is a statement that's going to say if (a > b) { ... swap ... }

    Now suppose you've got a struct Student which contains two ints, representing age and grade. You want to sort by age first, and when the ages are the same, sort by grade.

    So you define a function

    Code:
    bool compareStudent( Student a, Student b)
    {
       // this function will return true if student a is greater than student b
    
       // if ages are the same, then sort by grade instead
       if (a.age == b.age)
          return (a.grade > b.grade);
       else 
          return (a.age > b.age);
    }
    
    Then in your sort code you can use if ( compareStudent(a,b) ) { ... swap ... }

    Hope that helps.
     
  13. savar macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #13
    Sure, just perform 3 bubble sorts in reverse order of your keys. So to sort on name,year, and grade, you'd bubble sort:

    1) grade
    2) year
    3) name

    This works because bubble sort preserves existing order when two values are equal. So you sort by name, then when you sort by year, any two people with the same year will stay in the same order -- and that order is "sorted by name". People in different years will still get moved around so that the years get put into their proper order.

    The main thing you haven't considered is how is your bubble sort function going to sort multiple types of data. For instance, your name is a string, but year is an int. You need to write what is known as polymorphic code if you want one sort function that can operate on multiple datatypes.
     
  14. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #14
    Well, if he writes a compare function like the one I suggested a few posts up, that would take care of the different data types. In this case it works because (I think) the data to be sorted on is already nicely aggregated in a struct. The compare function can use multiple if statements as needed and can do whatever is needed to handle the different data types. You could even mix operators, which is what I had to do recently when I had to sort a bunch of elements by percentage (percentage in descending order, best % first), EXCEPT if they had a manual override ranking (#1, #2, ...) which had to be sorted in ascending order.

    The next step up would be to overload the < > operators for the struct type to make the sort code cleaner.

    For bonus points, store the data in a STL vector<struct &>, create a sort functor, and use the STL sort function :)
     

Share This Page