Skip to content

Instantly share code, notes, and snippets.

@bryangoodrich
Created July 24, 2015 23:18
Show Gist options
  • Save bryangoodrich/1b9253533d60ea26662f to your computer and use it in GitHub Desktop.
Save bryangoodrich/1b9253533d60ea26662f to your computer and use it in GitHub Desktop.
Input and rotate a grid through designated input format
/*********************************************************************
* Compilation: g++ grid.cpp -o grid.exe
* Execution: grid.exe < input.txt
*
* Input and rotate a grid
*
* This is a toy code snippet for handling informational
* input (integer size of grid), importing multiple
* columns, and exporting with formatted output. Nothing
* amazing, but can provide a basis for this sort of
* data ingress and egress from a program or data
* structure.
*
* $ cat input.txt
* 3
* NW N NE
* W 0 E
* SW S SE
*
* $ grid.exe < input.txt
* NW W SW
* N 0 S
* NE E SE
*********************************************************************/
#include <iostream> // std::cout, std::cin, std::right, std::endl
#include <string> // std::string
#include <iomanip> // std::setw
int main( )
{
int N;
std::cin >> N;
std::string arr[N][N];
for (int i = 0; i < N; ++i)
{
std::string a, b, c;
std::cin >> a >> b >> c;
arr[i][0] = a;
arr[i][1] = b;
arr[i][2] = c;
}
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
{
std::cout <<
std::right <<
std::setw(2) <<
arr[j][i] << " ";
}
std::cout << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment