Discussion:
Sorting each row of a 2D integer array
Add Reply
b***@gmail.com
2017-05-07 16:13:00 UTC
Reply
Permalink
Raw Message
I have a 3 * 3 integer array
9 4 6
8 7 1
6 3 5

I have written this program to sort each row, so that the array becomes:

4 6 9
1 7 8
3 5 6

However, only the 1st invocation of BSort works. Even if I change the order of invocation, only the 1st invocation works. Can anybody help?

#include <iostream>
#include <iomanip.h>
using namespace std;

void Display(int A[][3], int N)
{ for(int i=0; i<N; i++)
{ for(int j=0; j<N; j++)
cout << setw(4) << A[i][j];
cout << "\n";
}
}

void BSort(int A[], int N)
{ int i, j, temp, flag;

for(i=0; i<N-1 && flag; i++)
{ for(j=0,flag=0; j<N-1-i; j++)
{ if(A[j] > A[j+1])
{ temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
flag = 1;
}
}
}
}

int main()
{ int A[][3] = { { 9, 4, 6 },
{ 8, 7, 1 },
{ 6, 3, 5 }
};

Display(A, 3);
cout << "\n======================\n\n";
BSort(A[0], 3); // Sorting row 0
BSort(A[1], 3); // Sorting row 1
BSort(A[2], 3); // Sorting row 2
Display(A, 3);
}
Barry Schwarz
2017-05-07 16:30:43 UTC
Reply
Permalink
Raw Message
Post by b***@gmail.com
I have a 3 * 3 integer array
9 4 6
8 7 1
6 3 5
4 6 9
1 7 8
3 5 6
However, only the 1st invocation of BSort works. Even if I change the order of invocation, only the 1st invocation works. Can anybody help?
#include <iostream>
#include <iomanip.h>
using namespace std;
If you are going to code in C++, you should ask in comp.lang.c++.
Contrary to popular belief, they are different languages.
Post by b***@gmail.com
void Display(int A[][3], int N)
This function will invoke undefined behavior if N > 3. It will
produce unexpected results if N < 3. If the '3' is to be hard coded
in the first parameter, it might as well be a #define.
Post by b***@gmail.com
{ for(int i=0; i<N; i++)
{ for(int j=0; j<N; j++)
cout << setw(4) << A[i][j];
cout << "\n";
}
}
void BSort(int A[], int N)
{ int i, j, temp, flag;
for(i=0; i<N-1 && flag; i++)
At this point, the value in flag is indeterminate. Evaluating it
causes undefined behavior.

The difference in behavior you see can be explained by the fact that
its value is (by happenstance) initially 0 on the first call to this
function but the value is preserved (because the same memory is being
reused) for subsequent calls.
Post by b***@gmail.com
{ for(j=0,flag=0; j<N-1-i; j++)
{ if(A[j] > A[j+1])
{ temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
flag = 1;
}
}
}
}
int main()
{ int A[][3] = { { 9, 4, 6 },
{ 8, 7, 1 },
{ 6, 3, 5 }
};
Display(A, 3);
cout << "\n======================\n\n";
BSort(A[0], 3); // Sorting row 0
BSort(A[1], 3); // Sorting row 1
BSort(A[2], 3); // Sorting row 2
Display(A, 3);
}
--
Remove del for email
Stefan Ram
2017-05-07 16:57:57 UTC
Reply
Permalink
Raw Message
Post by b***@gmail.com
Can anybody help?
This is comp.lang.c.

You should ask questions about C++ in comp.lang.c++.

main.c

#include <stdio.h>
#include <stdlib.h>

int compar( void const * const vl, void const * const vr )
{ int const * const l = vl; int const * const r = vr; return *l - *r; }

int main( void )
{ int s[] ={ 9, 4, 6, 8, 7, 1, 6, 3, 5 };
for( int i = 0; i < 9; i += 3 )
{ qsort( s + i, 3, sizeof 9, compar );
for( int j = 0; j < 2; ++j )printf( "%d, ", s[ i + j ]);
printf( "%d\n", s[ i + 2 ]); }}

transcript

4, 6, 9
1, 7, 8
3, 5, 6

main.cpp

#include <algorithm>
#include <array>
#include <initializer_list>
#include <iostream>
#include <ostream>
#include <string>

using namespace ::std::literals;

int main()
{ ::std::array< int, 3 * 3 >s{ { 9, 4, 6, 8, 7, 1, 6, 3, 5 }};
for( auto i: { 0, 3, 6 })
{ ::std::sort( begin( s )+ i, begin( s )+ i + 3 );
for( auto j: { 0, 1 })::std::cout << s[ i + j ] << ", ";
::std::cout << s[ i + 2 ] << '\n'; }}

transcript

4, 6, 9
1, 7, 8
3, 5, 6
Ike Naar
2017-05-07 17:29:26 UTC
Reply
Permalink
Raw Message
Post by b***@gmail.com
I have a 3 * 3 integer array
9 4 6
8 7 1
6 3 5
4 6 9
1 7 8
3 5 6
However, only the 1st invocation of BSort works.
Even if I change the order of invocation, only the 1st invocation works.
Can anybody help?
You are asking for help on a C++ program in a C newsgroup.
Wouldn't it be more sensible to ask in a C++ group (e.g. comp.lang.c++)?
Post by b***@gmail.com
#include <iostream>
#include <iomanip.h>
#include <iostream>
#include <iomanip.h>
That should be <iomanip>, not <iomanip.h>
Post by b***@gmail.com
using namespace std;
Bad idea. It brings *everyting* from the std namespace into the global
namespace and that will lead into unpleasant surprises.
If you want to use cout without qualifications, use 'using std::cout'.
Or omit the 'using' directive altogether and just write std::cout
if you want std::cout.
Post by b***@gmail.com
void Display(int A[][3], int N)
{ for(int i=0; i<N; i++)
{ for(int j=0; j<N; j++)
cout << setw(4) << A[i][j];
cout << "\n";
}
}
void BSort(int A[], int N)
{ int i, j, temp, flag;
for(i=0; i<N-1 && flag; i++)
{ for(j=0,flag=0; j<N-1-i; j++)
{ if(A[j] > A[j+1])
{ temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
flag = 1;
}
}
}
}
The BSort function has a bug (check the use of the 'flag' variable).
Since you're using C++, why not use std::sort instead?

Loading...