Advertisement
Guest User

Untitled

a guest
Sep 24th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Student{
  5.         int ID;
  6.         char name[20];
  7.         int marks;
  8. };
  9.  
  10. void sortByMarks(Student arr[], int length);
  11.  
  12. int main(){
  13.     struct Student stu[3];
  14.  
  15.     for(int i=0; i<3; i++){
  16.         cout<<"Enter ID, name and marks of student "<<i+1<<" : \n";
  17.         cin>>s[i].id>>stu[i].name>>stu[i].marks;
  18.     }
  19.  
  20.     sortByMarks(stu,3);
  21.  
  22.     for(int i=0; i<3; i++){
  23.         cout<<stu[i].id<<"\t"<<stu[i].name<<"\t"<<stu[i].marks<<"\n";
  24.     }
  25.  
  26.  
  27.     return 0;
  28. }
  29.  
  30. void sortByMarks(Student arr[], int length){
  31.     bool didSwap;
  32.     Student temp;
  33.  
  34.     do{
  35.         didSwap = false;
  36.  
  37.         for (int i=0; i<length-1; i++){
  38.             if (arr[i].marks> rr[i+1].marks){
  39.                 temp=arr[i];
  40.                 arr[i]=arr[i+1];
  41.                 arr[i+1]=temp;
  42.                 didSwap=true;
  43.             }
  44.         }
  45.     }
  46.     while(didSwap);
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement