commit 32136c887cd17ca904b2637b0b46aa6aa55bef6b4307a197772d809ef65ba06b Author: Tyler Beckman Date: Fri Sep 20 00:55:19 2024 -0600 upload diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..357fb81 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..adfbf73 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/ty/Dev/School/CSCI200/Sets/2/L2A", + "program": "/home/ty/Dev/School/CSCI200/Sets/2/L2A/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..6e42470 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..342f77b --- /dev/null +++ b/Makefile @@ -0,0 +1,68 @@ +TARGET = L2A +SRC_FILES = main.cpp + +# Tyler's custom makefile extensions +.DEFAULT_GOAL := all # Necessary so `make` doesn't run the "pack" target, as it is declared before "all" +.PHONY: pack clean-run c run + +## Adds only the necessary files for build into a .tar.gz file, named appropriately +ARCHIVED_FILES = Makefile $(SRC_FILES) $(SRC_FILES:.cpp=.h) $(SRC_FILES:.cpp=.hpp) +pack: + tar --ignore-failed-read -czvf $(TARGET).tar.gz $(shell echo $(ARCHIVED_FILES) | xargs ls -d 2>/dev/null) + +## Runs the pack target and then attempts to build & run the program to make sure it functions correctly +pack-test: pack + $(eval TMP := $(shell mktemp -d)) + tar -xvzf $(TARGET).tar.gz --directory $(TMP) + make -C $(TMP) + $(TMP)/$(TARGET) + rm -rf $(TMP) + +## An extension of the clean command that is shorter to type and removes a potential .tar.gz file +c: clean + $(DEL) -f $(TARGET).tar.gz + +## Simply builds and then executes the program +run: all + ./$(TARGET) + +# NO EDITS NEEDED BELOW THIS LINE + +CXX = g++ +CXXFLAGS = -O2 +CXXFLAGS_DEBUG = -g +CXXFLAGS_WARN = -Wall -Wextra -Wunreachable-code -Wshadow -Wpedantic +CPPVERSION = -std=c++17 + +OBJECTS = $(SRC_FILES:.cpp=.o) + +ifeq ($(shell echo "Windows"), "Windows") + TARGET := $(TARGET).exe + DEL = del + Q = +else + DEL = rm -f + Q = " +endif + +all: $(TARGET) + +$(TARGET): $(OBJECTS) + $(CXX) -o $@ $^ + +.cpp.o: + $(CXX) $(CXXFLAGS) $(CPPVERSION) $(CXXFLAGS_DEBUG) $(CXXFLAGS_WARN) -o $@ -c $< + +clean: + $(DEL) -f $(TARGET) $(OBJECTS) Makefile.bak + +depend: + @sed -i.bak '/^# DEPENDENCIES/,$$d' Makefile + @$(DEL) sed* + @echo $(Q)# DEPENDENCIES$(Q) >> Makefile + @$(CXX) -MM $(SRC_FILES) >> Makefile + +.PHONY: all clean depend + +# DEPENDENCIES +main.o: main.cpp diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..5c66611 --- /dev/null +++ b/main.cpp @@ -0,0 +1,389 @@ +/* CSCI 200: Fix Loop and Function Errors + * + * Author: Tyler Beckman + * + * Description: + * This program illustrates a variety of common loop and function + * errors. I have fixed the errors in each section. + * Note on style guide requirement: The original code used 4 spaces, so I + * reformatted most of it with clang-format. + * + * Copyright 2024 Dr. Jeffrey Paone + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + */ + +#include // to query the current time +#include // for cout, cin, endl etc. +#include +#include // for mt19937 generator +using namespace std; + +#include + +/** + * @brief Prints a smiley face into the standard out + */ +void print_smiley_face(void) { cout << ":)" << endl; } + +/** + * @brief adds five to the given parameter + * @param x integer to be added to + * @returns parameter plus five + */ +int add_five(int x) { return x + 5; } + +/** + * @brief generates a single random integer number within the expected range + * @param MIN lower inclusive bound of range + * @param MAX upper inclusive bound of range + * @returns random integer within the range [MIN, MAX] + */ +int generate_random_integer(const int MIN, const int MAX) { + // create the Mersenne Twister generator + mt19937 mt( + (unsigned int)chrono::steady_clock::now().time_since_epoch().count()); + uniform_int_distribution intDist(MIN, MAX); + + return intDist(mt); +} + +/** + * @brief generates a single random floating point number within the expected + * range + * @param MIN lower inclusive bound of range + * @param MAX upper inclusive bound of range + * @return random float within the range [MIN, MAX] + */ +float generate_random_float(const float MIN, const float MAX) { + // create the Mersenne Twister generator + mt19937 mt( + (unsigned int)chrono::steady_clock::now().time_since_epoch().count()); + uniform_real_distribution floatDist( + MIN, std::nextafter(MAX, std::numeric_limits::max())); + + return floatDist(mt); +} + +/** + * @brief sums three integer values + * @param x first value to be added + * @param y second value to be added + * @param z third value to be added + * @returns the sum of all three parameters + */ +int sum(int x, int y, int z) { return x + y + z; } + +/** + * @brief multiples two floats together + * @param a first value to multiply + * @param b second value to multiply + * @returns the product of the two float parameters + */ +float multiply(float a, float b) { return a * b; } + +/** + * @brief runs test summing numbers in the range [1, N] + */ +void loop_test_1() { + // FIX = Give "sum" an initial value, adjust the loop condition to be + // inclusive TESTS: + + cout << endl; + cout << "******************" << endl; + cout << "Section Looping 1 " << endl; + cout << "******************" << endl; + + int n; + cout << "Enter a number greater than 1 to sum up to: "; + cin >> n; + + int sum = 0; + for (int i = 1; i <= n; ++i) { + sum += i; + } + cout << "The sum of the numbers from 1 to " << n << " (inclusive) is: " << sum + << endl; +} + +/** + * @brief runs test summing set of prices entered by the user + */ +void loop_test_2() { + // FIX = Move totalPrice = 0 outside of the loop + // TESTS: + + cout << endl; + cout << "******************" << endl; + cout << "Section Looping 2 " << endl; + cout << "******************" << endl; + + int numItems; + cout << "How many items do you have? "; + cin >> numItems; + cout << endl; + + int counter = 1; + float totalPrice = 0, price; + while (counter <= numItems) { + cout << "Enter the price of item " << counter << ": "; + cin >> price; + totalPrice += price; + counter++; + } + cout << "The total price is: " << totalPrice << endl; +} + +/** + * @brief runs test summing numbers in the range [1, N] + */ +void loop_test_3() { + // FIX = Loop based on the n variable rather than the sum variable, increment + // the counter variable TESTS: + + cout << endl; + cout << "******************" << endl; + cout << "Section Looping 3 " << endl; + cout << "******************" << endl; + + int n; + cout << "What number do you wish me to sum to?" << endl; + cin >> n; + + int sum = 0, counter = 1; + do { + sum += counter++; + cout << "Sum so far: " << sum << endl; + } while (counter <= n); + + cout << endl << "Section III Recap" << endl; + + cout << "I calculated the sum of numbers from 1 to " << n + << " (inclusive) as " << sum << endl; +} + +/** + * @brief runs test summing i^2 in the range [1, N] + */ +void loop_test_4() { + // FIX = Switch increment in for loop to a decrement + // TESTS: + + cout << endl; + cout << "******************" << endl; + cout << "Section Looping 4 " << endl; + cout << "******************" << endl; + + cout << "I will now calculate the sum of squares from 1 to N (inclusive)" + << endl; + + int n; + cout << "Enter N: "; + cin >> n; + + int sum = 0; + for (int i = n; i > 0; --i) { + sum += i * i; + } + + cout << "The sum of squares from 1 to " << n << " is: " << sum << endl; +} + +/** + * @brief runs test summing i^3 in the range [1, N] + */ +void loop_test_5() { + // FIX = Adjust loop condition to loop until counter is n (inclusive), move + // counter increment inside of loop TESTS: + + cout << endl; + cout << "******************" << endl; + cout << "Section Looping 5 " << endl; + cout << "******************" << endl; + + cout << "I will now calculate the sum of cubes from 1 to N (inclusive)" + << endl; + + int n; + cout << "Enter N: "; + cin >> n; + + int sum = 0, counter = 1; + while (counter <= n) { + sum += (counter * counter * counter); + counter++; + } + + cout << "The sum of cubes from 1 to " << n << " is: " << sum << endl; +} + +/** + * @brief runs test using function to add five to an entered value + */ +void function_test_1() { + // FIX = + // TESTS: + + cout << endl; + cout << "******************" << endl; + cout << "Section Function 1" << endl; + cout << "******************" << endl; + + int numTrees; + cout << "How many trees do you currently have? "; + cin >> numTrees; + cout << "There are initially " << numTrees << " trees." << endl; + numTrees = add_five(numTrees); + cout << "We planted five trees, there are now " << numTrees << " trees." + << endl; +} + +/** + * @brief runs test to call a function that outputs a smiley face to the + * standard out + * + */ +void function_test_2() { + // FIX = Implement the smiley face print function + // TESTS: + + cout << endl; + cout << "******************" << endl; + cout << "Section Function 2" << endl; + cout << "******************" << endl; + + print_smiley_face(); +} + +/** + * @brief runs test to generate five random integers within a provided range + */ +void function_test_3() { + // FIX = Use uniform_int_distribution in the random int function + // TESTS: + + cout << endl; + cout << "******************" << endl; + cout << "Section Function 3" << endl; + cout << "******************" << endl; + + int min, max; + cout << "Enter the minimum integer range value: "; + cin >> min; + cout << "Enter the maximum integer range value: "; + cin >> max; + + cout << "Five different random numbers are: " << endl; + for (int i = 0; i < 5; i++) { + cout << "\t" << generate_random_integer(min, max) << endl; + } +} + +/** + * @brief runs test to use function to sum three values + */ +void function_test_4() { + // FIX = Pass the 3rd number to the sum function + // TESTS: + + cout << endl; + cout << "******************" << endl; + cout << "Section Function 4" << endl; + cout << "******************" << endl; + + int num1, num2, num3; + cout << "Enter three integer numbers: "; + cin >> num1 >> num2 >> num3; + cout << "The sum of all three is " << sum(num1, num2, num3) << endl; +} + +/** + * @brief runs test to generate five random floats within a provided range + */ +void function_test_5() { + // FIX = Use uniform_real_distribution in the random float function, and call + // it rather than using its value TESTS: + + cout << endl; + cout << "******************" << endl; + cout << "Section Function 5" << endl; + cout << "******************" << endl; + + float min, max; + cout << "Enter the minimum float range value: "; + cin >> min; + cout << "Enter the maximum float range value: "; + cin >> max; + cout << "Five different random floats are: " << endl; + for (int i = 0; i < 5; ++i) { + cout << "\t" << generate_random_float(min, max) << endl; + } +} + +/** + * @brief runs test to use function to multiply two provided numbers + */ +void function_test_6() { + // FIX = Implement the multiply function rather than just declaring it + // TESTS: + + cout << endl; + cout << "******************" << endl; + cout << "Section Function 6" << endl; + cout << "******************" << endl; + + float leftHandSide, rightHandSide; + cout << "Enter two floats: "; + cin >> leftHandSide >> rightHandSide; + cout << "Their product is: " << multiply(leftHandSide, rightHandSide) << endl; +} + +int main() { + cout << "Welcome To Looping Function World!" << endl; + + loop_test_1(); // run looping test 1 + loop_test_2(); // run looping test 2 + loop_test_3(); // run looping test 3 + loop_test_4(); // run looping test 4 + loop_test_5(); // run looping test 5 + + function_test_1(); // run function test 1 + function_test_2(); // run function test 2 + function_test_3(); // run function test 3 + function_test_4(); // run function test 4 + function_test_5(); // run function test 5 + function_test_6(); // run function test 6 + + cout << endl; + cout << "******************" << endl; + cout << "Section Done" << endl; + cout << "******************" << endl; + + cout << endl + << "Congrats! You fixed them all (hopefully correctly!)" << endl + << endl + << "Goodbye" << endl + << endl; + + return 0; +}