I need help with a program

Hi guys can you help me with this program, please

Consider the two-dimensional array A [1..n, 1..m] with integer elements.
Compose a program that will rearrange the columns of the matrix A so that the elements on its last line to either in ascending order by the method of selection. The modified picture will be displayed on the screen.
For example :

.... -7 -6 -8 ................... -8 -6 -7
.... -3 -9 -5 ................... -5 -9 -3
.... 4 2 0 ............................ 0 2 4
Last edited on
rearrange the columns of the matrix A so that the elements on its last line to either in ascending order by the method of selection.

1. Whar is "method of selection"?

2. Does "last line" mean the bottom row? In your example, does it mean that the {4, 2, 0} has to be in ascending order?

3. Does "rearrange columns" mean that a whole column has to be shifted (to get the last value of column where it should be)?


You have shown no code written by you. No effort. No mistakes. We can't explain what is wrong in the code that you have not written.
the middle row makes 0 sense to me.
It makes sense, if one actually should:
1
2
vector<vector<int>> arr( Cols, vector<int>(Rows) ); // column-major matrix
sort( begin(arr), end(arr), [](const auto& lhs, const auto& rhs){return lhs.back() < rhs.back();});
I see it.
you need to store this data transposed, and sort it row-wise which c++ can do easily.
if you store it 'normally' it will be a lot more trouble.
(yes, I just stated in english what the above code does).

if you MUST use a 2-d array, you can still load it trasnposed and sort the rows (which are actually the columns).
Last edited on
@keskiverto
3. Does "rearrange columns" mean that a whole column has to be shifted (to get the last value of column where it should be)?

Yes
I made a program..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include  <iostream>

using namespace std;

int main()

{
   int n,m,i,j,ni,nj;
   int A[99][99];
   int aux;

   cout<< "Linii : "; cin>>n;
   cout<< "Coloane : "; cin>>m;

   for(i=0;i<n;i++)//input elemenetele
    for(j=0; j<m;j++)
    {
       cout<<"A["<<i<<"]"<<"["<<j<<"]";
       cin>>A[i][j];
    }
    for(i=n-1;i<n;i++){
    for(j=0; j<m;j++){
     for(ni = i;ni<n;ni++){
     for(nj = j;nj<m;nj++){
    {
         if(A[i][j] > A[ni][nj])
         {
            aux = A[ni][nj];
            A[ni][nj] = A[i][j];
            A[i][j] = aux;

         }

    }
     }
     }
    }

    }
   for (i = 0; i < n; i++)
	{
		for (j = 0; j < m; j++)
		{
			cout << A[i][j] << " ";
		}
		cout << endl;
	}

}

    

but it actually sorts the last row but it doesnt arrange the columns
Last edited on
First a note:
1
2
3
4
for ( i=n-1; i<n; i++ ){ // there is only one iteration: with i == n-1
  for ( j=0; j<m; j++ ){
    for ( ni = i; ni<n; ni++ ){ // there is only one iteration: with ni == n-1
      for ( nj = j; nj<m; nj++ ){

Therefore, you have loops that very little and can be take out:
1
2
3
4
i = n-1;
for ( j=0; j<m; j++ ){
  ni = i; // but this is always the same and thus can be done outside
  for ( nj = j; nj<m; nj++ ){


Second, your lines 28-30 swap values of elements A[ni][nj] and A[i][j]
Therefore, you essentially have:
1
2
3
4
5
6
7
8
int row = n-1;
for ( j=0; j<m; j++ ) {
  for ( nj = j; nj<m; nj++ ) {
    if ( A[row][j] > A[row][nj] ) {
      // swap A[row][j] and A[row][nj]
    }
  }
}

Yes, you sort the last row. You swap elements of the last row.

But you want to swap entire columns:
1
2
3
4
5
6
7
8
int last = n-1;
for ( j=0; j<m; j++ ) {
  for ( nj = j; nj<m; nj++ ) {
    if ( A[last][j] > A[last][nj] ) {
      // swap  column j with column nj
    }
  }
}

You have to swap elements on every row of the array, not just on the last.
and how can i implement this in my code?
How would you print columns j and nj?
@keskiverto
How would you print columns j and nj?

I still dont get it, could you please recreate the code and explain?
Lets take your original array:
-7 -6 -8
-3 -9 -5
 4  2  0

How would you print columns 0 and 1, (to get output):
-7 -6
-3 -9
4 2

Last edited on
To display only the first 2 columns:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>

int main()
{
	const std::vector<std::vector<int>> array {{ -7, -6, -8 }, {-3, -9, -5}, {4, 2, 0}};

	for (const auto& r : array) {
		std::copy(r.begin(), std::prev(r.end()), std::ostream_iterator<int>(std::cout, " "));
		std::cout << '\n';
	}
}



-7 -6
-3 -9
4 2


Or to display specified columns:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <initializer_list>

int main()
{
	const std::vector<std::vector<int>> array {{ -7, -6, -8 }, {-3, -9, -5}, {4, 2, 0}};
	const std::initializer_list<int> cols {0, 2};	// Cols to display

	for (const auto& r : array) {
		for (const auto& c : cols)
			std::cout << r[c] << ' ';

		std::cout << '\n';
	}
}



-7 -8
-3 -5
4 0

Last edited on
For the original problem, as C++20 consider to display the required array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <vector>
#include <iostream>
#include <algorithm>

int main()
{
	struct tosort {
		int val {};
		size_t pos {};
	};

	const std::vector<std::vector<int>> array {{ -7, -6, -8 }, {-3, -9, -5}, {4, 2, 0}};
	std::vector<tosort> vsort;

	for (size_t col {}; const auto & v : array)
		vsort.emplace_back(v.back(), col++);

	std::sort(vsort.begin(), vsort.end(), [](const auto& a, const auto& b) {return a.val > b.val; });

	for (const auto& r : array) {
		for (const auto& [val, pos] : vsort)
			std::cout << r[pos] << ' ';

		std::cout << '\n';
	}
}



-8 -6 -7
-5 -9 -3
0 2 4

Last edited on
@seeplus: You make them too fancy for the apparent skill level of the OP.
Topic archived. No new replies allowed.