Swap method in C

Visage said:
I really do think you need to go back to first principles;

1. Write down your program in pseudocode (at first glance it seems like a basic bubble sort).
2. For each line of the pseudo code, write a function call that will perform the specified action for that line.
3. Fill in the individual function bodies.

Ok mate will try this today

Thanks again, any more advice is appreciated
 
Visage said:
I really do think you need to go back to first principles;

1. Write down your program in pseudocode (at first glance it seems like a basic bubble sort).
I think it was actually supposed to be an insertion sort; that's why I provided a (correct) version of insertion sort in the answer I gave.

To be honest, not understanding how the sort works seems to be 90% of tsj's problem; maybe it would be better to use a bubble sort as that is easier to understand.

2. For each line of the pseudo code, write a function call that will perform the specified action for that line.
I don't really agree with this, because if taken literally, I think he's going to end up splitting the actual sort
across several functions, which is going to be a nightmare.

Tsj: What I would do is just go back to the code I gave you that worked (at least I think it worked for you). All you need to do is replace the bit that compares the points (in the for statement) with a check to see if "compare(a,b) < 0" (where a,b are the two things you're comparing: team[j] and T or some such).
 
Last edited:
DaveF said:
I think it was actually supposed to be an insertion sort; that's why I provided a (correct) version of insertion sort in the answer I gave.

To be honest, not understanding how the sort works seems to be 90% of tsj's problem; maybe it would be better to use a bubble sort as that is easier to understand.

I don't really agree with this, because if taken literally, I think he's going to end up splitting the actual sort
across several functions, which is going to be a nightmare.

Tsj: What I would do is just go back to the code I gave you that worked (at least I think it worked for you). All you need to do is replace the bit that compares the points (in the for statement) with a check to see if "compare(a,b) < 0" (where a,b are the two things you're comparing: team[j] and T or some such).

Ok mate, im just going to try this after ive eaten some food then. will be back to let you know how it goes :D
 
DaveF said:
I don't really agree with this, because if taken literally, I think he's going to end up splitting the actual sort
across several functions, which is going to be a nightmare.

If he did at at that level of granulairty then yes, I agree. I was thinking more like getData(), sortData(), outputData() level....
 
After doing what you said:

Code:
int compare(team_info a, team_info b) {
                        
        int result = a.points-b.points;
        if (!result)
            result = (a.goalsfor-a.goalsagainst)-(b.goalsfor-b.goalsagainst);
        if (!result)
            result = a.goalsfor - b.goalsfor;
}
        
void sort_pass() {
                                 
int i,j;
        for (i=0;i<MAX_TEAMS;i++)
        {                       
                team_info T = arrdetails[i];
                for (j=i-1;j>=0 && compare(arrdetails[j],T)<0);j--)
                        arrdetails[j+1]=arrdetails[j];
                arrdetails[j+1]=T;
        }
        
}

That looks fine to me but it says there is not a valid return statement:

cc: Warning: final.c, line 230: Non-void function "compare" does not contain a return statement. (missingreturn)
 
tsj said:
After doing what you said:

Code:
int compare(team_info a, team_info b) {
                        
        int result = a.points-b.points;
        if (!result)
            result = (a.goalsfor-a.goalsagainst)-(b.goalsfor-b.goalsagainst);
        if (!result)
            result = a.goalsfor - b.goalsfor;
}
        
void sort_pass() {
                                 
int i,j;
        for (i=0;i<MAX_TEAMS;i++)
        {                       
                team_info T = arrdetails[i];
                for (j=i-1;j>=0 && compare(arrdetails[j],T)<0);j--)
                        arrdetails[j+1]=arrdetails[j];
                arrdetails[j+1]=T;
        }
        
}

That looks fine to me but it says there is not a valid return statement:

I've added

return result;

to the end of the compare function to return the value of result.

This solved that problem... now I'm missing a bunch of braces

----------------^
cc: Error: final.c, line 245: Missing ";". (nosemi)
for (j=i-1;j>=0 && compare(arrdetails[j],T)<0);j--)
-------------------------------------------------------------^
cc: Error: final.c, line 245: Missing ";". (nosemi)
for (j=i-1;j>=0 && compare(arrdetails[j],T)<0);j--)
------------------------------------------------------------------^

LOL
 
tsj said:
I've added

return result;

to the end of the compare function to return the value of result.

This solved that problem... now I'm missing a bunch of braces

----------------^
cc: Error: final.c, line 245: Missing ";". (nosemi)
for (j=i-1;j>=0 && compare(arrdetails[j],T)<0);j--)
-------------------------------------------------------------^
cc: Error: final.c, line 245: Missing ";". (nosemi)
for (j=i-1;j>=0 && compare(arrdetails[j],T)<0);j--)
------------------------------------------------------------------^

LOL

The error message tells you all you need to know.....
 
Visage said:
The error message tells you all you need to know.....

Ok nice one. i got that sorted now mate:

from this:
Code:
Id Team       Played Won Lost Drawn GoalsF GoalsA Points
0           england      1       1       0       0       3       1       3
1            wolves      2       0       2       0       6       9       0
2         liverpool      1       0       0       1       1       1       1
3              manu      1       0       0       1       1       1       1
4           everton      0       0       0       0       0       0       0
5           chelsea      1       1       0       0       7       5       3
6        nottingham      0       0       0       0       0       0       0
7            astonv      0       0       0       0       0       0       0
8             wigan      0       0       0       0       0       0       0
9            albion      0       0       0       0       0       0       0
10         coventry      0       0       0       0       0       0       0
11          arsenal      0       0       0       0       0       0       0

to this:
Code:
Id Team       Played Won Lost Drawn GoalsF GoalsA Points
5           chelsea      1       1       0       0       7       5       3
0           england      1       1       0       0       3       1       3
2         liverpool      1       0       0       1       1       1       1
3              manu      1       0       0       1       1       1       1
4           everton      0       0       0       0       0       0       0
6        nottingham      0       0       0       0       0       0       0
7            astonv      0       0       0       0       0       0       0
8             wigan      0       0       0       0       0       0       0
9            albion      0       0       0       0       0       0       0
10         coventry      0       0       0       0       0       0       0
11          arsenal      0       0       0       0       0       0       0
1            wolves      2       0       2       0       6       9       0

it seems to be working!!!!!
 
Thanks sooo much for the help guys. I'm going to try and get this entire assignment completed today. I gotta change the input from using ID's to using Strings(Team names instead of 0,1,2 etc) and a few other tweaks ;)
 
tsj said:
Ok nice one. i got that sorted now mate:

from this:
Code:
Id Team       Played Won Lost Drawn GoalsF GoalsA Points
0           england      1       1       0       0       3       1       3
1            wolves      2       0       2       0       6       9       0
2         liverpool      1       0       0       1       1       1       1
3              manu      1       0       0       1       1       1       1
4           everton      0       0       0       0       0       0       0
5           chelsea      1       1       0       0       7       5       3
6        nottingham      0       0       0       0       0       0       0
7            astonv      0       0       0       0       0       0       0
8             wigan      0       0       0       0       0       0       0
9            albion      0       0       0       0       0       0       0
10         coventry      0       0       0       0       0       0       0
11          arsenal      0       0       0       0       0       0       0

to this:
Code:
Id Team       Played Won Lost Drawn GoalsF GoalsA Points
5           chelsea      1       1       0       0       7       5       3
0           england      1       1       0       0       3       1       3
2         liverpool      1       0       0       1       1       1       1
3              manu      1       0       0       1       1       1       1
4           everton      0       0       0       0       0       0       0
6        nottingham      0       0       0       0       0       0       0
7            astonv      0       0       0       0       0       0       0
8             wigan      0       0       0       0       0       0       0
9            albion      0       0       0       0       0       0       0
10         coventry      0       0       0       0       0       0       0
11          arsenal      0       0       0       0       0       0       0
1            wolves      2       0       2       0       6       9       0

it seems to be working!!!!!

Except that your source data is wonky - Wolves should always be top of the table ;)
 
Back
Top Bottom