Question: //Selection.h #include #include using namespace std; typedef struct Student { int roll_no; string name; double marks[5]; double percentage; }Student; // calculate percentage for student void
//Selection.h
#include
// calculate percentage for student void Percentage(Student* Array, int length) { // local variable double total; // loop till the end of the array for (int i = 0; i < length; i++) { // assign total = 0 total = 0; // loop through all the marks of the students for (int j = 0; j < 5; j++) { // calculate the total total = total + Array[i].marks[j]; } // calculate the average and store in percentage Array[i].percentage = total / 5; } }
// selection sort (descending order) void selectionSort(Student* Array, int length) { // local variabes int MAX = 0; Student temp; // loop till the end of the array for (int i = 0; i < length; i++) { // assign value of 'i' index to MAX MAX = i; // loop till (n-i) for (int j = i + 1; j < length; j++) { // compare the percentage if (Array[j].percentage > Array[MAX].percentage) // assign value of 'j' index MAX (if more than) MAX = j; } // swapping numbers temp = Array[i]; Array[i] = Array[MAX]; Array[MAX] = temp; } } // selection sort (descending order) void selectionSortMarks(double* Array, int length) { // local variabes int MIN = 0; double temp; // loop till the end of the array for (int i = 0; i < length; i++) { // assign value of 'i' index to MAX MIN = i; // loop till (n-i) for (int j = i + 1; j < length; j++) { // compare the percentage if (Array[j] < Array[MIN]) // assign value of 'j' index MAX (if more than) MIN = j; } // swapping numbers temp = Array[i]; Array[i] = Array[MIN]; Array[MIN] = temp; } }
//main.cpp
#include
int main() { Student student[5]; // accepting marks for 5 students cout << "Enter 5 student details "; cout << endl; for (int i = 0; i < 5; i++) { cout << endl; cout << "Enter student roll no : "; cin >> student[i].roll_no; cin.ignore(std::numeric_limits
selectionSort(student, 5); cout << endl; cout << "Sorted (Descending order) as per percentage of the students" << endl; // printing the details of the students for (int i = 0; i < 5; i++) { cout << "Student Roll No :" << student[i].roll_no << endl; cout << "Student Name :" << student[i].name << endl; for (int j = 0; j < 5; j++) { cout << "Subject " << j + 1 << " : " << student[i].marks[j] << endl; } cout << "Student percentage :" << std::fixed << setprecision(2) << student[i].percentage << endl; cout << endl; }
return 0; }
Why Im getting error (identifier "getline" is undefined) for this coding.
And please explain why selection sorting is best fit over insertion sort to obtain this output
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
