diff --git a/meson.build b/meson.build index 16942b1..1c9141f 100644 --- a/meson.build +++ b/meson.build @@ -7,3 +7,14 @@ project( c_1_lab_1 = executable('c_1_lab_1', 'src/chapter_1/c_1_lab_1.cpp', install: true) c_1_ice_1 = executable('c_1_ice_1', 'src/chapter_1/c_1_ice_1.cpp', install: true) + +c_2_lab_1 = executable('c_2_lab_1', 'src/chapter_2/lab_1/main.cpp', install: true) +c_2_lab_1_arg = executable( + 'c_2_lab_1_arg', + 'src/chapter_2/lab_1/main.cpp', + install: true, + cpp_args: ['-DCHECK_ARG'], +) + +c_2_lab_2 = executable('c_2_lab_2', 'src/chapter_2/lab_2/main.cpp', install: true) +c_2_lab_3 = executable('c_2_lab_3', 'src/chapter_2/lab_3/main.cpp', install: true) diff --git a/src/chapter_2/lab_1/lab2part1.dat b/src/chapter_2/lab_1/lab2part1.dat new file mode 100644 index 0000000..c601b1d --- /dev/null +++ b/src/chapter_2/lab_1/lab2part1.dat @@ -0,0 +1,3 @@ +10 20 30 +56 6 8 9 11 +12 34 87 3 \ No newline at end of file diff --git a/src/chapter_2/lab_1/lab2part1a.txt b/src/chapter_2/lab_1/lab2part1a.txt new file mode 100644 index 0000000..7aaacb2 --- /dev/null +++ b/src/chapter_2/lab_1/lab2part1a.txt @@ -0,0 +1 @@ +1 2 3 4 5 6 -1 -2 -3 diff --git a/src/chapter_2/lab_1/main.cpp b/src/chapter_2/lab_1/main.cpp new file mode 100644 index 0000000..d0ea09f --- /dev/null +++ b/src/chapter_2/lab_1/main.cpp @@ -0,0 +1,62 @@ +#include //needed for exit function +#include +#include + +using namespace std; + +// Place fileSum prototype (declaration) here +int fileSum(string filename); + +int main(int argc, char *argv[]) { + + string filename; + +#ifdef CHECK_ARG // This is a flag to the compiler to include this code if + // CHECK_ARG is defined + if (argc < 2) { + cout + << "Please enter a file on the command line\n"; // Too little arguements + exit(1); + } + if (argc > 2) { + cout << "Too many arguements! Only 1 file is expected\n"; // Too many + // arguements + exit(1); + } + filename = + argv[1]; // set the file name to the second element in the command line +#endif +#ifndef CHECK_ARG // This is a flag to the compiler to include this code if + // CHECK_ARG is NOT defined + cout << "Enter the name of the input file: "; + cin >> filename; // set the file name to what the user inputs (interactive) + cout << "\n"; +#endif + + int sum = 0; + sum = fileSum(filename); + + cout << "Sum: " << sum << endl; + + return 0; +} + +// Place fileSum implementation here +int fileSum(string filename) { + fstream file; + file.open(filename); + + if (!file.is_open()) { + cout << "Error opening " << filename << endl; + exit(EXIT_FAILURE); + } + + int num; + int sum = 0; + + while (file >> num) { + sum += num; + } + + return sum; +} diff --git a/src/chapter_2/lab_2/alpha.txt b/src/chapter_2/lab_2/alpha.txt new file mode 100644 index 0000000..e85d5b4 --- /dev/null +++ b/src/chapter_2/lab_2/alpha.txt @@ -0,0 +1 @@ +abcdefghijklmnopqrstuvwxyz \ No newline at end of file diff --git a/src/chapter_2/lab_2/main.cpp b/src/chapter_2/lab_2/main.cpp new file mode 100644 index 0000000..684a3f3 --- /dev/null +++ b/src/chapter_2/lab_2/main.cpp @@ -0,0 +1,47 @@ +#include +#include +#include + +using namespace std; + +// Place charCnt prototype (declaration) here +int charCnt(string filename, char ch); + +int main() { + string filename; + char ch; + int chCnt = 0; + + cout << "Enter the name of the input file: "; + cin >> filename; + cout << endl; + cout << "Enter a character: "; + cin.ignore(); // ignores newline left in stream after previous input statement + cin.get(ch); + cout << endl; + + chCnt = charCnt(filename, ch); + cout << "# of " << ch << "'s: " << chCnt << endl; + + return 0; +} + +// Place charCnt implementation here +int charCnt(string filename, char ch) { + fstream file = fstream(filename); + + if (!file.is_open()) { + cout << "Error opening " << filename << endl; + exit(EXIT_FAILURE); + } + + int count = 0; + char currentCh; + + while (file.get(currentCh)) { + if (currentCh == ch) + count++; + } + + return count; +} diff --git a/src/chapter_2/lab_3/data1.csv b/src/chapter_2/lab_3/data1.csv new file mode 100644 index 0000000..b63b079 --- /dev/null +++ b/src/chapter_2/lab_3/data1.csv @@ -0,0 +1 @@ +1,2,3,4,5,6 \ No newline at end of file diff --git a/src/chapter_2/lab_3/data2.csv b/src/chapter_2/lab_3/data2.csv new file mode 100644 index 0000000..2257a24 --- /dev/null +++ b/src/chapter_2/lab_3/data2.csv @@ -0,0 +1 @@ +6,4,8,10 \ No newline at end of file diff --git a/src/chapter_2/lab_3/main.cpp b/src/chapter_2/lab_3/main.cpp new file mode 100644 index 0000000..6ce2657 --- /dev/null +++ b/src/chapter_2/lab_3/main.cpp @@ -0,0 +1,79 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +int main(int argc, char *argv[]) { + + string inputFile; + string outputFile; + + // Assign to inputFile value of 2nd command line argument + // Assign to outputFile value of 3rd command line argument + inputFile = argv[1]; + outputFile = argv[2]; + + // Create input stream and open input csv file. + ifstream input(inputFile); + + // Verify file opened correctly. + // Output error message and return 1 if file stream did not open correctly. + if (!input.is_open()) { + cout << "Error opening " << inputFile << endl; + return 1; + } + + // Read in integers from input file to vector. + string line; + getline(input, line); + + vector values; + stringstream ss(line); + string value; + + while (getline(ss, value, ',')) { + values.push_back(stoi(value)); + } + + // Close input stream. + input.close(); + + // Get integer average of all values read in. + int sum = std::accumulate(values.begin(), values.end(), 0); + int average = sum / static_cast(values.size()); + + // Convert each value within vector to be the difference between the original + // value and the average. + for_each(values.begin(), values.end(), + [average](int &num) { num -= average; }); + + // Create output stream and open/create output csv file. + ofstream output(outputFile); + + // Verify file opened or was created correctly. + // Output error message and return 1 if file stream did not open + // correctly. + if (!output.is_open()) { + cout << "Error opening " << outputFile << endl; + return 1; + } + + // Write converted values into output csv file, each integer separated by + // a comma. + for (unsigned int i = 0; i < values.size(); i++) { + output << values[i]; + if (i < values.size() - 1) { + output << ","; + } + } + + // Close output stream. + output.close(); + + return 0; +} diff --git a/src/chapter_2/lab_3/out1.csv b/src/chapter_2/lab_3/out1.csv new file mode 100644 index 0000000..29e1630 --- /dev/null +++ b/src/chapter_2/lab_3/out1.csv @@ -0,0 +1 @@ +-2,-1,0,1,2,3 \ No newline at end of file