Column Wise Descending sort Matrix C++

Im arranging a descending column wise on a matrix
im close but the last part of the loop is sending out 6 1 3 instead of 6 3 1

Enter Order of Matrix - 3 4
Enter the elements of the matrix: 10 6 8 9 19 12 1 2 3 22 33 41

Output

41 33 22 6
19 12 8 1
10 9 2 3

Expected output
41 33 22 6
19 12 8 3
10 9 2 1
Code:
for (int i = 0; i < num1; i++) {


for (int j = 0; j < num2; j++) {
for (int k = 0; k < num2 - j - 1; k++) {
if (array[i][k] < array[i][k + 1]) {
int t = array[i][k];
array[i][k] = array[i][k + 1];
array[i][k + 1] = t;
}
}
}
}
THIS PART of your code is sorting across each ROW descending, not each column!

Something else ... in the code you have not shown ... is mangling the rest.

Show the WHOLE of your code. (In CODE TAGS).



You had better explain more clearly exactly what you are trying to do. Your input matrix looks, I believe, like this:
10  6  8  9
19 12  1  2
 3 22 33 41

and it's not clear even how to get your "expected" output from that.
Last edited on
oh my bad here is my code body
NOTE: this code is inside a switch case statement; that is why there is a choice variable in the end.

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
cout << "enter order of matrix: ";
			cin >> num1 >> num2;
			cout << "Enter the elements of the matrix: ";
		  	 for(int i = 0; i < num1; ++i)
   	 	{
       		 for(int j = 0; j < num2; ++j)
       		 {
           	 cin >> array[i][j];
       	 }
  	  	}
  	  	cout << "GIVEN MATRIX: \n"; 
  	  
  	  	   	for(int i = 0; i < num1; ++i)
   	 	{
       	 	for(int j = 0; j < num2; ++j)
       	 	{
          	  cout << " "<< array[i][j];
       	 }
       	 cout << endl;
       	 
  	 	 }
  	 	 cout << "Matrix after arranging Row-Wise\n";
  	 	 
        for (int i = 0; i < num1; i++) { 
  
          
            for (int j = 0; j < num2; j++) { 
  
                 
                for (int k = 0; k < num2 - j - 1; k++) { 
                    if (array[i][k] < array[i][k + 1]) { 
  
                        
                        int t = array[i][k]; 
                        array[i][k] = array[i][k + 1]; 
                        array[i][k + 1] = t; 
                    } 
                } 
            } 
        } 
        
        
        	for(int i = 0; i < num1; ++i)
   	 	{
       	 	for(int j = 0; j < num2; ++j)
       	 	{
          	  cout << " "<< array[i][j];
       	 }
       	 cout << endl;
  	 	 }
  	 	  cout << "Matrix after arranging Column-Wise\n";
  	 	 
      for (j=0;j<num1;++j) {
 
		for (i=0;i<num2;++i) {
 
			for (k=i+1;k<num2;++k) {
 
				if (array[i][j] < array[k][j]) {
 
					a = array[i][j];
					array[i][j] = array[k][j];
					array[k][j] = a;
					

					
 
				}
 
			}
 
		}
 
	}
        
        	for(int i = 0; i < num1; ++i)
   	 	{
       	 	for(int j = 0; j < num2; ++j)
       	 	{
          	  cout << " "<< array[i][j];
       	 }
       	 cout << endl;
  	 	 }
  	 	cout << "Try again (y/n) ";
		  	cin >> choice;
		  	if (choice == 'y' || choice == 'Y')
		  	{
		  		goto menu;
			}
			else{
				exit(1);
			}
Last edited on
No, the WHOLE of your code, so that we can run it.

PLUS, the COMPLETE explanation of what you are trying to do.
im tryin to do this certain type of activity
https://ibb.co/B45fHh3
w/ this code
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>
using namespace std;

int main()
{
	menu:
	int temp,a,num,num1,num2,place1,place2,row2,column2,i,j,k;
	char choice;
	int array[10][10];
	int arr[101][101]; 
    int maximum = 0;
    int minimum = minimum - 1;
	system("cls");
	cout << "1.) Problem 1 \n";
	cout << "2.) Problem 2\n";
	cout << "Enter choice: "; 
	cin >> num;
	switch(num){
		case 1:
	cout<<"Enter order of Matrix:";
			cin>>num1>>num2;
			cout<<"Enter "<<num1<<" X "<<num2<< " Matrix:\n";
			//input
				for(int i=1;i<=num1;i++)
					for(int j=1;j<=num2;j++)
						cin>>array[i][j];
			//display
			cout<<"Given Matrix is: \n";
			for(int i=1;i<=num1;i++)	{
				for(int j=1;j<=num2;j++)
					cout<<" "<<array[i][j];
					cout<<"\n"; }
			//comparing
			place1=0;
			place2=0;
			for(i=1;i<=num1;i++){	
				for(j=1;j<=num2;j++){
					if (maximum<array[i][j]){
						maximum = array[i][j];
						place1=i;
						place2=j;
						}
					}	
				}	
				
			cout<< " Largest is "<<maximum<<" and is present at position "<<place1<<" , "<<place2<<endl;	
			
			for(i=1;i<=num1;i++){
					for(j=1;j<=num2;j++){
						if (minimum>array[i][j]){  
						minimum = array[i][j];
						}
					}	
				}
					for(i=1;i<=num1;i++){
						for(j=1;j<=num2;j++){
						if (minimum == array[i][j]){
						place1=i;
						place2=j;
						}
					}
				}	
				
			
				
			cout<< " Smallest is "<<minimum<< " and is present at position "<<place1<<" , "<<place2<<endl; 
			cout << "Try again? (y/n) ";
			cin >> try1;
			if(try1 == 'y' || try1 == 'Y')
			{
				goto menu;
			}
			else {
				exit(1)
			}
			break;
		
		case 2:
			cout << "enter order of matrix: ";
			cin >> num1 >> num2;
			cout << "Enter the elements of the matrix: ";
		  	 for(int i = 0; i < num1; ++i)
   	 	{
       		 for(int j = 0; j < num2; ++j)
       		 {
           	 cin >> array[i][j];
       	 }
  	  	}
  	  	cout << "GIVEN MATRIX: \n"; 
  	  
  	  	   	for(int i = 0; i < num1; ++i)
   	 	{
       	 	for(int j = 0; j < num2; ++j)
       	 	{
          	  cout << " "<< array[i][j];
       	 }
       	 cout << endl;
       	 
  	 	 }
  	 	 cout << "Matrix after arranging Row-Wise\n";
  	 	 
        for (int i = 0; i < num1; i++) { 
  
          
            for (int j = 0; j < num2; j++) { 
  
                 
                for (int k = 0; k < num2 - j - 1; k++) { 
                    if (array[i][k] < array[i][k + 1]) { 
  
                        
                        int t = array[i][k]; 
                        array[i][k] = array[i][k + 1]; 
                        array[i][k + 1] = t; 
                    } 
                } 
            } 
        } 
        
        
        	for(int i = 0; i < num1; ++i)
   	 	{
       	 	for(int j = 0; j < num2; ++j)
       	 	{
          	  cout << " "<< array[i][j];
       	 }
       	 cout << endl;
  	 	 }
  	 	  cout << "Matrix after arranging Column-Wise\n";
  	 	 
      for (j=0;j<num1;++j) {
 
		for (i=0;i<num2;++i) {
 
			for (k=i+1;k<num2;++k) {
 
				if (array[i][j] < array[k][j]) {
 
					a = array[i][j];
					array[i][j] = array[k][j];
					array[k][j] = a;
					

					
 
				}
 
			}
 
		}
 
	}
        
        	for(int i = 0; i < num1; ++i)
   	 	{
       	 	for(int j = 0; j < num2; ++j)
       	 	{
          	  cout << " "<< array[i][j];
       	 }
       	 cout << endl;
  	 	 }
  	 	cout << "Try again (y/n) ";
		  	cin >> choice;
		  	if (choice == 'y' || choice == 'Y')
		  	{
		  		goto menu;
			}
			else{
				exit(1);
			}
  	 	 break;
		default: 
			cout << "invalid choice";
	}
	
	return 0;
}
Your code doesn't compile.
my bad add ; on line 75
and declare try1 as a character
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>
using namespace std;

int main()
{
	menu:
	int temp,a,num,num1,num2,place1,place2,row2,column2,i,j,k;
	char choice;
	char try1;
	int array[10][10];
	int arr[101][101]; 
    int maximum = 0;
    int minimum = minimum - 1;
	system("cls");
	cout << "1.) Problem 1 \n";
	cout << "2.) Problem 2\n";
	cout << "Enter choice: "; 
	cin >> num;
	switch(num){
		case 1:
	cout<<"Enter order of Matrix:";
			cin>>num1>>num2;
			cout<<"Enter "<<num1<<" X "<<num2<< " Matrix:\n";
			//input
				for(int i=1;i<=num1;i++)
					for(int j=1;j<=num2;j++)
						cin>>array[i][j];
			//display
			cout<<"Given Matrix is: \n";
			for(int i=1;i<=num1;i++)	{
				for(int j=1;j<=num2;j++)
					cout<<" "<<array[i][j];
					cout<<"\n"; }
			//comparing
			place1=0;
			place2=0;
			for(i=1;i<=num1;i++){	
				for(j=1;j<=num2;j++){
					if (maximum<array[i][j]){
						maximum = array[i][j];
						place1=i;
						place2=j;
						}
					}	
				}	
				
			cout<< " Largest is "<<maximum<<" and is present at position "<<place1<<" , "<<place2<<endl;	
			
			for(i=1;i<=num1;i++){
					for(j=1;j<=num2;j++){
						if (minimum>array[i][j]){  
						minimum = array[i][j];
						}
					}	
				}
					for(i=1;i<=num1;i++){
						for(j=1;j<=num2;j++){
						if (minimum == array[i][j]){
						place1=i;
						place2=j;
						}
					}
				}	
				
			
				
			cout<< " Smallest is "<<minimum<< " and is present at position "<<place1<<" , "<<place2<<endl; 
			cout << "Try again? (y/n) ";
			cin >> try1;
			if(try1 == 'y' || try1 == 'Y')
			{
				goto menu;
			}
			else {
				exit(1);
			}
			break;
		
		case 2:
			cout << "enter order of matrix: ";
			cin >> num1 >> num2;
			cout << "Enter the elements of the matrix: ";
		  	 for(int i = 0; i < num1; ++i)
   	 	{
       		 for(int j = 0; j < num2; ++j)
       		 {
           	 cin >> array[i][j];
       	 }
  	  	}
  	  	cout << "GIVEN MATRIX: \n"; 
  	  
  	  	   	for(int i = 0; i < num1; ++i)
   	 	{
       	 	for(int j = 0; j < num2; ++j)
       	 	{
          	  cout << " "<< array[i][j];
       	 }
       	 cout << endl;
       	 
  	 	 }
  	 	 cout << "Matrix after arranging Row-Wise\n";
  	 	 
        for (int i = 0; i < num1; i++) { 
  
          
            for (int j = 0; j < num2; j++) { 
  
                 
                for (int k = 0; k < num2 - j - 1; k++) { 
                    if (array[i][k] < array[i][k + 1]) { 
  
                        
                        int t = array[i][k]; 
                        array[i][k] = array[i][k + 1]; 
                        array[i][k + 1] = t; 
                    } 
                } 
            } 
        } 
        
        
        	for(int i = 0; i < num1; ++i)
   	 	{
       	 	for(int j = 0; j < num2; ++j)
       	 	{
          	  cout << " "<< array[i][j];
       	 }
       	 cout << endl;
  	 	 }
  	 	  cout << "Matrix after arranging Column-Wise\n";
  	 	 
      for (j=0;j<num1;++j) {
 
		for (i=0;i<num2;++i) {
 
			for (k=i+1;k<num2;++k) {
 
				if (array[i][j] < array[k][j]) {
 
					a = array[i][j];
					array[i][j] = array[k][j];
					array[k][j] = a;
					

					
 
				}
 
			}
 
		}
 
	}
        
        	for(int i = 0; i < num1; ++i)
   	 	{
       	 	for(int j = 0; j < num2; ++j)
       	 	{
          	  cout << " "<< array[i][j];
       	 }
       	 cout << endl;
  	 	 }
  	 	cout << "Try again (y/n) ";
		  	cin >> choice;
		  	if (choice == 'y' || choice == 'Y')
		  	{
		  		goto menu;
			}
			else{
				exit(1);
			}
  	 	 break;
		default: 
			cout << "invalid choice";
	}
	
	return 0;
}
Last edited on
You have num1 and num2 the wrong way round in lines 132, 134, 136.

When you fix those you get
1.) Problem 1 
2.) Problem 2
Enter choice: 2
enter order of matrix: 3 4
Enter the elements of the matrix: 10 6 8 9 19 12 1 2 3 22 33 41
GIVEN MATRIX: 
 10 6 8 9
 19 12 1 2
 3 22 33 41
Matrix after arranging Row-Wise
 10 9 8 6
 19 12 2 1
 41 33 22 3
Matrix after arranging Column-Wise
 41 33 22 6
 19 12 8 3
 10 9 2 1
Try again (y/n) 



Importantly, this error was NOTHING TO DO WITH YOUR ORIGINALLY-POSTED CODE SNIPPET. Hence the need to supply complete code and statement of the problem.
Last edited on
it's solved! thanks man life saver :)
Topic archived. No new replies allowed.