C/C++

Помогите пожалуйста с программированием на C++

Студенты сдали пять экзаменов. Нужно отсортировать список студентов по убыванию общего балла по результатам сданных экзаменов.
Если общий балл - это сумма баллов каждого студента по всем пяти экзаменам, то можно как-нибудь так:
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
int i, j, k, l, n;
cout << "n » ";
cin >> n;
int student[n][5], balls[n][2];
for (i = 0; i < n; i++)
{
balls[i][0] = i + 1;
balls[i][1] = 0;
for (j = 0; j < 5; j++)
{
cin >> student[i][j];
balls[i][1] += student[i][j];
}
}
for (i = 1; i < n; i++)
for (j = n - 1; j >= i; j--)
if (balls[j - 1][1] < balls[j][1])
{
k = balls[j - 1][1];
l = balls[j - 1][0];
balls[j - 1][1] = balls[j][1];
balls[j - 1][0] = balls[j][0];
balls[j][1] = k;
balls[j][0] = l;
}
cout << endl;
for (i = 0; i < n; i++)
{
cout << "student " << setw(3) << i + 1 << " » ";
for (j = 0; j < 5; j++)
cout << setw(3) << student[i][j];
cout << endl;
}
cout << endl;
for (i = 0; i < n; i++)
cout << "student" << setw(3) << balls[i][0]
<< " » " << setw(3) << balls[i][1] << endl;
system("pause > nul");
return 0;
}
*сергей Осауленко*
*сергей Осауленко*
29 440
Лучший ответ
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct student
{
string name;
int ball1;
int ball2;
int ball3;
int ball4;
int ball5;
};

struct sort
{
int num;
double sr_ball;
};

int max_ball(sort * s, const int i)
{
int j = 0;
for(int x = 1; x < i; x++)
if(s[x].sr_ball > s[j].sr_ball) j = x;
return j;
}

int next_max_ball(sort * s, const int i, const int max)
{
int j = -1;
for(int x = 0; x < i; x++)
{
if(s[x].sr_ball > s[j].sr_ball)
{
if(s[x].sr_ball < s[max].sr_ball)
{
j = x;
}
}
}
return j;
}

int main()
{
const int n = 20;
student s[n];
sort t[n];
int i = 0;
const char file[] = "data.txt";
ifstream db;
db.open(file);
if(db.is_open())
{
while(!db.eof() && i < n)
{
db >> s[i].name;
db >> s[i].ball1;
db >> s[i].ball2;
db >> s[i].ball3;
db >> s[i].ball4;
db >> s[i].ball5;

t[i].sr_ball = (double) (s[i].ball1 + s[i].ball2 + s[i].ball3 \
+ s[i].ball4 + s[i].ball5) / 5.;
i++;
}
db.close();
}

int count_find = 0;
int maxball = max_ball(t,i);
for(int x = 0; x < i; x++)
{
if(t[x].sr_ball == t[maxball].sr_ball)
{
t[x].num = 1;
count_find++;
}
}

int max = maxball;
int b = 2;
while(count_find < i)
{
int next = next_max_ball(t, i, max);
if(next == -1) break;
for(int x = 0; x < i; x++)
{
if(t[x].sr_ball == t[next].sr_ball)
{
t[x].num = b;
count_find++;
}
}
max = next;
b++;
}

// вывожу на печать вместе с баллами
for(int x = 1; x < b; x++)
{
for(int y = 0; y < i; y++)
{
if(t[y].num == x)
{
cout << s[y].name << '\t';
cout << s[y].ball1 << '\t';
cout << s[y].ball2 << '\t';
cout << s[y].ball3 << '\t';
cout << s[y].ball4 << '\t';
cout << s[y].ball5 << endl;
}
}
}

return 0;
}
// пример файла с данными:

Рустам Набиуллин Я посчитал средний балл. Возможно, что следовало просто сложить оценки. Подумайте над этом сами.