Washi
Washi

Reputation: 1

How to print students name in ascending according to marks

I had sort the mark for all tests, quizzes, assignments and final exam in ASCENDING order. But I don't know how to display all student's name in ascending order based on their mark for each tests, quizzes, assignments and final exam.

Below are my code to sort each mark for tests, quizzes, assignments and final exam in ascending order. How to change the code so that it will display the students name, not the marks for each tests, quizzes, assignments and final exam?

Please, help. Thank you in advance.

#include <iostream>
#include <string>
using namespace std;

//global constant
const int NUM_STUDENTS=5;//row
const int NUM_SCORES=4;//col
string name[5]={"Hani","Haziq","Aiman","Farah","Sabrina"};
string mark[4]={"test","quiz","assignment","final exam"};

//Function prototypes
void ascenDescen(double [ ][NUM_SCORES],int);

int main ( )
{
    cout<<"This program will help you keep track of your academic record!"<<endl;

    double scores[NUM_STUDENTS][NUM_SCORES]=
     {{9.0,2.7,16.0,78.0},
      {7.4,2.7,19.0,88.0},
      {8.9,3.5,17.5,93.7},
      {10.0,3.0,19.5,64.8},
      {6.3,3.0,16.0,74.2}};

    //function call
    ascenDescen(scores,NUM_STUDENTS);

    cout<<endl;
    cout<<"THANK YOU."<<endl;

    return 0;
}

void ascenDescen (double table[][NUM_SCORES],int rows)
{
    //for ascending 
    cout<<"Press ENTER to sort the mark for all tests, quizzes, assignments and final exam in ASCENDING order : \n\n"; 
    char ch;
    ch=cin.get();

    double ascen;
    for(int col=0;col<NUM_SCORES;col++)
    {
        for(int row=0;row<NUM_STUDENTS;row++)
        {
             for(int j=row+1;j<NUM_STUDENTS;++j)
             {
                if(table[row][col]>table[j][col])
                {
                    ascen=table[row][col];
                    table[row][col]=table[j][col];
                    table[j][col]=ascen;
                }
             }
        }

        cout<<mark[col]<<" mark in ASCENDING order : \n";
        for(int row=0;row<NUM_STUDENTS;row++)
        {
            cout<<"  ";
            cout<<table[row][col];
            cout<<endl;
        }
    }
     cout<<"________________________"<<endl;

}

Upvotes: 0

Views: 353

Answers (2)

Thomas Matthews
Thomas Matthews

Reputation: 57729

A rule of thumb: If you need parallel arrays, chances are you should have a vector of struct.

Let's try modeling the data using a Student structure:

struct Student
{
  std::string name;
  std::vector<int> marks;
};

Thus far, every Student has a name and has some marks. The has-a relationship indicates composition. The is-a relationship indicates inheritance.

Let's add a sort method for the marks. The students will be easier to order by marks, if the marks are sorted.

struct Student
{
  //...
  void sort_marks()
  {
    std::sort(marks.begin(), marks.end()); // Assume default of `std::less<int>`
  }
};

To perform an ordering other than the default, we'll need to define a custom ordering function:

bool Order_By_Marks(const Student& a, const Student& b)
{
    bool a_is_less_than_b = true;
    unsigned int quantity_of_marks = a.marks.size();
    if (b.marks.size() < quantity_of_marks)
    {
       quantity_of_marks = b.marks.size();
    }
    for (unsigned int i = 0; i < quantity_of_marks; ++i)
    {
       if (a.marks[i] > b.marks[i])
       {
         a_is_less_than_b = false;
         break;
       }
    }
    return a_is_less_than_b;
}

To sort a database of Student by marks:

std::vector<Student> database;
// ... input students ...
for (i = 0; i < database.size(); ++i)
{
    database[i].sort_marks();
}
std::sort(database.begin(), database.end(), Order_By_Marks);

You'll need to walk through the code with a debugger to verify the ordering of the student marks and also the ordering of the students by mark.

Upvotes: 2

Washi
Washi

Reputation: 1

#include <iostream>
#include <string>
using namespace std;

//global constant
const int NUM_STUDENTS=5;//row
const int NUM_SCORES=4;//col
string name[5]={"Hani","Haziq","Aiman","Farah","Sabrina"};
string mark[4]={"test","quiz","assignment","final exam"};

struct student {

};

//Function prototypes
void ascenDescen(double [ ][NUM_SCORES],int);

int main ( )
{
    cout<<"This program will help you keep track of your academic record!"<<endl;

    double scores[NUM_STUDENTS][NUM_SCORES]=
     {{9.0,2.7,16.0,78.0},
      {7.4,2.7,19.0,88.0},
      {8.9,3.5,17.5,93.7},
      {10.0,3.0,19.5,64.8},
      {6.3,3.0,16.0,74.2}};

    //function call
    ascenDescen(scores,NUM_STUDENTS);

    cout<<endl;
    cout<<"THANK YOU."<<endl;

    return 0;
}

void ascenDescen (double table[][NUM_SCORES],int rows)
{
    //for ascending 
    cout<<"Press ENTER to sort the mark for all tests, quizzes, assignments and final exam in ASCENDING order : \n\n"; 
    cin.get();

    double ascen;
    string temp1[5] = name;
    string temp2;
    for(int col=0;col<NUM_SCORES;col++)
    {
        for(int row=0;row<NUM_STUDENTS;row++)
        {
             for(int j=row+1;j<NUM_STUDENTS;++j)
             {
                if(table[row][col]>table[j][col])
                {
                    ascen=table[row][col];
                    temp2 = temp1[row];
                    table[row][col]=table[j][col];
                    temp1[row] = temp1[j]; 
                    table[j][col]=ascen;
                    temp1[j] = temp2;

                }
             }
        }

        cout<<mark[col]<<" mark in ASCENDING order : \n";
        for(int row=0;row<NUM_STUDENTS;row++)
        {
            cout<<"  ";
            cout << temp1[row] << " " <<table[row][col];
            cout<<endl;
        }
        for(int i = 0; i < NUM_STUDENTS; i++) 
        {
        temp1[i] = name[i];
        }
    }
     cout<<"________________________"<<endl;

}

Upvotes: 0

Related Questions