So, I have been writing a sorting loop for my program. It automatically sorts Arrays and has done it in the past efficently. That being said, once I added it into my American Football Manager program, things did not work out so effectively.
So below, I will provide a working version of this sorting loop, and then after the Football Manager version which isn't working.
Here is the working one...
#include <string>
using namespace std;
#include <time.h>
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
#include <stdio.h>
int main()
{
int i, j,temp;
int values[10] = { 7, 4, 2, 8, 1, 3, 9, 6, 10, 5 }; // array of 10 'int' values
int values_count = sizeof(values) / sizeof(int); // 'sizeof' returns the number of 'int' values in the array (10)
cout << values_count;
printf ("Unsorted...\n");
for (i = 0; i < values_count; i++)
printf("element %2d: %2d\n", i, values[i]);
for (i = 0; i < values_count - 1; i++) // loop from 0 to 8 (the first element is values[0]; 10th is values[9])
{
// Compare values[i] with each element from values[i+1] to the values[9] (indexed in the inner loop by 'j').
// After executing the inner loop, values[i]is now assured to be less than any from values[i+1] to values[9].
for (j = i + 1; j < values_count; j++) // When i=0, loop from 1-9; When i=1, loop from 2-9; When i=2, loop from 3-9, etc.
{
// We are sorting to get 'least-to-greatest'. So, any 'values' array elememnt values[j] beyond values[i]
// (outer loop) that is greater than values[i] is swapped with the [i] postition in the array
if (values[j] < values[i])
{
temp = values[i];
values[i] = values[j];
values[j] = temp;
}
}
}
printf("\nSorted...\n");
for (i = 0; i < values_count; i++)
printf("element %2d: %2d\n", i, values[i]);
return 0;
}
That one works perfectly. Here is the second that is failing...
#include <string>
using namespace std;
#include <time.h>
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
#include <stdio.h>
int main()
{
class Team{
public:
int wins, losses;
};
Team teams[10];
int team_count = 10;
int j, i, temp;
teams[1].wins = 1;
teams[2].wins = 2;
teams[3].wins = 5;
teams[4].wins = 3;
teams[5].wins = 6;
teams[6].wins = 10;
teams[7].wins = 7;
teams[8].wins = 4;
teams[9].wins = 8;
teams[10].wins = 9;
for (i = 0; i < team_count; i++){
printf("element %2d: %2d\n", i, teams[i].wins);
for (i = 0; i < team_count - 1; i++) // loop from 0 to 8 (the first element is values[0]; 10th is values[9])
{
// Compare values[i] with each element from teams[i+1] to the teams[9] (indexed in the inner loop by 'j').
// After executing the inner loop, teams[i]is now assured to be less than any from teams[i+1] to teams[9].
for (j = i + 1; j < team_count; j++) // When i=0, loop from 1-9; When i=1, loop from 2-9; When i=2, loop from 3-9, etc.
{
// We are sorting to get 'least-to-greatest'. So, any 'teams' array elememnt teams[j] beyond teams[i]
// (outer loop) that is greater than teams[i] is swapped with the [i] postition in the array
if (teams[j].wins < teams[i].wins)
{
temp = teams[i].wins;
teams[i].wins = teams[j].wins;
teams[j].wins = temp;
}
}
}
}
}
This is the output I get for the second version...
element 0: 4253632
Process exited after 0.08073 seconds with return value 0 Press any key to continue . . .
Help appreciated