Description
- Write C++ programs
- Compile C++ programs
- Implement programs that use recursion to solve advanced problems
Additional Reading
This lab exercise requires an understanding of some concepts to solve the problems. You are strongly encouraged to read the following tutorials to help you answer the problems.
- Organizing C++ files: function prototypes, implementations, and drivers.
- Using objects as parameters and return values in functions
- Passing arrays as parameters to functions
- File reading and writing (also includes dealing with arrays)
Instructions
Answer the programming problems sequentially (i.e., answer prob01 before prob02). If you have questions let your instructor or the lab assistant know. You can also consult your classmates.
When you answer two programming problems correctly, let your instructor know and wait for further instruction.
Lab exercise guide
Here’s a link to the Lab exercise guide in case you need to review the lab exercise objectives, grading scheme, or evaluation process.
Count Odd
Create a program that counts all the odd numbers from 0 to a number using recursion. Your program should have a function called count_odd
 that takes in an int
 value and returns an int
 value. The user will be asked for a number, and the program will display the following output.
Sample Output:
Enter a number: 5 The number of odds from 0 to 5 is 3
Enter a number: 13 The number of odds from 0 to 13 is 7
Place the count_odd
‘s function prototype in count-odd.hpp
 and it’s implementation in count-odd.cpp
. The main
 function already contains some code, but you need to complete the requirements that is described inside the file.
Submission checklist
- Created function prototype and stored inÂ
.hpp
 file. - Created function implementation and stored inÂ
.cpp
 file (see reference). - Call function in the driver
- Compiled and ran the driver (
main
). - Manually checked for compilation and logical errors.
- Ensured no errors on the unit test (
make test
). - Followed advice from the stylechecker (
make stylecheck
). - Followed advice from the formatchecker to improve code readbility (
make formatcheck
).
Code evaluation
Open the terminal and navigate to the folder that contains this exercise. Assuming you have pulled the code inside of /home/student/labex02-tuffy
 and you are currently in /home/student
 you can issue the following commands
cd labex02-tuffy
You also need to navigate into the problem you want to answer. To access the files needed to answer problem 1, for example, you need to issue the following command.
cd prob01
When you want to answer another problem, you need to go back up to the parent folder and navigate into the next problem. Assuming you are currently in prob01
, you can issue the following commands to go to the parent folder then go into another problem you want to answer;Â prob02
 for example.
cd ..
cd prob02
Use the clang++
 command to compile your code and the ./
 command to run it. The sample code below shows how you would compile code save in count-odd.cpp
 and main.cpp
, and into the executable file main
. Make sure you use the correct filenames required in this problem. Take note that if you make any changes to your code, you will need to compile it first before you see changes when running it.
clang++ -std=c++17 main.cpp count-odd.cpp -o main
./main
You can run one, two, or all the commands below to test
 your code, stylecheck
 your code’s design, or formatcheck
 your work. Kindly make sure that you have compiled and executed your code before issuing any of the commands below to avoid errors.
make test
make stylecheck
make formatcheck
A faster way of running all these tests uses the all
 parameter.
make all
Linear Search Array
Create a recursive linear_search
 function that receives an integer array (int[]
), a number to find in the array, and the size of the array. The function should return the index of the number in the array if it exists, and returns -1 if it isn’t in the array.
Place the linear_search
‘s function prototype in lsa.hpp
 and it’s implementation in lsa.cpp
. The main
 function already contains some code, but you need to complete the requirements that is described inside the file.
Sample output:
Array: 3 16 22 8 11 0 55 34 27 31 Please enter a number you want to search for: 8 The index of 8 in the array is: 3
Array: 3 16 22 8 11 0 55 34 27 31 Please enter a number you want to search for: 15 15 is not in the array
Submission checklist
- Created function prototype and stored inÂ
.hpp
 file. - Created function implementation and stored inÂ
.cpp
 file (see reference). - Call function in the driver
- Compiled and ran the driver (
main
). - Manually checked for compilation and logical errors.
- Ensured no errors on the unit test (
make test
). - Followed advice from the stylechecker (
make stylecheck
). - Followed advice from the formatchecker to improve code readbility (
make formatcheck
).
Code evaluation
Open the terminal and navigate to the folder that contains this exercise. Assuming you have pulled the code inside of /home/student/labex02-tuffy
 and you are currently in /home/student
 you can issue the following commands
cd labex02-tuffy
You also need to navigate into the problem you want to answer. To access the files needed to answer problem 1, for example, you need to issue the following command.
cd prob01
When you want to answer another problem, you need to go back up to the parent folder and navigate into the next problem. Assuming you are currently in prob01
, you can issue the following commands to go to the parent folder then go into another problem you want to answer;Â prob02
 for example.
cd ..
cd prob02
Use the clang++
 command to compile your code and the ./
 command to run it. The sample code below shows how you would compile code save in lsa.cpp
 and main.cpp
, and into the executable file main
. Make sure you use the correct filenames required in this problem. Take note that if you make any changes to your code, you will need to compile it first before you see changes when running it.
clang++ -std=c++17 main.cpp lsa.cpp -o main
./main
You can run one, two, or all the commands below to test
 your code, stylecheck
 your code’s design, or formatcheck
 your work. Kindly make sure that you have compiled and executed your code before issuing any of the commands below to avoid errors.
make test
make stylecheck
make formatcheck
A faster way of running all these tests uses the all
 parameter.
make all
Recursive Power Function
Create a recursive function called power
 that receives two positive int
 parameters that represent the base and exponent. The function should return the base raised to the exponent.
Place the power
‘s function prototype in power.hpp
 and it’s implementation in power.cpp
. The main
 function already contains some code, but you need to complete the requirements that is described inside the file.
Sample Output:
Enter a base: 5 Enter an exponent: 3 5 ^ 3 = 125
Enter a base: 2 Enter an exponent: 10 2 ^ 10 = 1024
Submission checklist
- Created function prototype and stored inÂ
.hpp
 file. - Created function implementation and stored inÂ
.cpp
 file (see reference). - Call function in the driver
- Compiled and ran the driver (
main
). - Manually checked for compilation and logical errors.
- Ensured no errors on the unit test (
make test
). - Followed advice from the stylechecker (
make stylecheck
). - Followed advice from the formatchecker to improve code readbility (
make formatcheck
).
Code evaluation
Open the terminal and navigate to the folder that contains this exercise. Assuming you have pulled the code inside of /home/student/labex02-tuffy
 and you are currently in /home/student
 you can issue the following commands
cd labex02-tuffy
You also need to navigate into the problem you want to answer. To access the files needed to answer problem 1, for example, you need to issue the following command.
cd prob01
When you want to answer another problem, you need to go back up to the parent folder and navigate into the next problem. Assuming you are currently in prob01
, you can issue the following commands to go to the parent folder then go into another problem you want to answer;Â prob02
 for example.
cd ..
cd prob02
Use the clang++
 command to compile your code and the ./
 command to run it. The sample code below shows how you would compile code save in power.cpp
 and main.cpp
, and into the executable file main
. Make sure you use the correct filenames required in this problem. Take note that if you make any changes to your code, you will need to compile it first before you see changes when running it.
clang++ -std=c++17 main.cpp power.cpp -o main
./main
You can run one, two, or all the commands below to test
 your code, stylecheck
 your code’s design, or formatcheck
 your work. Kindly make sure that you have compiled and executed your code before issuing any of the commands below to avoid errors.
make test
make stylecheck
make formatcheck
A faster way of running all these tests uses the all
 parameter.
make all
GCD calculator
Create a recursive Greatest Common Divisor (GCD) calculator function called gcd
 that receives two integer inputs. The function should return the GCD of those two numbers.
There are two methods to compute the GCD of two numbers: Euclidean and Dijkstra’s algorithm. You can choose either of the two algorithms to implement your recursive gcd
 function.
Euclidean Algorithm
The Euclidean algorithm mainly uses division and the remainder of the two numbers to find the GCD. It can be represented as a recursive algorithm such that
gcd(num1, num2) = num2 if num1 == zero // base case
= gcd(num2 % num1, num1) if num1 > zero // recursive case
You can learn more about this topic by going through these slides.
Dijkstra’s GCD algorithm
Dijkstra’s algorithm mainly uses subtraction to find the GCD. It can be represented as a recursive algorithm such that
gcd(num1, num2) = num2 // if num1 and num2 are equal
= gcd(num1 - num2, num2) // if num1 is greater than num2
= gcd(num1, num2 - num1) // if num2 is greater than num1
As an example, lets say we want to find the GCD of the two numbers 72 and 56
gcd(72 , 56)
72-56 , 56 Take the larger number, 72 and subtract 56 from it
16 , 56 This is our end result, but we need to keep going. Until they are equal
gcd(16 , 56)
16 , 56-16 Take the larger number,56 and subtract 16 from it
16 , 40 This is our end result, but this still doesnt give us our gcd, so we keep going until both numbers are equal
gcd(16 , 40)
16 , 40-16 Take the larger number 40 and subtract 16 from it.
16 , 24 This is our end result, but this still doesnt give us our gcd, so we keep going until both numbers are equal
gcd(16,24)
16 , 24-16 Take the larger number 24 and subtract 16 from it.
16 , 8 This is our end result, but this still doesnt give us our gcd, so we keep going until both numbers are equal
gcd(16, 8)
16-8, 8 Take the larger number 16 and subtract 8 from it.
8 ,8 this is our end result, and on the next function call we will validate the equality of those two numbers there.
gcd(8,8)
8 , 8 The two numbers are equal. THIS IS OUR GCD.
You can learn more about this topic by going through these slides
Place the gcd
‘s function prototype in gcd.hpp
 and it’s implementation in gcd.cpp
. The main
 function already contains some code, but you need to complete the requirements that is described inside the file.
Sample Output
Enter the first number: 24 Enter the second number: 18 The GCD of the numbers 24 and 18 is 6
Submission checklist
- Created function prototype and stored inÂ
.hpp
 file. - Created function implementation and stored inÂ
.cpp
 file (see reference). - Call function in the driver
- Compiled and ran the driver (
main
). - Manually checked for compilation and logical errors.
- Ensured no errors on the unit test (
make test
). - Followed advice from the stylechecker (
make stylecheck
). - Followed advice from the formatchecker to improve code readbility (
make formatcheck
).
Code evaluation
Open the terminal and navigate to the folder that contains this exercise. Assuming you have pulled the code inside of /home/student/labex02-tuffy
 and you are currently in /home/student
 you can issue the following commands
cd labex02-tuffy
You also need to navigate into the problem you want to answer. To access the files needed to answer problem 1, for example, you need to issue the following command.
cd prob01
When you want to answer another problem, you need to go back up to the parent folder and navigate into the next problem. Assuming you are currently in prob01
, you can issue the following commands to go to the parent folder then go into another problem you want to answer;Â prob02
 for example.
cd ..
cd prob02
Use the clang++
 command to compile your code and the ./
 command to run it. The sample code below shows how you would compile code save in gcd.cpp
 and main.cpp
, and into the executable file main
. Make sure you use the correct filenames required in this problem. Take note that if you make any changes to your code, you will need to compile it first before you see changes when running it.
clang++ -std=c++17 main.cpp gcd.cpp -o main
./main
You can run one, two, or all the commands below to test
 your code, stylecheck
 your code’s design, or formatcheck
 your work. Kindly make sure that you have compiled and executed your code before issuing any of the commands below to avoid errors.
make test
make stylecheck
make formatcheck
A faster way of running all these tests uses the all
 parameter.
make all
Max Value
Write a program that finds the largest number in an array of positive numbers using recursion. Your program should have a function called array_max
 that should pass in an int
 array, and an int
 for the size of the array, respectively, and should return an int
 value.
Given an array of size 10 with contents:Â {10, 9, 6, 1, 2, 4, 16, 8, 7, 5}
The output should be:
The largest number in the array is 16
The function should return -1 if the input is invalid.
Place the array_max
‘s function prototype in array_max.hpp
 and it’s implementation in array_max.cpp
. The main
 function already contains some code, but you need to complete the requirements that is described inside the file.
Submission checklist
- Created function prototype and stored inÂ
.hpp
 file. - Created function implementation and stored inÂ
.cpp
 file (see reference). - Call function in the driver
- Compiled and ran the driver (
main
). - Manually checked for compilation and logical errors.
- Ensured no errors on the unit test (
make test
). - Followed advice from the stylechecker (
make stylecheck
). - Followed advice from the formatchecker to improve code readbility (
make formatcheck
).
Code evaluation
Open the terminal and navigate to the folder that contains this exercise. Assuming you have pulled the code inside of /home/student/labex02-tuffy
 and you are currently in /home/student
 you can issue the following commands
cd labex02-tuffy
You also need to navigate into the problem you want to answer. To access the files needed to answer problem 1, for example, you need to issue the following command.
cd prob01
When you want to answer another problem, you need to go back up to the parent folder and navigate into the next problem. Assuming you are currently in prob01
, you can issue the following commands to go to the parent folder then go into another problem you want to answer;Â prob02
 for example.
cd ..
cd prob02
Use the clang++
 command to compile your code and the ./
 command to run it. The sample code below shows how you would compile code save in array_max.cpp
 and main.cpp
, and into the executable file main
. Make sure you use the correct filenames required in this problem. Take note that if you make any changes to your code, you will need to compile it first before you see changes when running it.
clang++ -std=c++17 main.cpp array_max.cpp -o main
./main
You can run one, two, or all the commands below to test
 your code, stylecheck
 your code’s design, or formatcheck
 your work. Kindly make sure that you have compiled and executed your code before issuing any of the commands below to avoid errors.
make test
make stylecheck
make formatcheck
A faster way of running all these tests uses the all
 parameter.
make all