OK thanks, take 2:
This code calculates "Diving scores". My problem is two fold, one- if the contestants are in a tie, they need to be outputed in alphabetical order; two, because they are tied they share the position in the tier and then you continue with the order minus the position one of the tied contestants would take, i.e. let's say there ar 6 contestants 2 ties for 2nd place. the order should be: 1, 2, 2, 4, 5 6.
Code:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
const int ND = 3; //Number of dives
const int NJ = 9; //Number of judges
struct divers
{
string name;
float dtotal [ND];
float ftotals;
};
int main()
{
divers all_divers[20];
int ndiv, min, i, j, k, num, q, aa, bb, cc;
float dd, js, s, x, total, small, large;
js = 0; dd = 0, q = 0;
num = 1;
small = 10.0;
large = 0.0;
total = 0.0;
divers temp;
string temp1;
cout << "RANK" << right << setw(10) << "NAME" << '\t' << '\t' ;
for ( i = 0; i < ND; i++)
{
cout << " DIVE " << i + 1<< '\t' << '\t';
}
cout << setw(4) << "TOTAL" << endl;
cout << "-------------------------------------------------------------------------------"<< endl;
while (fin >> ndiv)
{
if (ndiv != 0)
{
for ( i = 0; i < ndiv; i++)
{
all_divers[i].ftotals = 0.0;
}
for ( i = 0; i < ND; i++)
{
for (j = 0; j < ndiv; j++)
{
fin >> all_divers[j].name >> dd;
for (k = 0; k < NJ; k++)
{
fin >> js;
total += js;
if (js < small)
{
small = js;
}
if (js > large)
{
large = js;
}
}
total = total - (small + large);
all_divers[j].dtotal[i] = dd * total;
all_divers[j].ftotals += all_divers[j].dtotal[i];
total = 0.0;
small = 10.0;
large = 0.0;
}
}
cout << fixed << showpoint;
for (i = 0; i < ndiv; i++)
{
min = i;
for ( j = i+1; j < ndiv; j++)
{
if (all_divers[j].ftotals > all_divers[min].ftotals)
min = j;
}
temp = all_divers[i];
all_divers[i] = all_divers[i] = all_divers[min];
all_divers[min] = temp;
}
// This is the problem area.
for ( i = 0; i < ndiv; i++)
{
if ( all_divers[i].ftotals == all_divers[i+1].ftotals)
{
num = num;
for(aa = 0; aa <= ndiv; aa++)
{
bb = aa;
for (cc = aa + 1; cc <= ndiv ; cc++)
{
if (all_divers[i].name > all_divers[bb].name)
bb = cc;
}
}
temp1 = all_divers[i].name;
all_divers[i].name = all_divers[bb].name;
all_divers[bb].name = temp1;
cout << setfill('0') << setw(2) << setfill(' ') << num << right << setw(12) << all_divers[i].name << '\t';
}
else
{
cout << setfill('0') << setw(2) << setfill(' ') << num << right << setw(12) << all_divers[i].name << '\t';
num++;
} //It ends here
for ( j = 0; j < ND; j++)
{
cout << right << setw(14) << setprecision(2) << all_divers[i].dtotal[j] << '\t';
}
cout << setw(13) << setprecision(2) << all_divers[i].ftotals << endl;
}
num = 1;
cout << endl;
cout << " **************************************************
*****************************" << endl <<endl;
}
if (ndiv == 0)
cout <<endl;
}
return 0;
}