#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?

Are you required to use a two-dimensional array as datastructure?

If not I would suggest that you use a 'struct' to create some kind of StudentRecord-datastructure.

I have tried to make a rewrite of you code, using a 'struct':

#include <cstdlib>
#include <iostream>
#include <cmath>

struct StudentRecord
{
	int number;
	double lab_score;
	double midterm_score;
	double final_score;
	double c_grade;
};

void SelectionSort(StudentRecord *pStudents, int count);

using namespace std;

int main()
{
	int i;
	int count;
	StudentRecord *pStudents;
	double totallab;
	double totalmid;
	double totalfinal;
	double avglab;
	double avgmid;
	double avgfinal;

	cout << "Number of students: ";
	cin >> count;

	cout << endl;

	pStudents = new StudentRecord[count];

	for(i = 0; i < count; i++)
	{
		cout << "Please enter student number: ";
		cin >> pStudents[i].number;
	}

	cout << endl;
	cout << "Please enter Lab scores." << endl;

	for(i = 0; i < count; i++)
	{
		cout << "Student " << pStudents[i].number << ": ";
		cin >> pStudents[i].lab_score;
	}

	cout << endl;
	cout << "Please enter Midterm scores." << endl;

	for(i = 0; i < count; i++)
	{
		cout << "Student " << pStudents[i].number << ": ";
		cin >> pStudents[i].midterm_score;
	}

	cout << endl;
	cout << "Please enter Final scores." << endl;

	for(i = 0; i < count;i++)
	{
		cout << "Student " << pStudents[i].number << ": ";
		cin >> pStudents[i].final_score;
	}

	cout << endl;

	totallab   = 0.0;
	totalmid   = 0.0;
	totalfinal = 0.0;

	for(i = 0; i < count; i++)
	{
		totallab   += pStudents[i].lab_score;
		totalmid   += pStudents[i].midterm_score;
		totalfinal += pStudents[i].final_score;
	}

	avglab   = totallab   / (double)count;
	avgmid   = totalmid   / (double)count;
	avgfinal = totalfinal / (double)count;

	for(i = 0; i < count; i++)
	{
		pStudents[i].c_grade  = pStudents[i].lab_score     * 0.35;
		pStudents[i].c_grade += pStudents[i].midterm_score * 0.25;
		pStudents[i].c_grade += pStudents[i].final_score   * 0.40;
	}

	SelectionSort(pStudents, count);

	cout << "Student-num     " << "Labs   " << "MidTerm  " << "Final  " << "C-Grade" << endl;
	cout << "----------------------------------------------";

	for(i = 0; i < count; i++)
	{
		cout << pStudents[i].number        << "\t";
		cout << pStudents[i].lab_score     << "\t";
		cout << pStudents[i].midterm_score << "\t";
		cout << pStudents[i].final_score   << "\t";
		cout << pStudents[i].c_grade       << endl;
	}

	cout << endl;
	cout << endl;
	cout << endl;
	cout << "Average of Labs: "     << avglab   << endl;
	cout << "Average of Midterms: " << avgmid   << endl;
	cout << "Average of Finals: "   << avgfinal << endl;
	cout << endl;
	cout << endl;
	cout << endl;

	for(i = 0; i < count; i++)
	{
		if(pStudents[i].c_grade < 50.0)
		{
			cout << "Student " << pStudents[i].number << " has failed!!" << endl;
		}
	}

	if(count > 1)
		delete [] pStudents;
	else
		delete pStudents;

	return 0;
}

void SelectionSort(StudentRecord *pStudents, int count)
{
	int i, j, min;
	StudentRecord temp;

	for(i = 0; i < count - 1; i++)
	{
		min = i;
		for(j = i + 1; j < count; j++)
		{
			if(pStudents[j].c_grade < pStudents[min].c_grade)
				min = j;
		}
		temp = pStudents[i];
		pStudents[i] = pStudents[min];
		pStudents[min] = temp;
	}
}

yes i am required to use 2d arrays

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.