Description
- Write C++ programs
- Compile C++ programs
- Implement programs that use templates and vectors
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.
Pair Template
In this program, you will be creating a class that utilizes class templates.
MyPair Template Class
Create a class called MyPair
 that uses template <class T>
 and will have the following:
Private Member Variables
value1_
 which is aÂT
 object.value2_
 which is aÂT
 object.
Non-Default Constructor
Create a non-default constructor that takes in two T
 objects as parameters and initializes the values to the data member variables respectively.
Accessors
Create accessors for both data members
Display Member Function
Create a member function called display()
 that will display the member variables in the order shown in the output below.
[Value1, Value2]
Display Reverse Member Function
Create a member function called display_reverse()
 that will display the member variables in reverse order as shown in the output below.
[Value2, Value1]
Max Value Member Function
Create a member function called max_value()
 that will return a T
 object. This member function should compare the two member variable values and return the greater value (if the values are equal, return the second value).
Min Value Member Function
Create a member function called min_value()
 that will return a T
 object. This member function should compare the two member variable values and return the lesser value (if the values are equal, return the second value).
Swap Value Member Function
Create a member function called swap_value()
 that will swap the two member variable values.
Other instructions
Complete the main
 function as described. Place your classes in pair_class_template.hpp
. Member functions that take more than five lines or use complex constructs should have their function prototype in pair_class_template.hpp
 and implementation in pair_class_template.cpp
.
Sample Output
[3, 5]
[5, 3]
5
3
[5, 3]
[3, 5]
[9.7, 6.4]
[6.4, 9.7]
9.7
6.4
[6.4, 9.7]
[9.7, 6.4]
[a, z]
[z, a]
z
a
[z, a]
[a, z]
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 saved in pair_class_template.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 pair_class_template.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
Statistics Calculator
In this program, you will be using the vector
 library.
Statistics Calculator Class
Create a class called StatisticsCalculator
 that will have the following:
Data members
data_set_
 that is anÂstd::vector
 that containsÂdouble
 values.Âstd::vector
 uses templates so we specify the typeÂdouble
 inside theÂ< >
.
Member functions
add_data
Create a member function called add_data
 that takes 1 parameter, a double
 that will be stored in the vector. Use the std::vector
 member function push_back
 to store the data passed through the parameter into the vector.
data_at
Create a member function called data_at
 that takes 1 parameter, an int
 that will be the index. This function should return the the element from the vector at the index using the index operator ([ ]
).
size_of_data
Create a member function called size_of_data
 that should return the size of the vector using the std::vector
 member function size
.
mean
Create a member function called mean
 that should return the mean of all the elements in the vector. If the vector is empty return a value of 0
. You can use std::vector
‘s member function empty()
 to check.
Other instructions
Complete the main
 function as described. Place your classes in statistics_calculator.hpp
. Member functions that take more than five lines or use complex constructs should have their function prototype in statistics_calculator.hpp
 and implementation in statistics_calculator.cpp
.
Sample output 1
Welcome to the Statistics Calculator! Please select a menu option: 1 - Add to the data set 2 - Display the entire data set 3 - Get the mean of the data set 0 - Exit the program Selection: 1 Input the number you want to add to the data set: 23 Welcome to the Statistics Calculator! Please select a menu option: 1 - Add to the data set 2 - Display the entire data set 3 - Get the mean of the data set 0 - Exit the program Selection: 2 The data set 0: 23 Welcome to the Statistics Calculator! Please select a menu option: 1 - Add to the data set 2 - Display the entire data set 3 - Get the mean of the data set 0 - Exit the program Selection: 3 The mean of the data is 23.00 Welcome to the Statistics Calculator! Please select a menu option: 1 - Add to the data set 2 - Display the entire data set 3 - Get the mean of the data set 0 - Exit the program Selection: 0 Exiting...
Sample output 2
Welcome to the Statistics Calculator! Please select a menu option: 1 - Add to the data set 2 - Display the entire data set 3 - Get the mean of the data set 0 - Exit the program Selection: 2 The data set Welcome to the Statistics Calculator! Please select a menu option: 1 - Add to the data set 2 - Display the entire data set 3 - Get the mean of the data set 0 - Exit the program Selection: 3 The mean of the data is 0.00 Welcome to the Statistics Calculator! Please select a menu option: 1 - Add to the data set 2 - Display the entire data set 3 - Get the mean of the data set 0 - Exit the program Selection: 0 Exiting...
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 saved in statistics_calculator.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 statistics_calculator.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