Skip to content

Instantly share code, notes, and snippets.

@theotheo
Created January 10, 2019 14:35
Show Gist options
  • Save theotheo/594d8ebdaf4fa75495e057b7838bdcd4 to your computer and use it in GitHub Desktop.
Save theotheo/594d8ebdaf4fa75495e057b7838bdcd4 to your computer and use it in GitHub Desktop.
Reorder matrix
#include <iostream>
int main()
{
int n = 5;
int aa[5][5];
int a[5][5] = {
{0, 1, 1, 1, 1},
{1, 1, 0, 1, 1},
{1, 0, 1, 1, 1},
{1, 1, 1, 0, 1},
{1, 1, 1, 1, 0}};
for(int i=0; i < n; i++) {
int pos = std::distance(a[i], std::find(a[i], a[i] + n, 0));
std::copy(a[i], a[i] + n, aa[n - 1 - pos]);
}
std::cout << "original";
for(int i=0; i < n; i++) {
for(int j=0; j < n; j++) {
std::cout << a[i][j];
}
std::cout << std::endl;
}
std::cout << "sorted";
for(int i=0; i < n; i++) {
for(int j=0; j < n; j++) {
std::cout << aa[i][j];
}
std::cout << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment