Answer the question
In order to leave comments, you need to log in
Why doesn't bubble sort work on an array?
You need to sort the last names and first names of students in descending order of their average score.
File content:
10
Makov Anton 2 3 4
Antonov Mak 3 4 5
Matros Maks 4 4 4
Atos Andrei 5 5 5
Patos Mukuta 4 3 5
Katos Mukola 1 2 1
Katod Sasha 2 3 2
Anod Anton 4 5 5
Ashot Maks 2 3 2
Dakota Bohdan 3 2 3
My code
#include "stdafx.h"
#include <iomanip>
#include <iostream>
#include <fstream>
using namespace std;
class Student
{
char surname[50];
char name[50];
int a,b,c; // бали
double sredne;
public:
char *getSurname()
{
return surname;
}
void setSurname(char *surname)
{
strcpy(this->surname, surname);
}
char *getName()
{
return name;
}
void setName(char *name)
{
strcpy(this->name, name);
}
int getBall()
{
return a,b,c;
}
void setBall(int a, int b, int c)
{
this->a = a;
this->b = b;
this->c = c;
}
double getSredne()
{
return sredne;
}
void setSredne(double sredne)
{
this->sredne = sredne;
}
Student(double sredne)
{
this->sredne = sredne;
}
void Print()
{
cout << std::setprecision(2);
cout << surname << name << " has " << a<<" "<<b<<" "<<c << " marks. " << "Sredne: " << sredne <<endl;
}
};
int main(int argc, char* argv[])
{
ifstream fin("input_data.txt");
if (!fin)
{
cout << "Can't open file!"<<endl;
}
int kilkist, i, j;
fin >> kilkist;
Student *s = new Student[kilkist];
for (i = 0; i<kilkist; i++)
{
char name[50];
char surname[50];
double a,b,c;
fin >> surname;
fin >> name;
fin >> a;
fin >> b;
fin >> c;
s[i].setSurname(surname);
s[i].setName(name);
s[i].setBall(a,b,c);
double sredne = (a + b + c) / 3;
s[i].setSredne(sredne);
}
//сортировка не работает!
for (i = 0; i < kilkist - 1; i++)
{
for (j = i + 1; j < kilkist; j++)
{
if (s[i].getSredne < s[j].getSredne)
{
Student temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
for (i = 0; i<kilkist; i++)
s[i].Print();
fin.close();
return 0;
}
s[i].getSredne < s[j].getSredne
)?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question