Program doesnt sort

Hi, i tried to make a program that will sort a 2d array rows in ascending order, with the bubble sort,but it doesnt work, can someone please tell me where is the mistake

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
#include <iostream>
using namespace std;
int aux;
int main()
 {
     int m,n,i,j,ok;


     cout<<"Rows "; cin>>n;
     cout <<"Columns "; cin>>m;
     int  M[n][m];

for(i=0; i<n;i++)
    for (j=0; j<m; j++){
    cout<<"M["<<i<<"]["<<j<<"]=  "; cin>>M[i][j];
}

  ok=0;
  while(ok==0)
  {
      ok=1;
      for(i=0;i<n-1;i++)
        for(j=0;i<m;j++)
        if(M[i][j]>M[i+1][j])
      {
          aux = M[i][j];
          M[i][j] = M[i+1][j];
          M[i+1][j]=aux;
          ok=0;
      }
  }



    cout<<endl;
    cout<< "Sorted data : "<<endl;
    cout<<endl;

    for(i=0; i<n;i++){
    for (j=0; j<m; j++){
            cout<<M[i][j]<< "  ";}
            cout<<"\n";
}
    return 0;

}

Last edited on
Line 23 looks wrong. I think you meant for(j=0; j<i; ++j)
Line 23 looks wrong. I think you meant for(j=0; j<i; ++j)

Still doesnt work..
Last edited on
What does "Still doesnt work.." actually mean?

Show your input, the program's output with that input and your desired output.

C++ doesn't have variable length arrays, the size of the array has to be known at compile time.
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
#include <iostream>
#include <utility>  // std::swap

int main()
{
   const unsigned SIZE { 4 };

   int x[SIZE][SIZE] { { 7, 3, -2, 0 },
                       { -5, 9, 2, 12 },
                       { 8, -1, 4, 10 },
                       { -6, 11, 0, 5 } };

   std::cout << "The UNsorted array:\n";
   for (unsigned i { }; i < SIZE; i++)
   {
      for (unsigned j { }; j < SIZE; j++)
      {
         std::cout << x[i][j] << '\t';
      }
      std::cout << '\n';
   }
   std::cout << '\n';

   for (unsigned i { }; i < SIZE; i++)
   {
      for (unsigned j { }; j < SIZE; j++)
      {
         unsigned m = i;
         unsigned n = j + 1;

         while (true)
         {
            if (n == SIZE)
            {
               n = 0;
               m++;
               if (m == SIZE) break; // Stopping condition: past the last element in array
            }

            if (x[i][j] > x[m][n]) std::swap(x[i][j], x[m][n]);

            n++;
         }
      }
   }

   std::cout << "The sorted array:\n";
   for (unsigned i { }; i < SIZE; i++)
   {
      for (unsigned j { }; j < SIZE; j++)
      {
         std::cout << x[i][j] << '\t';
      }
      std::cout << '\n';
   }
}
The UNsorted array:
7       3       -2      0
-5      9       2       12
8       -1      4       10
-6      11      0       5

The sorted array:
-6      -5      -2      -1
0       0       2       3
4       5       7       8
9       10      11      12
Last edited on
Topic archived. No new replies allowed.