Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program to sort a 2-dimensional array in ascending order according t

ID: 3778425 • Letter: W

Question

Write a C++ program to sort a 2-dimensional array in ascending order according to the value of quiz 1 (located in second column) . The two Dimensional array has 3 columns and 5 rows. The array holds the following values: the first column holds the studentID, the second column holds grade of quiz 1 and the third column holds the grade of quiz 2 and each row represents a single student.

In the main program you need to declare the array, fill the array, print the array before sorting, sort the array, and then print the array after sorting.

Test your program on the following:

Input values:

11111

20

11

22222

10

20

33333

39

14

44444

29

15

55555

22

23

Array after sorting and displayed to screen:

22222

10

20

11111

20

11

55555

22

23

44444

29

15

33333

39

14

Remember to swap the complete row.  

11111

20

11

22222

10

20

33333

39

14

44444

29

15

55555

22

23

Explanation / Answer

#include<iostream>
using namespace std;


void sort(int a[][3])
{
int t1,t2,t3;

//Here apply any sorting algoritm. I used bubble sort
for(int i=0;i<4;i++)
{
   for(int j=0;j<4-i;j++)
   {
   if(a[j][1]>a[j+1][1])
   {
       //Swapping the row contents using tmp variables t1,t2,t3

       t1=a[j][0];
       t2=a[j][1];
       t3=a[j][2];

       a[j][0]=a[j+1][0];
       a[j][1]=a[j+1][1];
       a[j][2]=a[j+1][2];
      
       a[j+1][0]=t1;
       a[j+1][1]=t2;
       a[j+1][2]=t3;
   }
   }
}
}

int main(void)
{

int array[5][3];

cout<<"Enter array by row wise:"<<endl;
for(int i=0;i<5;i++)
{
   cout<<"Enter row "<<i+1<<":";
   cin>>array[i][0]>>array[i][1]>>array[i][2];
}

cout<<"The array before sorting is:"<<endl;

for(int i=0;i<5;i++)
{
   cout<<array[i][0]<<":"<<array[i][1]<<":"<<array[i][2]<<endl;
}


sort(array);
cout<<"The array after sorting is:"<<endl;

for(int i=0;i<5;i++)
{
   cout<<array[i][0]<<":"<<array[i][1]<<":"<<array[i][2]<<endl;
}

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote