Question: Feel free to make another function to help (called a helper function). Maybe something like posRound which only takes positive numbers and rounds them might
Feel free to make another function to help (called a helper function). Maybe something like posRound which only takes positive numbers and rounds them might help.
If you get stuck, think about how YOU do this and try some examples (like below). How do you convert -2.5 to 2.5? If the value is 2.5, how do you round it to 3? You can either determine if 0.5 is rounded up or down (remove the whole numbers) orwhat if you cast 3.4 to be an integer? o int val = (int) 3.4; // This is 3 since you always round down or floor when you convert from a double to an int. o int val = (int) 3.7; // Also 3! o What value can I add to these numbers to make rounding work?
For example (int)(3.8 + 0.3) is 4
(int)(3.2 + 0.3); is 3.
You can do this rounding any way you want (but you should have a ternary expression used in there somewhere). I am just making some suggestions. Sample tests: Here is what I would expect the outputs for simpleRound. Side Note: Assert Statements Recall that an assert is just some true/false test. If what is in the assert statement is true, then nothing happens. If it is false, the program crashes with a message.
These are useful for sanity and safety checks in your program. Asserts also automatically get removed when you make a release build of your program that you send to customers.
Asserts are simple ways we can test our code. Better approaches (like unit tests) are more complex but a more appropriate solution. Asserts are shown for their ease of use in testing.
(perhaps this is like testing a wall socket works by sticking a fork in it. There are more elegant ways to test code but this does the job). Task 2: I want to look at a very Canadian problem: since we no longer have pennies, I want to know if a price can be exactly paid in dollars or coins. The function isExactChange returns true/1 if (and only if) the price can be represented with coins and dollars.
isExactChange takes an unsigned integer pennyPrice as an input parameter and returns 1 if the value can be represented with coins and dollars and 0 (false) if it cannot.
pennyPrice is the price in pennies. An item thats $5.23 would be represented as 523.
Now that we dont have pennies, we only have nickels (5 cents), dimes (10 cents), quarters (25 cents), loonies ($1), toonies ($2) and bills.
Notice numbers like 15 and 810 can be represented with exact change, but 17 and 431 cannot. Why is that? Do you notice a pattern? The pattern should be pretty simple (I hope). You should come up with your own tests (and perhaps printf some information). I only used asserts as a quick way to test that my code returns the expected values // Test isExactChange
assert(isExactChange (0));
assert(isExactChange (115));
assert(isExactChange (116) == 0);
assert(isExactChange (30));
assert(isExactChange (9218) == 0);
assert(simpleRound(2.5) == 3);
assert(simpleRound(-2.6) == -3);
assert(simpleRound(-6.2) == -6);
assert(simpleRound(5) == 5);
assert(simpleRound(11.2) == 11);
Task 4:
Num Quarters, Please do task 3 first. It will help you do this problem. Lets do a very similar problem to isExactChange. Write a function numQuarters that takes an unsigned int pennyPrice as an input parameter (see instructions above) and returns the number of quarters in that amount (an unsigned int).
For example:
assert(numQuarters(115) == 4);
// 4 quarters in $1.15
assert(numQuarters(131)==5);
// $1.25 in quarters and 6 cents extra.
Task 4:
Super Bowl Calculator:
Write two functions superBowlConvertSmall, and superBowlConvertLarge, where the function superBowlPrinter (below) takes an unsigned int between 1 and 100 (inclusive) and calls functions superBowlConvertSmall and superBowlConvertLarge to calculate the roman numeral of that number, then prints out a message about it. For example, if we give number 49 to superBowlPrinter, it should outputs: Super Bowl 49 is XLIX.
Notice something strange about roman numerals up to 100? They actually consist of two parts: o The numbers 1-9 are represented on the right side of the number. The number 19 is XIX, 39 is XXXIX. 59 is LIX. 13 is XIII and 43 is XLIII. o The larger number is located on the left of the number. For example, 59 is LIX while 55 is LV.
superBowlPrinter
Here is the function that calls the two functions you need to write.
You can define superBowlConvertSmall and superBowlConvertLarge below this code in your .cpp file.
const char* superBowlConvertSmall(unsigned int sbNum);
const char* superBowlConvertLarge(unsigned int sbNum);
// Takes a superbowl number between 0 and 100 (inclusive)
// and outputs a message about what the roman numbers for the game are. void superBowlPrinter(unsigned int sbNum) { if (sbNum == 0 || sbNum > 100) { return; }
// Example for a sbNum of 52, sbSmall is "II" and sbLarge is "L" const char* sbSmall = superBowlConvertSmall(sbNum % 10);
const char* sbLarge = superBowlConvertLarge(sbNum (sbNum % 10));
printf("Super Bowl % i is %s%s ", sbNum, sbLarge, sbSmall); } SuperBowlConvertSmall
For the function superBowlConvertSmall: o sbNum should only be a number between 0 and 9 o Please use a switch statement to return the correct message. o Notice the function returns const char*. We will look at this later. Just type that in and put return values in double quotes ("") such as: o superBowlConvertSmall(8) returns VIII
o superBowlConvertSmall(9) returns IX
o superBowlConvertSmall(6) returns VI
o superBowlConvertSmall(0) returns "" Please note quotes in word will not work in visual studio. Retype the above text if you use it.
For the function superBowlConvertLarge:
const char* superBowlConvertSmall(unsigned int sbNum);
const char* superBowlConvertLarge(unsigned int sbNum);
o The parameter sbNum for this function should be a multiple of 10 (so 10, 20, 30, ) and returns text like "L" or "XXX"notice that 39 is XXXIX and 33 is XXXIII
o Please use a switch statement to return the correct message.
o superBowlConvertLarge(30) returns "XXX" o superBowlConvertLarge(0) returns ""
A constant char* is essentially a text string or message (like hello world).
// If it helps, here are the function prototypes for the functions you need to define.
double myAbs(double myVal);
const char* superBowlConvertSmall(unsigned int sbNum);
const char* superBowlConvertLarge(unsigned int sbNum);
void superBowlPrinter(unsigned int sbNum); //(GIVEN)
int simpleRound(double orig);
int isExactChange (unsigned int pennyPrice);
int numQuarters(unsigned int pennyPrice);
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
