diff options
| author | Miles-Cell <[email protected]> | 2024-03-04 14:53:08 -0800 |
|---|---|---|
| committer | Miles-Cell <[email protected]> | 2024-03-04 14:53:08 -0800 |
| commit | 36ff4440099a079d28af1eab41b492453bd6205b (patch) | |
| tree | 0cbbbaac1a51d1f422b6ec2776ad7d9b3011475f | |
| parent | Exercise completed. (diff) | |
| download | homework-3-miles-cell-develop.tar.xz homework-3-miles-cell-develop.zip | |
Exercise corrected, completed and pushed.develop
| -rw-r--r-- | Homework 3/Homework 3/Header.h | 41 | ||||
| -rw-r--r-- | Homework 3/Homework 3/Source.cpp | 52 |
2 files changed, 41 insertions, 52 deletions
diff --git a/Homework 3/Homework 3/Header.h b/Homework 3/Homework 3/Header.h index 8cff6ae..692e9f6 100644 --- a/Homework 3/Homework 3/Header.h +++ b/Homework 3/Homework 3/Header.h @@ -1,15 +1,38 @@ #ifndef MAIN_H #define MAIN_H +#include <iostream> #include <string> -void printMessage(const std::string& message); -int add(int a, int b); -double multiply(double x, double y); - - - - - -#endif // MAIN_H +// Factorial Calculation +long factorial(int n) { + if (n == 0) + return 1; + else + return n * factorial(n - 1); +} + +// Fibonacci Sequence +long fibonacci(int n) { + if (n <= 1) + return n; + else + return fibonacci(n - 1) + fibonacci(n - 2); +} + +// Power Function +long power(int A, int b) { + if (b == 0) + return 1; + else + return A * power(A, b - 1); +} + +// Comments on the use of size_t: +// size_t could be used for the parameters of the factorial and fibonacci functions +// since these values are expected to be non-negative. However, for simplicity and +// to keep the parameter types consistent across functions, int is used here. + + +#endif MAIN_H diff --git a/Homework 3/Homework 3/Source.cpp b/Homework 3/Homework 3/Source.cpp index 7c1c444..34f0c01 100644 --- a/Homework 3/Homework 3/Source.cpp +++ b/Homework 3/Homework 3/Source.cpp @@ -3,50 +3,16 @@ // Class: CST 116 // Assignment: Homework 3 +#include "Header.h" -#include <iostream> +int main() { + // Example usage of the functions + int n = 5; + int A = 2, b = 3; -#include <string> - -using namespace std; - -void printMessage(const std::string& message); -int add(int a, int b); -double multiply(double x, double y); -int fibonacci(int n); - -int main() -{ - int nthTerm = 10; - - int result = fibonacci(nthTerm); - - cout << "The " << nthTerm << "th term of the Fibonacci sequence is: " << result << endl; + std::cout << "Factorial of " << n << " is: " << factorial(n) << std::endl; + std::cout << "Fibonacci of " << n << " is: " << fibonacci(n) << std::endl; + std::cout << "Power of " << A << " raised to " << b << " is: " << power(A, b) << std::endl; return 0; -} - - -int fibonacci(int n) -{ - if (n <= 1) - { - return n; - } - - return fibonacci(n - 1) + fibonacci(n - 2); -} - -double power(double A, int b) -{ - double x = 1; - - for (int i = 0; i < b; ++i) - { - x *= A; - } - - return x; -} - - +}
\ No newline at end of file |