diff options
author | msglm <msglm@techchud.xyz> | 2023-01-14 05:31:48 -0600 |
---|---|---|
committer | msglm <msglm@techchud.xyz> | 2023-01-14 05:31:48 -0600 |
commit | 9d53d8857eaa1c9405894a88ca75bc4657e42f35 (patch) | |
tree | eb1efc1d028b949dd83bb710c68be8eff58f26e7 /C++/Arrays | |
download | school-code-master.tar.gz school-code-master.tar.bz2 school-code-master.zip |
Diffstat (limited to 'C++/Arrays')
-rw-r--r-- | C++/Arrays/Files, Functions and Arrays bloatless version.cpp | 78 | ||||
-rw-r--r-- | C++/Arrays/Files, Functions and Arrays.cpp | 111 | ||||
-rwxr-xr-x | C++/Arrays/bloat | bin | 0 -> 26032 bytes | |||
-rwxr-xr-x | C++/Arrays/bloatless | bin | 0 -> 18232 bytes | |||
-rw-r--r-- | C++/Arrays/in.txt | 10 | ||||
-rw-r--r-- | C++/Arrays/instructions | 36 | ||||
-rw-r--r-- | C++/Arrays/out.txt | 19 |
7 files changed, 254 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/>. + */ diff --git a/C++/Arrays/Files, Functions and Arrays.cpp b/C++/Arrays/Files, Functions and Arrays.cpp new file mode 100644 index 0000000..2ce808a --- /dev/null +++ b/C++/Arrays/Files, Functions and Arrays.cpp @@ -0,0 +1,111 @@ +// Name: msglm +// Date: August 25th 2022 +// Program Name: Files, Functions and Arrays +// Description: Create a program that reads in an unknown number of ATU class names (such as Programming 2) from an input file (see explanation of unknown number below). Then, output those class names to both the screen and an output file - output once in the order in which the data was read in from the file and again in reverse order. + + + +#include <iostream> +#include <fstream> +#include <string> +using namespace std; + +// Named constants +const int ARR_SIZE = 50; //Personally, i'd pass this as an env var or a flag + + +//Global Variable declaration +// +//Usually this is bad practice, but the function requirement for this has forced me to partake in this +//I could have done some mess with passing by reference, but every function was assumed to share the same data +//(I wrote it all in main first and then fragmented it), so this is the best way I could find to reach requirement +// +//Something like this should never reach production ever. + +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 {}; //both counts up and is the length of the array. + +//Vars necessary to invert an array +int start {}; +string tmp; +int arrEnd {}; //Conflicts with namespace, hence the name + +//It's a shame this isn't STDIN and STDOUT +ifstream inFile; +ofstream outFile; + + +//Functions are pretty unncessary for this code and overcomplicate more than they help +//The original code for this before I split it into 5 functions was a cute little 40 SLOC thing. + +bool fileFound(string location) { + ifstream testFile; + testFile.open(location); + + if (testFile) { + return true; + } else { + return false; + } +} + +void orderArray() { + //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) { + epoch = epoch + 1; + } + +} + +void displayNormalOrderArray() { + int i = 0; + while(i<epoch){ + cout << tmpArr[i] << endl; + outFile << tmpArr[i] << endl; + i++; + } + +} + +void reverseOrderArray() { + //the skip of the last digit is purposeful, the other loop covers it + for(int i=epoch;i>=0;i--) { + cout << tmpArr[i] << endl; + outFile << tmpArr[i] << endl; + } +} + +void closeFiles() { + inFile.close(); + outFile.close(); +} + +int main() { + + //Var assignments + + // User input + if (fileFound("in.txt")) { + + inFile.open("in.txt"); + orderArray(); + outFile.open("out.txt"); + displayNormalOrderArray(); + reverseOrderArray(); + closeFiles(); + + 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/>. + */ diff --git a/C++/Arrays/bloat b/C++/Arrays/bloat Binary files differnew file mode 100755 index 0000000..e39fad8 --- /dev/null +++ b/C++/Arrays/bloat diff --git a/C++/Arrays/bloatless b/C++/Arrays/bloatless Binary files differnew file mode 100755 index 0000000..98d671b --- /dev/null +++ b/C++/Arrays/bloatless diff --git a/C++/Arrays/in.txt b/C++/Arrays/in.txt new file mode 100644 index 0000000..f00c965 --- /dev/null +++ b/C++/Arrays/in.txt @@ -0,0 +1,10 @@ +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 diff --git a/C++/Arrays/instructions b/C++/Arrays/instructions new file mode 100644 index 0000000..d09b59d --- /dev/null +++ b/C++/Arrays/instructions @@ -0,0 +1,36 @@ + +Project 1: Files, Functions and Arrays +Due: Friday, August 26th, 2022 before midnight +100 Points +Problem Statement: +Create a program that reads in an unknown number of college-name class names (such as Programming 2) from an input file (see explanation of unknown number below). Then, output those class names to both the screen and an output file - output once in the order in which the data was read in from the file and again in reverse order. +Step-by-step instructions: + + Create an array variable to store string values. Make this array variable so it can store up to 50 values in it. When declaring that array variable, use a constant int to allocate the size. + Create an input file variable to read in the class name data. Create an output file variable to store output + Use a loop of your choice to read in an unknown number of values from your input file and store that data into the array variable you created in step 1 (unknown number values: this means the number of data items stored in the file size is not static. It can change each time the program executes) + Use getline to read in string values from your input file so you may capture values with spaces in them + After reading in the class names from the input file and storing them into your array variable, output them to both the screen and an output file in the original order and again in reverse order + Use functions to do the following: + Check to see if the file was found + Read in the class names + Output the class names to the screen in the original order + Output the class names in reverse order + Close the files + +Be sure to use comments in your program: Name, Program Description, Date and anywhere else in the program you deem necessary. +***NOTE*** Here is an explanation of unknown number of records - Example: Let's say we want to read in from an input file the names of students attending class each class day. The number of students attending class varies because students sometimes miss class. Therefore, we do not know how many names will be read into our program when we take roll. Therefore, we need to build our system to handle this issue. In the assignment I asked you to create, you do not know how many class names will be inside of the input file. It could be 2 or 5 or 20. We don't know. So, you will need to write you program to handle this problem. Ths is simply using a while loop to read until the end of file is reached. I discuss how to do this in the video lectures on files. The only real problem is if the number of records read in from the input file exceeds the the array allocation. Example: string classNames[10]; and I try to read in 20 class names. This is not possible. To prevent this issue, you need to set your allocation to a value that likely be higher so this issue does not occur. +If you are stuck, no worries. I will help in class and through email. +Grading Rubric: + + If you do not include comments at the top of the program (name, program description, date), you will lose 15 points + If you do not use functions, you will use 10 points per missing function (50 points possible) + If your program does not compile (run), then I will give a grade of 0/100. But will give you the change to repair for points back (some points are better than none) + If your program is late (within 48 hours of the due date), you will lose 25 points + If your program is not formatted nicely (code all over the place, ugly), you will lose up to 25 points depending on the severity + If your program stops working when I run it, you will lose points. The amount depends on the severity of the error + If your program is identical to another student's work, I will contact both students for a conference where you may both receive a 0/100 for your project + If your program looks like a a professional programmer wrote it, I will contact you to determine if you did indeed write the program or copy off of the internet, you may receive a 0/100 for copying + This is just a list of typical issues I run into when grading to give you some idea of where your points go. Points can be taken off for other reasons. + + diff --git a/C++/Arrays/out.txt b/C++/Arrays/out.txt new file mode 100644 index 0000000..80c0dcc --- /dev/null +++ b/C++/Arrays/out.txt @@ -0,0 +1,19 @@ +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +9 +8 +7 +6 +5 +4 +3 +2 +1 |