0

a code about stuct,l dont know much abou it, please help me to find out my mistakes

input number n, and input n students' information(include his id, name, and 5 grades), get the sum of 5 grades.According to his total score , order the student from high to low.(my code is following)

18th Feb 2017, 9:24 PM
Disen
3 Answers
0
//thank you! #include <iostream> using namespace std; template<class T> void myswap (T&x,T&y) { //change x to y,change y to x T temp=x; x=y; y=temp;} template<class T> void bubbleSort (T a[ ],int n) { int i=n-1; while(i>0) { int lastexchangeindex=0; for(int j=0; j<i; j++) { if (a[ j+1]>a[ j]) {myswap(a[ j],a[ j+1]); lastexchangeindex=j;} } i=lastexchangeindex; } } struct student{ int num; string name; int grade1; int grade2; int grade3; int grade4; int grade5; }; int main() { int n; cin>>n; student object[n]; for(int x=0;x<n;x++) //imput cin>>object[n]>>endl; int score[n]; for(int x=0;x<n;x++) //get the totall score of 5 grades and order the student from large to small { score[n]=object[n].grade1+object[n].grade2+object[n].grade3+object[n].grade4+object[n].grade5; if(score[n]<score[n+1]) bubbleSort(object[n],object[n+1]) } for(int x=0;x<n;x++) //output cout<<object[n]<<endl; return 0; }
18th Feb 2017, 9:25 PM
Disen
0
l m learning c++ by myself...l dont konw how to corret this code
18th Feb 2017, 11:31 PM
Disen