R
R
romentigo2018-05-14 14:35:48
C++ / C#
romentigo, 2018-05-14 14:35:48

C++ - how to sort a structure?

The code is almost ready, but I don't know what to do next. In the structure, I indicate the number of students, then fill in the fields: Last Name, Group, Grade1, Grade2, Grade3. Sr - average score for exams. I thought at first to make Sr as a field, so that later I could sort the structure by the average score in descending order. But something is still wrong in the structure. I am attaching the code:

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
  setlocale(0, ".1251");
  struct student
  {
    char surname, gr;
    int ex1, ex2, ex3, sr;
  };
  int kol = 0;

  cout << "Введiть кiлькiсть студентiв: "; 
  cin >> kol;
  student *z = new student[kol];
  setlocale(0, ".1251");
  cout << "Введiть почергово рядки з вiдомостями про успiшнiсть " << kol
    << " студентiв: \nПрiзвище \tГрупа \tОцiнка1 \tОцiнка2 \tОцінка3" << endl;

  for (int i = 0; i < kol; i++)
  {
    puts(z[i].surname, z[i].gr, &z[i].ex1, &z[i].ex2, &z[i].ex3, &z[i].sr);
  }

  int Count = 0;
  double sr; 
  for (int i = 0; i < kol; i++)
  {
    if (z[i].ex1 >= 60 && z[i].ex2 >= 60 && z[i].ex3 >= 60)
    {
      Count++;
      z[i].sr = (z[i].ex1 + z[i].ex2 + z[i].ex3) / 3.0;
      setlocale(0, ".OCP");
      printf("%s\t%s\t%i\t%i\t%i\t%i\n", z[i].surname, z[i].gr, z[i].ex1, z[i].ex2, z[i].ex3, z[i].sr);
    }
  }

  char* tempS, tempG;
  int temp1, temp2, temp3, tempSr;
  for (int i = 0; i < Count; i++)
  {
    for (int j = 0; j < Count - i; j++)
    {
      if (z[i].sr < z[i + 1].sr)
      {
        tempS = z[i].surname;
        z[i].surname = z[i + 1].surname;
        z[i + 1].surname = tempS;

        tempG = z[i].gr;
        z[i].gr = z[i + 1].gr;
        z[i + 1].gr = tempG;

        temp1 = z[i].ex1;
        z[i].ex1 = z[i + 1].ex1;
        z[i + 1].ex1 = temp1;

        temp2 = z[i].ex2;
        z[i].ex2 = z[i + 1].ex2;
        z[i + 1].ex2 = temp2;

        temp3 = z[i].ex3;
        z[i].ex3 = z[i + 1].ex3;
        z[i + 1].ex3 = temp3;

        tempSr = z[i].sr;
        z[i].sr = z[i + 1].sr;
        z[i + 1].sr = tempSr;
      }
    }
  }

  for (int i = 0; i < kol; i++)
  {
    if (z[i].ex1 >= 60 && z[i].ex2 >= 60 && z[i].ex3 >= 60)
    {
      z[i].sr = (z[i].ex1 + z[i].ex2 + z[i].ex3) / 3.0;
      setlocale(0, ".OCP");
      printf("%s\t%s\t%i\t%i\t%i\t%i\n", z[i].surname, z[i].gr, z[i].ex1, z[i].ex2, z[i].ex3, z[i].sr);
    }
  }

  system("pause");
  return 0;

}

UPD: Ready-made functions and methods are not suitable

Answer the question

In order to leave comments, you need to log in

3 answer(s)
@
@pestunov, 2019-02-24
@romentigo

No need to rearrange all fields one by one

tempS = z[i].surname;
        z[i].surname = z[i + 1].surname;
        z[i + 1].surname = tempS;

        tempG = z[i].gr;
        z[i].gr = z[i + 1].gr;
        z[i + 1].gr = tempG;

        temp1 = z[i].ex1;
        z[i].ex1 = z[i + 1].ex1;
        z[i + 1].ex1 = temp1;

        temp2 = z[i].ex2;
        z[i].ex2 = z[i + 1].ex2;
        z[i + 1].ex2 = temp2;

        temp3 = z[i].ex3;
        z[i].ex3 = z[i + 1].ex3;
        z[i + 1].ex3 = temp3;

        tempSr = z[i].sr;
        z[i].sr = z[i + 1].sr;
        z[i + 1].sr = tempSr;

temp = z[i];
        z[i] = z[i + 1];
        z[i + 1] = temp;

Unlike an array, structures can be assigned as a whole rather than element by element.

V
Vitaly, 2018-05-14
@vt4a2h

https://en.cppreference.com/w/cpp/algorithm/sort
You are interested in this part:

std::sort(s.begin(), s.end(), [](int a, int b) { return a > b; });

R
res2001, 2018-05-14
@res2001

Implement a copy constructor and/or a copy assignment operator in the structure, and bubble sort (because the simplest implementation of sorting). You can also implement several comparison functions for different attributes and pass these functions to the bubble (so that you can sort by different attributes using one sort function).
By code:
1. You start writing in C++, and then jump to C (puts, printf) ...
2. In struct student, the surname and gr fields are declared as char, and in the code you use them as C-strings - they are completely different things.
3. You are not using the puts function correctly. In this form, the program will not assemble for you. Check out the documentation .
Perhaps something else...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question