Hello, if you have any need, please feel free to consult us, this is my wechat: wx91due
PIC 10A Lec 4: Homework 4
(Soft Deadline Wed, 11/13, Hard Deadline Wed, 11/20 11:59PM)
Submitting your homework
• Upload hw4.cpp to Gradescope before the deadline.
You should be submitting exactly one file and it should be hw4.cpp.
• Name the file exactly as just stated.
Failure to do so will result in 0 points.
• Do not submit hw4.hpp, main.cpp, or output.txt.
• Do not enclose the file in a folder or zip it.
• Be sure that your file builds and runs when placed together with hw4.hpp and main.cpp in a
Visual Studio 2022 project, and make sure that this remains true after uncommenting the test cases in main.cpp.
Learning Objectives
• for loops.
• How to organize code between header files and and cpp files.
• How to read function comments and how to write function definitions.
• The difference between pure functions and procedures.
• Experience using the std::vector class template.
• When to pass arguments by value.
• When to pass arguments by reference.
• When to pass arguments by reference to const.
Tasks
1. Download hw4_template.zip from BruinLearn. The zip file includes: hw4.hpp, hw4.cpp, main.cpp
2. Create a new project in your IDE and add the files hw4.hpp, hw4.cpp, main.cpp. Videos on BruinLearn (Media Gallery/Code Organization: hpp, cpp) are available to demon strate how to do this on XCode and Visual Studio.
3. The assignment expressed in one sentence: for each function comment and declaration in hw4.hpp, provide a definition of the function within hw4.cpp.
The rest of this document will provide further explanation of what you have to do, and it will also provide some conceptual questions that will help your learning (submitting answers to these questions is not necessary).
4. Note that one example (void print(const vector<int>& v)) is completed for you, showing what you are expected to do.
Its function comment and declaration can be found in hw4.hpp.
Its definition can be seen in hw4.cpp.
5. Note that main.cpp includes lots of commented out testing code.
Upon uncommenting everything, building and running the code, the user should be prompted to type something (as a result of calling get_double). If they type 8.8 and hit ENTER, the output should be the same as what is saved in output.txt.
Hints and comments
1. Your tasks are to complete the functions in hw4.cpp. When working on a function, consider starting by reading the header file hw4.hpp carefully to understand what the function does, and what input parameters it takes in.
Also, take a look at main.cpp of the use case of the function, as well as the expected outputs in output.txt.
When debugging, you can un-commented the corresponding test code for the function in main.cpp and compare with the corresponding outputs in output.txt.
2. void basic_arith(int, int);
The purpose of this question is to start getting you used to organizing your code into functions, in this case, a procedure. Otherwise, you have solved basic_arith before!
Lines 2 to 6 of output.txt say. . . and you have seen this exact output on HW1.
The difference is that a user is not typing the ints into the console.
Instead, someone is specifying the ints when they call the function in main.cpp.
The only other difference is the output line at the beginning.
Make sure that your output from running the code in main.cpp includes. . .(the line as shown in output.txt)
3. bool leap_year(int);
The purpose of this question is similar to the previous one. You have seen how to solve this problem before and so maybe try to solve it in one line and without any if statements. This will test how well you understand bools.
Notice that the test cases provided in main.cpp go through all the different scenarios for why a year might be a leap year or not. They are good test cases because they are likely to reveal any problems that someone’s code has.
The testing code that is provided in main.cpp also gives further practice with vector<int> and control flow. Try to go over and make sure you understand the code line by line.
At this point you should compare basic_arith and leap_year.
Do you understand why is one a procedure and one a pure function?
4. void capitalize(string& s);
You have seen capitalizing before. However, this function is only supposed to change lowercase alphabetic characters. An if statement together with some char comparisons (which compare the int-casted values) can be helpful here.
Do you understand why the reference in the function signature is useful here?
Do you understand why this function is a procedure?
5. vector<int> concatenate(const vector<int>&, const vector<int>&);
This function will need to construct a new vector<int>, edit it, and return it.
Do you understand why the references to const in the function signature are useful here?
Do you understand why this function is a pure function?
6. void reverse(vector<int>&);
To define this function, consider using std::swap for about (v.size() / 2) times.
Do you understand why the reference in the function signature is useful here?
Do you understand why this function is a procedure?
7. double get_double(const string&);
This function is neither a pure function nor a procedure. It does three things:
• Prints a message to the console.
• Extracts characters from the input buffer.
This will prompt a user for input when the input buffer is empty.
• If the user types characters that can be interpreted as a double, it returns the corresponding double.
The first two points prevent it from being a pure function.
The last prevents it from being a procedure.
Nevertheless, you can see it is useful. In main.cpp, running the corresponding test code . . . A correctly coded get_double will lead to the following.
• An output of What is your favorite number? followed by a space.
• The user having the opportunity to respond.
• If the user types 8.8 and hits ENTER, the code will continue to execute, and the console will display. . . (as shown in output.txt)
8. string new_capitalized_version(string);
One difference between this function and capitalize is that new_capitalized_version is a pure function whereas capitalize is a procedure.
Passing by value allows a copy of the string to be made by the parameter in the function scope.
Consider solving this question by applying the procedure capitalize on the copy of the string, and then returning the mutated/modified copy.
Something to take away: since the parameter will take care of any necessary copying, your function body should not explicitly create copy of a std::string anywhere. This question has a very short solution.
9. bool is_magic_square(const vector< vector<int> >&);
This is the longest question. Here are some hints.
• Store the size of the vector<vector<int>>. This is the number of rows.
• Considering checking the conditions to be a magic square one by one. If a condition fails, we can return false. Otherwise, we can move onto the next condition.
• Check the vector<vector<int>> is square by looping over the rows and comparing the size of each row with the number previously stored.
• For an (N × N)-square, check each of the numbers 1 to N2 appears exactly once.
– Consider using nested for-loops to loop through all the values in the square; if they are too big or small, return false; otherwise, consider recording their presence in a vector<bool> called appeared.
– The vector appeared should have a certain size and initial values at initialization -what should be its size and the initial values?
– Finally, loop through appeared to check that it is full of all trues, and no falses remains. This ensures every value is used exactly once because using a value twice would cause us to miss another value.
• For an (N × N)-square, the “magic total” is given by 1
2
· N · (N2 + 1). This is the value the rows, columns, and diagonals need to sum to. Check that they all satisfy this condition.
– Use nested for-loops to check the rows sum to the magic total.
– Use nested for-loops to check the columns sum to the magic total.
– Avoid trying to check the rows and columns simultaneously. Doing them separately makes the code structure and logic cleaner.
– Use a single for-loop to check the first diagonal sums to the magic total.
– Use a single for-loop to check the second diagonal sums to the magic total.