Question
//Selection Sortings Selection.h #include #include using namespace std; typedef struct Student { int roll_no; string name; double marks[5]; double percentage; }Student; // calculate percentage for
//Selection Sortings
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 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
#include
#include
#include
#include "selection.h"
using namespace std;
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
cout << "Enter student name : ";
getline(cin, student[i].name);
cout << endl;
for (int j = 0; j < 5; j++)
{
cout << "Enter marks for subject " << j + 1 << " : ";
cin >> student[i].marks[j];
}
}
// calculate percentage
Percentage(student, 5);
for (int i = 0; i < 5; i++)
{
selectionSortMarks(student[i].marks, 5);
}
// sort students based on overall percentage
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;
}
1) Please do Explain in brief of how the basic algorithm works including the efficiency of the algorithm for the above C++ program . The used sortings was Selection sort.
2) Explain why selection sort is used for this program instead of Insertion sort
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started