M
M
martuwka2017-09-21 23:01:41
C++ / C#
martuwka, 2017-09-21 23:01:41

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;
}

I've been working on this problem for a few hours now. Why doesn't the student GPA comparison work: ( s[i].getSredne < s[j].getSredne)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2017-09-21
@martuwka

Why doesn't the student's GPA comparison work: (s[i].getSredne < s[j].getSredne)?

Because it's not a function call, but something else. It will be right
s[i].getSredne() < s[j].getSredne()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question