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

Dani AI

Generated

If you must keep a 2D array (as you confirmed) the reason rows don't follow the sorted grade is that only the grade cell is being swapped. Two practical fixes that keep a 2D-array layout are: (A) swap every column for the two row indices when you swap, or (B) sort an index/permutation array and print rows in that order (no row copying). 's struct solution is cleaner, but the two options below work with your existing double array[][5] design.

A — swap whole rows inside selection sort (swap every column for the two rows):

void SelectionSort(double array[][5], int height)
{
    for (int i = 0; i < height - 1; ++i) {
        int min = i;
        for (int j = i + 1; j < height; ++j)
            if (array[j][4] < array[min][4]) min = j;
        if (min != i) {
            for (int col = 0; col < 5; ++col) {
                double tmp = array[i][col];
                array[i][col] = array[min][col];
                array[min][col] = tmp;
            }
        }
    }
}

B — keep rows where they are and sort an index vector (faster to move ints than rows; stable if you use stable_sort):

#include <vector>
#include <algorithm>
#include <numeric>

void MakeOrderByGrade(double array[][5], int height, std::vector<int>& order)
{
    order.resize(height);
    std::iota(order.begin(), order.end(), 0);
    std::stable_sort(order.begin(), order.end(),
        [&](int a, int b){ return array[a][4] < array[b][4]; });
}

Print rows using order[i] as the row index.

Quick troubleshooting notes: initialize your accumulators (e.g., totallab = totalmid = totalfinal = 0.0;) — otherwise you get undefined sums. Storing student IDs as numbers in a floating column makes printing awkward; keep IDs in an int column or store them separately. For portable C++ avoid compiler VLA extensions: prefer std::vector or allocate a contiguous block (or double (*arr)[5] = new double[height][5];). Use std::stable_sort if you need to preserve original order for equal grades.

Recommended Answers

All 2 Replies

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.