PIC 10A Homework 4 Winter 2022
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
PIC 10A
Homework 4
Winter 2022
Instructions: A template to answer this homework is provided on Bruinlearn. If you have not set up a project with more than one .cpp file before, go back to the videos that I posted in week 1. They explain how to get started. Your submission should consist only of functions.hpp and functions.cpp. functions.hpp should contain function declarations with function comments. functions.cpp should contain function definitions. While you are free to change the names of function parameters, YOU CANNOT CHANGE FUNCTION NAMES OR PARAMETER TYPES. Otherwise, youll likely get a build error and 0 points. We will test your functions by running something similar to the main.cpp I provided.
1. (6 points)
(a) Write a pure function bool leap year(int year) that tests whether a year is a leap year. (Just like I did in class, you can assume the Gregorian correction has always applied.) Recall the description on wikipedia:
“Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400. For example, the years 1700, 1800, and 1900 are not leap years, but the years 1600 and 2000 are.”
If you have a good understanding of booleans, your definition can be one line long. (b) Write a procedure void fib(int N) which prints the first N Fibonacci numbers. Have its
first line be assert (0 <= N && N <= 46); so that your definition looks like
void fib(int N) {
assert (0 <= N && N <= 46);
// Fill in here.
}
Because of the assert statement, passing values to the function outside of the specified range will cause a runtime error. Because this is part of the function’s behavior, your function comment should mention this. (Why dont I want to allow 47, 48, 49, ...?)
(c) Write a function double get double(string prompt) that
prints prompt followed by a space,
reads in a double from the console,
returns the double which was read in.
If a user types something that causes cin.fail () to become true, the function should cout “cin failed, so 0 was returned.” and it should return 0 .
This function is neither a pure function or a procedure because it prints something and returns something. Because the behavior regarding cin.fail () is part of the function’s behavior, your function comment should mention this.
Here is a typical usage:
double currentSalary = get_double("What’s your current salary?");
double desiredRaise = get_double("What percentage raise would you like?"); double newSalary = currentSalary * (1.0 + desiredRaise/100.0);
if (newSalary > 100000) { cout << "Get outta here!" << endl; }
else { cout << "We’ll think on it." << endl; }
2. (3 points) Write a pure function vector
if v is the vector containing 1 4 9 16 and w is the vector containing 9 7 4 9 11, then concatenateMyVectors(v, w) returns the vector containing 1 4 9 16 9 7 4 9 11. main.cpp also provides an example.
3. (2 points) Write a procedure void sort5(int& i1, int& i2, int& i3, int& i4, int& i5) that swaps its five inputs to arrange them in increasing order. This procedure should not print anything. main.cpp provides examples.
4. (3 points) Write a procedure void reverse(vector
main.cpp also provides an example.
(I’d use swap about v.size()/2 times.)
5. (6 points) In this question, we’ll think about booleans as having values 0 and 1 (rather than false and true) and we’ll use them to store bits.
(a) Write a procedure void print(const vector
(b) Write a procedure void addZeros(vector
(c) Write a pure function vector
● 210 is 01 in binary, 810 is 0001 in binary, and their sum should be 1010 which is 0101 in binary.
● 1010 is 0101 in binary, 810 is 0001 in binary, and their sum should be 1810 which is
01001 in binary.
You should not need to convert to the decimal system. Instead, implement addition with carrying. The examples above are done as follows. We also provide a third code example.
Addition with carrying:
0100 01010 110011000100
+ 0001 + 00010 + 111001010110
---- ----- ------------
0101 01001 010110110001
Carry: 1 111 1 11
Note: This is the same as how you are taught to add in elementary school. However, we’re writing numbers in binary and with the lowest significant bit on the left. I promise that going from left to right is done to make the question easier!
Note 2: I have purposefully passed the two vectors by value (as opposed to reference to const). Ive done this so that you can addZeros to (copies of) the vectors without getting a build error.
(d) The output of a main() function saying · · ·
vector
vector
vector
print(b3);
should be 010110110001.
2022-02-07