#include <cstdlib>
#include <iostream>
#include <cmath>
#define width 5
using namespace std;
void SelectionSort(double array[][5], int height);
int main()
{
int height;
int i,j,lab,midterm,final,number;
float avglab,avgmid,avgfinal,totallab,totalmid,totalfinal;
cout<<"Number of students: ";
cin>>height;
cout<<"\n";
double array[height][5];
for(i=0;i<height;i++)
{
cout<<"Please enter student number: ";
cin>>number;
array[i][0]=number;
}
cout<<"\n";
cout<<"Please enter Lab scores.\n";
for(i=0;i<height;i++)
{
cout<<"Student ";printf("%.0f",array[i][0]);cout<<": ";
cin>>lab;
array[i][1]=lab;
}
cout<<"\n";
cout<<"Please enter Midterm scores.\n";
for(i=0;i<height;i++)
{
cout<<"Student ";printf("%.0f",array[i][0]);cout<<": ";
cin>>midterm;
array[i][2]=midterm;
}
cout<<"\n";
cout<<"Please enter Final scores.\n";
for(i=0;i<height;i++)
{
cout<<"Student ";printf("%.0f",array[i][0]);cout<<": ";
cin>>final;
array[i][3]=final;
}
cout<<"\n";
for(i=0;i<height;i++)
{
totallab+=array[i][1];
totalmid+=array[i][2];
totalfinal+=array[i][3];
}
avglab=totallab/height;
avgmid=totalmid/height;
avgfinal=totalfinal/height;
for(i=0;i<height;i++)
{
array[i][4]=array[i][1]*0.35+array[i][2]*0.25+array[i][3]*0.40;
}
SelectionSort(array, height);
cout<<"Student-num "<<"Labs "<<"MidTerm "<<"Final "<<"C-Grade";
cout<<"\n----------------------------------------------";
for(i=0;i<height;i++)
{
cout<<"\n";
for(j=0;j<width;j++)
{
printf("%.0f",array[i][j]);
cout<<"\t";
}
}
cout<<"\n";
cout<<"\n\n";
cout<<"Average of Labs: "<<avglab;
cout<<"\nAverage of Midterms: "<<avgmid;
cout<<"\nAverage of Finals: "<<avgfinal;
cout<<"\n\n\n";
for(i=0;i<height;i++)
{
if(50>array[i][4])
{
cout<<"Student ";printf("%.0f",array[i][0]);cout<<" has failed!!\n";
}
}
return 0;
}
void SelectionSort(double array[][5], int height)
{
int i,j,min,temp;
for(i=0;i<height-1;i++)
{
min=i;
for(j=i+1;j<height;j++)
{
if(array[j][4]<array[min][4])
min=j;
}
temp=array[i][4];
array[i][4]=array[min][4];
array[min][4]=temp;
}
}
now my question is I can sort the 4th column but how can I swap arrays according to the sorting of the 4th column?