diff options
Diffstat (limited to 'C++/Arrays/Files, Functions and Arrays bloatless version.cpp')
-rw-r--r-- | C++/Arrays/Files, Functions and Arrays bloatless version.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/C++/Arrays/Files, Functions and Arrays bloatless version.cpp b/C++/Arrays/Files, Functions and Arrays bloatless version.cpp new file mode 100644 index 0000000..963feef --- /dev/null +++ b/C++/Arrays/Files, Functions and Arrays bloatless version.cpp @@ -0,0 +1,78 @@ +// Name: msglm +// Date: +// Program Name: +// Description: + + +#include <iostream> +#include <fstream> +#include <string> +using namespace std; + +// Named constants +int ARR_SIZE = 50; //Personally, i'd pass this as an env var or a flag + +int main() { + + //Variable declaration + string tmpArr[ARR_SIZE]; //Probably could pass through the file first to get the size first and declare this, but that's past this program's scope. + int epoch = 0; //both counts up and is the length of the array. + + //Vars necessary to invert an array + int start = 0; + string tmp; + int end = 0; + + //It's a shame this isn't STDIN and STDOUT + ifstream inFile; + ofstream outFile; + + // User input + if (inFile) { + + inFile.open("in.txt"); + outFile.open("out.txt"); + + //Read the line, put it at the epoch in the array, also check if EOF. if EOF, abort. + while(getline(inFile, tmpArr[epoch]) && inFile.peek() != EOF) { + cout << tmpArr[epoch] << endl; + outFile << tmpArr[epoch] << endl; + epoch = epoch + 1; + } + + //Epoch is assumed to be the length of the variable at this point, so will use accordingly + end = epoch; + + //exchange the positions of the first and last var in an array while slowly moving to the center + //i.e + //on first pass of the while look will turn [1, 2, 3, 4] into [4, 2, 3, 1] + //and the second will turn [4, 3, 2, 1] + // + //I opted to use this algorithm to not create any more data + while (start < end) { + tmp = tmpArr[start]; + tmpArr[start] = tmpArr[end]; + tmpArr[end] = tmp; + start = start + 1; + end = end - 1; + } + //finally, output the reversed array and dump it into a file + for(int i=0;i<epoch+1;i++) { + cout << tmpArr[i] << endl; + outFile << tmpArr[i] << endl; + } + + return 0; + } else { + cout << "ERROR. Input File doesn't exist!"; + return 1; + + } +} +/* + This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License Version 3 ONLY as published by the Free Software Foundation. + + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. + */ |