b***@gmail.com
2017-05-07 16:13:00 UTC
Permalink
I have a 3 * 3 integer arrayRaw Message
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);
}