Answer to Question #159008 in C++ for huraira

Question #159008

there are many errors in it please solve it without using void

#include<iostream>

using namespace std;

//STRUCT STUDENT TO STORE THE STUDENT DETAILS

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;

       }

   }


1
Expert's answer
2021-01-28T04:28:08-0500
#include <iostream>
#include <iomanip>
using namespace std;


//STRUCT STUDENT TO STORE THE STUDENT DETAILS


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, x;


    cout << "Enter the number of students: ";
    cin >> n; //ask for number of records
    student s[n];
    
    input(s, n); //call to input()
    
    cout << "\nDetails student information: \n";
    cout << "************************************************************\n";
    cout << setw(20) << "Student_Name |" << setw(25) << "Registration_Number |" << setw(20) << "Final_Mark |\n";
    showAll(s, n); //call to showAll()
   
    cout << "Enter the value of x: ";
    cin >> x; //read the value of x
    cout << "\nDetails student information whose finalscore greater than x: \n";
    cout << "************************************************************\n";   
    cout << setw(20) << "Student_Name |" << setw(25) << "Registration_Number |" << setw(20) << "Final_Mark |\n";
    PrintgreaterToX(s,n,x); //call to PrintgreaterToX()


}


//define method input()
void input(student s[], int n) {


    //loop to read n student records
    for(int i = 0; i < n; i++) {
       cout << "Enter details for student - "<< i + 1;
       cout << "\nname: ";
       cin >> s[i].name; //read the name
       cout << "registration number: ";
       cin >> s[i].regd_no; //read the rigistration number
       cout << "final mark: ";
       cin >> s[i].finalMark; //read the final mark
    }
}


//define showAll()
void showAll(student s[], int n) {
   
    //loop to print the details of all student
    for(int i = 0; i < n; i++) {
        cout << setw(18) << s[i].name<< " |" << setw(23) << s[i].regd_no << " |" << setw(18) << s[i].finalMark << " |\n"; 
    }
}


  //define PrintgreaterToX()
void PrintgreaterToX(student s[], int n, int x) {
    string temp;
    //sort the records in ascending order according to final score using selection sort
    for(int i = 0; i < n; i++) {
        for(int j = i + 1; j < n; j++) {
            if(s[i].finalMark > s[j].finalMark) {
              
                //swap registration numbers
                int 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
                string 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 (int i = 0; i < n; i++) {
        if(s[i].finalMark > x) {
            cout << setw(18) << s[i].name<< " |" << setw(23) << s[i].regd_no << " |" << setw(18) << s[i].finalMark << " |\n"; 
        }
    }
}

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
New on Blog