This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
notes:discrete:fall2023:projects:mor0 [2023/11/09 00:41] – [Recursion] wgates1 | notes:discrete:fall2023:projects:mor0 [2023/11/16 01:24] (current) – [Matrix] cfoster8 | ||
---|---|---|---|
Line 3: | Line 3: | ||
=====Matrix===== | =====Matrix===== | ||
+ | A matrix is simply data stored within a grid. It can be thought of as a multi-dimensional array, or "an array of arrays" | ||
+ | And the initialization would look something like this | ||
+ | <code C> | ||
+ | int matrix[2][3] = { { 1, 7 }, { 2, 9, 6 } }; | ||
+ | </ | ||
+ | |||
+ | When accessing data within a matrix imagine the matrix as a coordinate grid, and the numbers you give the matrix is the coordinate point of that data within the matrix. | ||
+ | |||
+ | Like arrays, Matrices are fixed in size. You allocate the memory for it when created and it cannot be changed. | ||
+ | |||
+ | In theory, a matrix can have as many dimensions as you want. As many times as you can nest an array in an array, a matrix of that many dimensions can be created. Because that's all a matrix is, at least this type of matrix, an array of arrays to the nth degree. | ||
=====Recursion===== | =====Recursion===== | ||
The concept of recursion is actually simple but implementing it is more challenging than you might think. Recursion works by creating a function that calls itself until it hits a base case to end/ | The concept of recursion is actually simple but implementing it is more challenging than you might think. Recursion works by creating a function that calls itself until it hits a base case to end/ | ||
A simple implementation of this can be seen with a function that gets the factorial value given a certain number. | A simple implementation of this can be seen with a function that gets the factorial value given a certain number. | ||
+ | |||
+ | Here is what that function looks like: | ||
+ | <code C> | ||
+ | // Factorial function using recursion | ||
+ | int factorial(int n) | ||
+ | { | ||
+ | // Base case for when the number given is 0 | ||
+ | if (n == 0) | ||
+ | { | ||
+ | return 1; | ||
+ | } | ||
+ | |||
+ | // Takes the given value and multiplies it by the previous factorial value | ||
+ | else | ||
+ | { | ||
+ | // Function calls itself (calls n-1) until it reaches the base case (0) | ||
+ | return n * factorial(n - 1); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | As you can see the function starts with the base case and then actually implements the logic for the function. Let's give n a value of 3. First, we are going to check if n is 0 and when it fails that check it will return 3*factorial(2), | ||
+ | |||
+ | ====Properties of Recursion==== | ||
+ | |||
+ | Recursion can cause issues when not handled properly. The easiest way for this to happen is to cause a **stack overflow**. | ||
+ | |||
+ | A stack overflow occurs when a recursive function is never properly broken and repeats indefinitely until memory (the stack) is used up. This happens when the base case condition check is faulty or nonexistent. | ||
+ | |||
+ | ====Why Use Recursion Instead of a Loop?==== | ||
+ | |||
+ | There are several differences between loops and recursion. A couple of them being: | ||
+ | * Loop uses repetition to go over sequential data, while recursion uses a selection structure. | ||
+ | * Loops are less memory and processor intensive than recursive functions. | ||
+ | |||
+ | Recursion does have its advantages over loops though. | ||
+ | * The code is easier to understand and is generally shorter. | ||
+ | * Recursion can solve some problems that normal loops can not. | ||