shifting the next column with respect to the first column in a text file

Hello,
I have a text file consisting of three columns:
12 15 16
13 12 10
10 15 18
18 20 11

I want to shift the next 2 columns in this manner:
12 20 18
13 15 11
10 12 16
18 15 10

Help is appreciated! Thanks
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
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

using matrix = vector< vector<int> >;


void print( const matrix &M )
{
   for ( const auto &row : M )
   {
       for ( auto e : row ) cout << setw(3) << e << ' ';
       cout << '\n';
   }
}


matrix roll( const matrix &M )
{
   int rows = M.size(), cols = M[0].size();
   matrix N( rows, vector<int>( cols ) );
   for ( int r = 0; r < rows; r++ )
   {
      for ( int c = 0; c < cols; c++ ) N[r][c] = M[(r+rows-c)%rows][c];
   }
   return N;
}


int main()
{
   matrix M = { { 12, 15, 16 },
                { 13, 12, 10 },
                { 10, 15, 18 },
                { 18, 20, 11 } };
   cout <<    "Before:\n";   print( M );
   matrix N = roll( M );
   cout << "\n\nAfter:\n";   print( N );
}



Before:
 12  15  16 
 13  12  10 
 10  15  18 
 18  20  11 


After:
 12  20  18 
 13  15  11 
 10  12  16 
 18  15  10 
Last edited on
Thank you for this solution.
The example I provided was a sample. What should be done when I have 1000 rows in a .txt file. Can you please show me the way to solve it by importing the file.txt and again saving it as transformedfile.text
Set up currently to run as a demo. Comment out the "Simulated" line and uncomment the "File" line to run with files.

Another way of doing it would simply be to put the columns in separate vectors instead of the rows and use std::rotate.

What is the purpose of this, BTW?


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
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;

using matrix = vector< vector<int> >;

// Simulated file for demo
istringstream datafile( "12  15  16\n"
                        "13  12  10\n"
                        "10  15  18\n"
                        "18  20  11\n" );

//======================================================================

istream & operator >> ( istream &in, matrix &M )
{
   M.clear();
   for ( string line; getline( in, line ); )
   {
      stringstream ss( line );
      vector<int> row;
      for ( int i; ss >> i; ) row.push_back( i );
      M.push_back( row );
   }
   return in;
}

//======================================================================

ostream & operator << ( ostream &out, const matrix &M )
{
   for ( const auto &row : M )
   {
       for ( auto e : row ) out << setw(3) << e << ' ';
       out << '\n';
   }
   return out;
}

//======================================================================

matrix roll( const matrix &M )
{
   int rows = M.size(), cols = M[0].size();
   matrix N( rows, vector<int>( cols ) );
   for ( int r = 0; r < rows; r++ )
   {
      for ( int c = 0; c < cols; c++ ) N[r][c] = M[(r+rows-c)%rows][c];
   }
   return N;
}

//======================================================================

int main()
{
   istream &in = datafile;     ostream &out = cout;                         // Simulated
// ifstream in( "file.txt" );   ofstream out( "transformedfile.txt" );      // Files
   
   matrix M;
   in >> M;
   out << "Before:\n" << M << '\n';

   matrix N = roll( M );
   out << "After:\n"  << N << '\n';
}

Last edited on
Thank you!
I was actually trying to mix the data set in a way to cancel out the correlation factor.
Last edited on
Registered users can post here. Sign in or register to post.