Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dukebound85

macrumors Core
Original poster
Jul 17, 2005
19,160
4,152
5045 feet above sea level
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
 

dukebound85

macrumors Core
Original poster
Jul 17, 2005
19,160
4,152
5045 feet above sea level
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!
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
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.
 

pilotError

macrumors 68020
Apr 12, 2006
2,237
4
Long Island
code

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

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

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);
}
 

dukebound85

macrumors Core
Original poster
Jul 17, 2005
19,160
4,152
5045 feet above sea level
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.[/QUOTE]



thanks, yea i just included the meat of the program. i have it as a define statement along with all the include files. 

any idea on how to get it so instead of the numerical value of enum level is displayed, to actually get ninth, tenth and so on.

i have it now  like

Joe 0 3.9 

and i want Joe ninth 3.9? How do I access the enum vlaues I guess is what im asking

thanks for all the help
 

notjustjay

macrumors 603
Sep 19, 2003
6,056
167
Canada, eh?
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.
 

dukebound85

macrumors Core
Original poster
Jul 17, 2005
19,160
4,152
5045 feet above sea level
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;
}
 

dukebound85

macrumors Core
Original poster
Jul 17, 2005
19,160
4,152
5045 feet above sea level
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
 

notjustjay

macrumors 603
Sep 19, 2003
6,056
167
Canada, eh?
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.
 

savar

macrumors 68000
Jun 6, 2003
1,950
0
District of Columbia
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

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.
 

notjustjay

macrumors 603
Sep 19, 2003
6,056
167
Canada, eh?
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.

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 :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.