CSCI 162 – Lab 10
NOTES:
- Feel free to use your laptop if you have it.
- Ensure I have recorded your completion. Failure to do so will result in a grade of 0.
- You may want to jump to section Recursion Visualization to start the lab. Especially the 2nd link.
Scrap Paper
1. Get s crap piece of paper and a pencil/pen.
2. Use this to scratch ideas down.
3. This is worth marks.
Recursion Visualization:
Check out the following websites that help you visualize recursion:
o https://visualgo.net/en/recursion?slide=1
o https://cscircles.cemc.uwaterloo.ca/java_visualize/#
This one is in Java, but it’s similar enough to C++ four our situation
Try adding the following code and running the visualizer.
public class Recursion {
public static int sum(int n) {
if (n == 1) {
return 1;
}
return n + sum(n - 1);
}
public static void main(String[] args) {
sum(5);
}
}
Sum
Write an iterative function that will take a single number n and return the sum of all numbers between 1 and n
inclusively.
Write a recursive function that will take a single number n and return the sum of all numbers between 1 and n
inclusively. Have this function written in a way where the base case is n == 1.
o The function will start with n and work down towards the base case.
Write a recursive function that will take two numbers cur and n and return the sum of all numbers between 1 and
n inclusively. Have this function written in a way where the base case is cur == n.
o This function will start with 1 and work up towards the base case.
Factorial
Write an iterative function that will take a single number n and return the factorial of that number.
Write a recursive function that will take a single number n and return the factorial of that number. Have this function
written in a way where the base case is n == 1.
o The function will start with n and work down towards the base case.
Write a recursive function that will take two numbers cur and n and return the factorial of n. Have this function
written in a way where the base case is cur == n.
o This function will start with 1 and work up towards the base case.
Linear Search on Arrays
Write an iterative function to do a linear search. The function should take an array of integers, the length of the
array, and a target integer. The function will return true or false.
Write a recursive function to do a linear search. This function should take an array of integers, the length of the array,
a target integer, and a current index.
o Try to figure this one out without Googling the answer.
o You are strongly encouraged to work with your neighbours.
o Also, you better be using your scrap paper. only
o Be aware that there is no rule saying that you can have only 1 base case.
Building a Linked Structure
Write a recursive function that takes a number n and a counter cur and it returns a pointer to the head of the
structure containing the numbers 1 – n (one number in each node). The head of the structure should contain the
number 1 (not 5).
o Use cur to count up towards the value n (like you did for the sum and fact).
o You’ll need a Node class for this.
Write your own or download one from Moodle.
If you’re feeling fancy, make a Node structure.
This will actually be easier in the long run.
Linear Search on Linked Structure
Write a recursive function that takes a pointer to a node and a target and performs a linear search on the linked
structure. The function will return true or false.
Kattis Problems
1. Go work on Kattis problems.
2. If you want to try something different, check out LeetCode: https://leetcode.com/problemset/
3. Recommended Kattis Problems (don’t worry, you don’t need to do all of them or anything):
a. https://open.kattis.com/problems/quadrant
b. https://open.kattis.com/problems/judgingmoose
c. https://open.kattis.com/problems/timeloop
d. https://open.kattis.com/problems/oddities
e. https://open.kattis.com/problems/fizzbuzz
f. https://open.kattis.com/problems/twostones
g. https://open.kattis.com/problems/spavanac
h. https://open.kattis.com/problems/cetvrta
i. https://open.kattis.com/problems/bus
j. https://open.kattis.com/problems/timeloop
k. https://open.kattis.com/problems/oddities
l. https://open.kattis.com/problems/fizzbuzz
m. https://open.kattis.com/problems/sibice
n. https://open.kattis.com/problems/datum
o. https://open.kattis.com/problems/dicecup
p. https://open.kattis.com/problems/autori
q. https://open.kattis.com/problems/apaxiaaans
r. https://open.kattis.com/problems/hissingmicrophone
s. https://open.kattis.com/problems/trik
t. https://open.kattis.com/problems/pot
u. https://open.kattis.com/problems/filip
v. https://open.kattis.com/problems/reversebinary
w. https://open.kattis.com/problems/sevenwonders
x. https://open.kattis.com/problems/zamka
y. https://open.kattis.com/problems/bijele
z. https://open.kattis.com/problems/cold
aa. https://open.kattis.com/problems/nastyhacks
bb. https://open.kattis.com/problems/grassseed
cc. https://open.kattis.com/problems/pet
dd. https://open.kattis.com/problems/batterup
ee. https://open.kattis.com/problems/aboveaverage
ff. https://open.kattis.com/problems/icpcawards
gg. https://open.kattis.com/problems/quickbrownfox
hh. https://open.kattis.com/problems/nodup
ii. https://open.kattis.com/problems/conundrum
jj. https://open.kattis.com/problems/bela
kk. https://open.kattis.com/problems/kornislav