Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting a structure 1

Status
Not open for further replies.

ecannizzo

Programmer
Sep 19, 2000
213
US
I'm trying to sort a structure, but I keep getting errors. Can anyone tell me what's wrong with me code? Thanks!

void sortindex(int index_c)
{
int i, hold, pass, swap;

for (pass = 1; pass <= index_c; pass++) {
swap = 0;
for (i = 0; i < index_c - pass; i++) {
if ((indxfrec_t) > (indxfrec_t[i + 1])) {
swap = 1;
hold = indxfrec_t;
indxfrec_t = indxfrec_t[i + 1];
indxfrec_t[i + 1] = hold;
}
}
if (!swap)
break;
}
for (i = 0; i < index_c; i++)
printf(&quot;\n%s\t%ld&quot;, indxfrec_t.partid, indxfrec_t.recoffset);
}
 
it is tough to read... can you repost and use the code tag?

(SEE Process TGML at bottom of page)
Matt
 
Code:
void sortindex(int index_c)
{
	int i, hold, pass, swap;

	for (pass = 1; pass <= index_c; pass++) {
		swap = 0;
		for (i = 0; i < index_c - pass; i++) {
			if ((indxfrec_t[i]) > (indxfrec_t[i + 1])) {
				swap = 1;
				hold = indxfrec_t[i];
				indxfrec_t[i] = indxfrec_t[i + 1];
				indxfrec_t[i + 1] = hold;
			}
		}
		if (!swap)
			break;
	}
	for (i = 0; i < index_c; i++)
		printf(&quot;\n%s\t%ld&quot;, indxfrec_t[i].partid, indxfrec_t[i].recoffset);
}
 
I think the problem lies in this line

for (i = 0; i < index_c - pass; i++)

with this, you will never see the last index.

Second indxfrec_t is a structure...
a.) did you overload the < operator for it?
b.) you need to write a unique swap routine for this because it is a structure. Instead of ints you should use the struct type.

c.) if the struct contains pointers you WILL definitely need to overload the = operator, otherwise you can use the default = operator and just swap em as you are doing with hold. Hold would become of type <struct type>

Lemme know if there were more things i missed.

Matt
 
I think you hit the nail on the head with your last couple of comments. This is one of the errors I am getting:

'>' struct does not define this operator...

I'm getting it for the equal sign too.

I don't know what you mean by overload the < operator. Can you tell me?

Thanks!
 
struct exampleStruct{
int a,b,c,d;

int operator < (struct exampleStruct);
};

int exampleStruct::eek:perator < (struct es)
{
int cmp = es.a + es.b + es.c + es.d;
return a+b+c+d < cmp;
}

Something like that will override how the < operator is handled on comparisons between structs. If you just want to compare a vaule in each then make the funciton do that.

The = operator follows the same format except you set a = es.a b=es.b etc...

Matt
 
I do apologize... i didnt realize i was in the C forum... operator overloading will not work in C, only in C++. You will need to write a unique funciton called compare(struct <structName>)

that will return the appropriate value for you. Sorry bout the confusion... i forget what forum i am in at times.

Matt
 
Thanks for reading my answer. Well, I think the logic error lies in line:
if ((indxfrec_t) > (indxfrec_t[i + 1]))

See, (because this is C - not C++) indxfrec_t is a struct so you can't just compare the struct by using its variable name, but you must using its member.
Try looking at this (For example):

typedef struct MyStruct {
int a;
float real;
} structure;

structure struk;
int myint = 10;

// ... other code

if (struk > myint) // You can't make this expression.
//...
else
//...

/** See, in the if expression, we compare struk with myint. In fact, struk is a variable name for MyStruct. So if you want to compare the myint with member from MyStruct, you should do :
if (struk.a > myint)
or maybe:
if (struk.real > myint)

so you must also refer to struk member.
Well, that's all, I hope my answer will help you. **/
 
Have you tried creating a linked list?

use struct pointers and the malloc function to create new records (look at the dynamic arrays post). Then you can add pointers to the previous and next records in your list as you go along.
This means that you can change the pointers to change the record positions without having to use bubble sort techniques.

I'll see if I can put some code together for you to look at.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top