Answer to Question #155917 in C++ for Usman

Question #155917

Display record of all the students greater than X marks in final exam (in ascending order with respect to marks obtained in final exam). The value of X will be entered by the user. 


1
Expert's answer
2021-01-17T08:07:55-0500
struct student
{
    string name;
    int regd_no,finalMark;
};
//method declarations
void input(student [],int);
void showAll(student [],int);
void PrintgreaterToX(student [],int,int);
//driver code
int main()
{
   int n,i,x;
   cout<<endl<<"Enter the number of students : ";
   cin>>n; //ask for number of records
   student *s = new student[n];//allocate memory to store n student records
   input(s,n); //call to input()
   cout<<endl<<"DETAIL STUDENT INFORMATION\n****************************\n";
   cout<<"REGD_NO\tNAME\tFINAL_SCORE\n**************************\n";
   showAll(s,n); //call to showAll()
   cout<<endl<<"Enter the value of X : ";
   cin>>x; //read the value of x
   cout<<endl<<"STUDENTS DETAILS WHOSE FINALSCORE GREATER TO "<<x<<"\n*******************************************\n";
       cout<<"REGD_NO\tNAME\tFINAL_SCORE\n**************************\n";
   PrintgreaterToX(s,n,x); //call to PrintgreaterToX()
}
//define method input()
void input(student *s,int n)
{
    int i;
    //loop to read n student records
    for(i=0;i<n;i++)
    {
       cout<<"Enter details for STUDENT - "<<i+1;
       cout<<endl<<"NAME : ";
       fflush(stdin);
       getline(cin,s[i].name); //read the name
       cout<<endl<<"REGISTRATION NUMBER : ";
           fflush(stdin);
       cin>>s[i].regd_no; //read the rigistration number
       cout<<endl<<"FINAL MARK : ";
           fflush(stdin);
       cin>>s[i].finalMark; //read the final mark
       }
}
//define showAll()
void showAll(student *s, int n)
{
   int i;
   //loop to print the details of all student
   for(i=0;i<n;i++)
   {
      cout<<endl<<s[i].regd_no<<"\t"<<s[i].name<<"\t"<<s[i].finalMark;
       }
   }
   //define PrintgreaterToX()
   void PrintgreaterToX(student *s, int n,int x)
{
   int i,j,t;
   string temp;
   //sort the records in ascending order according to final score using selection sort
   for(i=0;i<n;i++)
   {
      temp.clear();
      for(j=i+1;j<n;j++)
      {
         if(s[i].finalMark > s[j].finalMark)
         {
            //swap registration numbers
            t=s[i].regd_no;
            s[i].regd_no = s[j].regd_no;
            s[j].regd_no=t;
            //swap marks
            t=s[i].finalMark;
            s[i].finalMark = s[j].finalMark;
            s[j].finalMark=t;
            //swap names
            temp=s[i].name;
            s[i].name = s[j].name;
            s[j].name=temp;
               }
           }
       }
       //loop to print the student details having mark greater than X
   for(i=0;i<n;i++)
   {
      if(s[i].finalMark > x)
      cout<<endl<<s[i].regd_no<<"\t"<<s[i].name<<"\t"<<s[i].finalMark;
       }
   }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS