Conditionals Using bools and scope
Video
Key Takeways
Video Addendum
the %
symbol is used for the mod operator. a % b
would return the remainder after a is divided by b.
For example: 14 % 5
is 4
. and 3 % 10
is is 3
.
For more help see: (Khan Academy)[https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/what-is-modular-arithmetic]
Assignment
- For each of the code samples below
- Describe what you think is wrong with the code (This means write it down in complete sentences!).
- Confirm your hypothesis by building a memory table
- Double check your work by trying to run the code in an example Repl.
- Currently your calculator asks the user for two numbers. Create a new repl, where you have a calulator that asks for both 2 numbers and one of the following operators
+ - * / %
. It should then print out the result.- After you’ve finished that, upgrade your calulator again to ask it for 3 numbers and 2 operator. Be sure to also print out the result not only at the end, but after each operator!
- What about 4 numbers and 3 operators?
- What about 5 numbers and 4 operators?
- Did you notice any patterns as to what you were doing? Try describing what you think the pattern is (This means write it down in complete sentences!).
- Note: Do NOT worry about order of operations.
Code Samples
Sample 0
#include <iostream>
int main() {
int avg_rainfall_us_inches = 31
{
int avg_rainfall_wa_inches = 38;
{
int avg_rainfall_seattle_inches = 37;
}
}
std::cout << "Average Rainfall in the US is " << avg_rainfall_us_inches << "inches" << std::endl;
std::cout << "Average Rainfall in Washington is " << avg_rainfall_wa_inches << "inches" << std::endl;
std::cout << "Average Rainfall in Seattle is " << avg_rainfall_seattle_inches << "inches" << std::endl;
}
Sample 1
#include <iostream>
int main() {
int num_days = 11;
std::cout << "Avatar hit $1 billion dollars in " << num_days << std::endl;
int num_days = 5;
std::cout << "Avengers hit $1 billion dollars in " << num_days << std::endl;
}
Sample 2
#include <iostream>
int main() {
{
int num_days = 11;
std::cout << "Avatar hit $1 billion dollars in " << num_days << std::endl;
}
{
int num_days = 5;
std::cout << "Avengers hit $1 billion dollars in " << num_days << stdendl;
}
}
End of Assignment Checklist
- I finished all the assignments.
- I shared my assignments with others.
- I provided feedback for assignments of at least 2 others.
- I addressed the feedback from others and thanked them for the review.