Sorting Structure in C

I am trying to sort a structure in C. I am using one of the fields of the structure as a sort field. The current problem is it won't compile with the temp field of the sort as an integer and the other field being sorted as a array of the structure.
The error I am getting is
Cannot assign "majorteam" to "int" in function main
Cannot cast from "int" to "majorteam" in function main.

I have both the counters for the sort and the temp field defined as integers.

Here is the code:

for (y=0; y<=9; y++)
{
for (x=y; x<=9;x++)
{
if teamone[y].ability > teamone[x].ability)
{
tempteam = teamone[x];
teamone[x] = teamone[y];
teamone[y] = teampteam;
teamone[y] = tempteam;

then a print statement to print the list by ability.

Any help would be appreciated,
Thanks ctirish@aol.com




ctirishAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

captainkirkCommented:
I have made a few assumptions about the data definitions in your problem (the struct you are creating an array of, for example), but the following code compiles:

typedef      struct _team
{
      int ability;
        // and whatever other data...
} TEAM;

TEAM teamone[9];
TEAM tempteam;
TEAM majorteam;

int x, y;

for (y = 0; y <= 9; y++)
{
      for (x = y; x <= 9; x++)
      {
            if (teamone[y].ability > teamone[x].ability)
            {
                  tempteam   = teamone[x];
                  teamone[x] = teamone[y];
                  teamone[y] = tempteam;
            } // end if
      } // end for
} // end for

1) You obviously will need to load the data into the array of structs;

2) majorteam and tempteam needed to be defined as whatever struct you are using, in my modified code, TEAM. In your code, for example, you had:

tempteam = teamone[x]; // tempteam is an int and teamone[x] is a struct, so the assignment will not work... the same, I assume, is true for majorteam...


hope this helps...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
azamiCommented:
captainkirk has it right, I just want to point out that (judging from the error message) majorteam is the name of the struct, not a separate variable:

majorteam teamone[9];

Regardless, the problem is that teamone[n] is of type majorteam, and tempteam is of type int.  So the line
tempteam = teamone[x]; tries to fit a majorteam into an int variable and
teamone[y] = tempteam; tries to fit an int into a majorteam variable.

tempteam needs to be a majorteam, not an int.
0
captainkirkCommented:
yeah - sorry - missed that somehow... substitute "majorteam" for "TEAM" in the example...
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

danny_pavCommented:
also, you will be comparing when x == y.  It won't cause a problem but it is sloppy. You may want to clean that up.

Also, why not use a qsort or other given function?
0
azamiCommented:
This looks a lot like a class assignment, I assume that's why not to use a pre-built (and tested) sort routine.

If this is not a school project, there's actually quite a lot to improve -
* STL offers much better alternatives to arrays
* 9 appears several places and should be a constant
* as danny_pav mentions, this problem has been solved many times, just grab an existing sort function
0
ctirishAuthor Commented:
Thank you to all who helped. It was in fact a class assignment.
0
captainkirkCommented:
thanks....
0
azamiCommented:
I just want to point out that for getting assignment help here at EE, ctirish did everything right - a specific question, detailed error message, and a sample of already tried code.  And an obvious interest in finding an answer, as opposed to just getting free code.

So many people come here just looking for someone to do their homework for them!  Thanks for validating my faith in students!
0
jean_pierreCommented:
you could always overload the '=' operator to perform the struct assignment as well. While I'm not sure if you can do this in C, it is possible in C++ via a member function.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.