From 9d53d8857eaa1c9405894a88ca75bc4657e42f35 Mon Sep 17 00:00:00 2001 From: msglm Date: Sat, 14 Jan 2023 05:31:48 -0600 Subject: Inital Commit --- ...les, Functions and Arrays bloatless version.cpp | 78 ++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 C++/Arrays/Files, Functions and Arrays bloatless version.cpp (limited to 'C++/Arrays/Files, Functions and Arrays bloatless version.cpp') 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 +#include +#include +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. + */ -- cgit v1.2.3