Lab 9

You cannot submit for this problem because the homework's deadline is due.

EECS 280 Lab 09: Recursion

Lab Due Thursday, June 27, 2024, 11:59 pm

In this lab, we will practice recursion by implementing functions using iteration (loops) and recursion. You will also write tail recursive functions to understand their relationship to iterative solutions.

Completion Criteria/Checklist:

To pass this lab, you must finish tasks 1 and 2.

  • Implement hailstone and hailstone_iter.

  • Implement count_digits, count_digits_iter, and count_digits_tail.

Lab Exercises

The Files

Here's a summary of this lab's files.

File Description
lab09.h Contains the hailstone and count_digit function declarations.
lab09.cpp Contains the hailstone and count_digit function definitions.
main.cpp Contains the main function that runs testing code.

Testing Code

The main function in main.cpp contains some testing code we've written for you, which will print the results produced by your code.

The starter code should compile successfully without any modifications, so make sure you are able to compile and run it with the following commands. The code may be missing some pieces, contain some bugs, or crash when you run it, but you'll fix each throughout the course of the lab.

$ g++ -Wall -Werror -g -pedantic --std=c++17 lab09.cpp main.cpp -o lab09.exe
$ ./lab09.exe

Introduction

Task 1 - Hailstone Sequence

Pick any positive integer n. If n is even, compute n/2. If n is odd, compute 3n+1. Take the result as the new n and continue the process, but stop if you get to 1. For example, if we start at n = 7 the sequence is:

7, 22, 11, 34 ,17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1

Such a sequence of numbers is called a hailstone sequence. (Any guesses why?)

The Collatz conjecture states that every hailstone sequence eventually ends in 1. The conjecture remains unproven to this day, but most mathematicians believe it to be true.

Your task is to write functions that print the hailstone sequence for a given value of n by filling in the function stubs provided in lab09.cpp. Your functions should print the whole sequence on a single line, with a space after each element (including the last one). Notice that hailstone must be recursive while hailstone_iter must use iteration.

Hint: Use the recurrence relation given above as a model for your recursive function.

Task 2 - Digit Counting

Next you'll write a function that counts the number of times a digit appears in a number. For example, the digit 2 appears in the number 20120130 two times. You must write three variations of this function, once using "regular" recursion (count_digits), once using iteration (count_digits_iter) and once using a tail recursive helper function (count_digits_tail). Again, just fill in the function stubs in lab09.cpp with your code.

Hint: n % 10 gives you n's last digit (134 % 10 = 4), while n / 10 removes n's last digit (134 / 10 = 13).

Lab 9

Not Claimed
Status
Finished
Problems
1
Open Since
2024-06-25 00:00
DDL
2024-06-27 23:59
Extension
72.0 hour(s)