0

I'm studying c++ and I was given the task. I need to created structure Student

#include "stdafx.h"
using namespace std;

const int num = 5;

struct Student  {
    string name;
    int groupNumber;
    int progress[num];
};

and work with it.
it's my programm

#include "stdafx.h"
#include "Student.h"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
srand((unsigned)time(NULL));

int n;
cout << "Input n = ";
cin >> n;
cin.ignore();

Student * Group = new Student[n];

for (int i = 0; i < n; ++i)
{
    cout << "Input name: ";
    getline (cin, Group[i].name);
    Group[i].groupNumber = rand()%5 + 1;
    for (int j = 0; j < num; ++j)
    {
        Group[i].progress[j] = rand()%5 + 2;
    }
}

int * groupNumberArray = new int[n];
for (int i = 0; i < n; ++i)
{
    groupNumberArray[i] = Group[i].groupNumber;
}

for (int i; i < n - 1; ++i)
{
    int min = i;
    for (int j = i + 1; j < n; ++j)
    {
        if (groupNumberArray[min] <= groupNumberArray[j]) continue;
        min = j;
    }
    if (min != i)
    {
        int temp = groupNumberArray[i];
        groupNumberArray[i] = groupNumberArray[min];
        groupNumberArray[min] = temp;

        Student * tempStudent = &Group[i];
        &Group[min] = &Group[i];
        &Group[i] = tempStudent;
    }
}

cout << "\n\n";

delete [] Group;
delete [] groupNumberArray;

system("PAUSE");
return 0;
}  

I need to sort students in accordance with the growth of groupNumber.
I tried to use pointers but it works wrong. How to make the sorting works the right way?

12
  • 4
    Please state your requirements, otherwise we'll just say "use std::sort with a suitable predicate, the end". Commented Oct 11, 2013 at 12:45
  • 1
    The C++ way would be: Provide a comparator for the Student struct and just use the sort algorithm. Commented Oct 11, 2013 at 12:45
  • 2
    Declare group as std::vector<Student> group; then use std::sort .... Commented Oct 11, 2013 at 12:45
  • Great prediction juanchopanza :) Commented Oct 11, 2013 at 12:46
  • I'm just starting to learn С++, I dont know about std::sort and about std::vector. I need to use simple ways. Commented Oct 11, 2013 at 12:49

2 Answers 2

3

A possibility is to use std::sort with a comparator method on your array.

Example:

bool Compare_By_Group(const Student& a, const Student& b)
{
  return a.groupNumber /* insert comparison operator here */ b.groupNumber;
}

// The sort function call
    std::sort(&Group[0],
              &Group[/* last index + 1*/],
              Compare_By_Group);
Sign up to request clarification or add additional context in comments.

1 Comment

&Group[/* last index + 1*/] is illegal C++ (UB). Use Group + N instead (there’s really no reason to use &Group[0] instead of pointer decay of Group, except for reasons of overload resolution).
1
    vector<Student> students(n);
    .. populate students
    sort(students.begin(), students.end(), [](const Student& left, const Student& right){ return left.groupNumber < right.groupNumber; });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.