Hello, I have a task to make a structure, then sort it, etc. enter image description here

On the last two points do not pay attention, have not yet done.

I kind of found a way to make a structure, but I can't normally bring it out.

#include <iostream> #include <string> using namespace std; #define N 3 struct STUDENT { char name[30]; int group; int ses[5]; }; int sum, sra; int *p = &sra; int cmp( const void *p1, const void *p2 ) { return ( *( int * )p1 - * ( int * )p2 ); } int main() { STUDENT stud1[N]; for( int st = 0; st < N; st++ ) { cout << "Input first name: "; cin.getline( stud1[st].name, 30 ); cout << "Input number of group: "; cin >> stud1[st].group; for( int i = 0; i < 5; i++ ) { cout << "Input mark " << i + 1 << ": "; cin >> stud1[st].ses[i]; } for( int i = 0; i < 4; i++ ) { sum = sra = 0; sum = stud1[st].ses[i] + stud1[st].ses[i + 1]; sra = sum / 5; } cout << endl; cin.get(); } qsort( p, N, sizeof( STUDENT ), cmp ); int fl = 1; for( int i = 0; i < N; i++ ) { int j = 0; while( ( j < 5 ) && ( stud1[i].ses[j] != 2 ) ) { j++; } if( j < 5 ) { fl = 0; cout << "Student " << stud1[i].name << " (group " << stud1[i].group << ") have mark 2" << endl; } } if( fl ) { cout << "No students, which have mark 2" << endl; } system( "pause" ); return 0; } 

I tried to derive this:

 for( int st = 0; st < N; st++ ) { cout << stud1[st].name << " " << stud1[st].group << " " << stud1[st].ses[i] << endl; } 

and so

 for( int st = 0; st < N; st++ ) { for( int i = 0; i < 5; i++ ) { cout << stud1[st].name << " " << stud1[st].group << " " << stud1[st].ses[i] << endl; } } 

But in the first case, the last pillar does not display at all, and in the second each record is repeated 5 times (the number of assessments)

Help please find a solution)

And I just found out that sorting for some reason does not work, do not tell me what is my mistake?

  • I have just found out here that sorting is not working for some reason, could you tell me what my mistake is? - Roman

1 answer 1

Try this with nested loops:

 for (int st=0; st<N; st++) { cout << stud1[st].name << " " << stud1[st].group << " "; for(int i = 0; i < 5; ++i) cout << stud1[st].ses[i] << " "; cout << endl; } 

By the way, what are you doing here:

  for( int i = 0; i < 4; i++ ) { sum = sra = 0; sum = stud1[st].ses[i] + stud1[st].ses[i + 1]; sra = sum / 5; } 

Something strange code ... If the average score was considered, then it would be logical to expect

  sum = 0; for( int i = 0; i < 5; i++ ) { sum += stud1[st].ses[i]; } sra = sum / 5; 

Sorting should use

 int cmp( const void *p1, const void *p2 ) { return ( *( int * )p1 - * ( int * )p2 ); } 

Which compares STUDENTS. You compare it is not clear what. For example, a comparison by name should be

 int cmp( const void *p1, const void *p2 ) { return (strcmp(((STUDENT*)p1)->name,((STUDENT*)p2)->name); } 

and the function itself is called as

 qsort(stud1,N,sizeof(STUDENT),cmp); 

But keep in mind that you have C, but not C ++ ...

  • I have just found out here that sorting is not working for some reason, could you tell me what my mistake is? - Roman
  • Try as I wrote. True, I do not know which field you want to sort by. - Harry
  • It is on the average score among the 5 ratings, just for this and I am looking for it. If by name, then I would do it, but I got confused altogether ... And how do you understand that this is C language but not C ++? - Roman
  • It’s just that you don’t use any specific features of C ++ anywhere ... And by average rating, add the average rating field to the STUDENT structure, say, this is sra field. After calculations, sort with the function return (((STUDENT*)p1)->sra - ((STUDENT*)p2)->sra); - Harry