Homework 2 solution

$24.99

Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (1 vote)

1. Flip Flop: 10 points File: flip_flop.cpp We want to model the behavior of a strange sort of fish over some time. On secondsdivisibleby a itflips,onthosedivisibleby b itflopsandonthosedivisible by both it flips and flops. To simulate this behavior your program should print “flip” when it flips, “flop” when it flops and “flipflop” when it flips and flops. If the fish doesn’t do any action then you should just print out the current second. Input to your program for each case will be the second to start at, the number of seconds to simulate and the values for a and b. Your program should then log the behavior of the fish by printing its action or the current time as specified above for the desired number of seconds. Note that 0 counts as divisible by any number, recall that 0 % x is always 0 for any value of x. Example Input 3 0 16 3 5
1
10 10 2 7 4 20 12 4 Expected Output Case 0: flipflop 1 2 flip 4 flop flip 7 8 flip flop 11 flip 13 14 flipflop Case 1: flip 11 flip 13 flipflop 15 flip 17 flip 19 Case 2: flop 5 6 7 flop 9 10 11 flipflop 13 14 15 flop
2
17 18 19 flop 21 22 23
2. Worth Every Penny: 30 points File: worth_every_penny.cpp You are running a business of selling stainless-steel wedding rings. Bill, the software engineer you hired to program your business software is an experienced programmer, so he decided to use double-precision floating-point numbers (double) for all amounts of money in the software, fearing that one day a puny float wouldrunoutofprecisiontorepresentyourbillion-dollarfortune. Beinga brilliant businessman yourself, you have come up with a unique pricing strategy: the first ring is sold for 25 cents ($0.25), the second one for $10.25, the third one $20.25 and so on. In other words, the price for each ring is $10 more than the last. This plan has proven to work quite well: over the years you have sold 30’000’000 (thirty million) rings, and every penny earned was recorded in the software. The profit from selling all these rings is $4499999857500000. This result can be verified using, for example, Wolfram Alpha. See the following screenshot for how to use Wolfram Alpha to evaluate this number. Yet the software written by Bill surprisingly reports a different profit value. Obviously you are not happy and decide to take the matters into your own hands. In this problem you will write a program to compute the exact total profit after the n−1th ring is sold. n will be given to your program as input, and it will be an integer between 1 and 30000000, inclusively. For example, if n =3, the profit should be 0.25+10.25+20.25=30.75. In addition to the exact total profit, your program should also output the number computed by Bill’s program (remember that he used double-precision floating-point numbers for all his calculations). Print your output in dollars, in fixed point notation, to up to 2 digits after the decimal point. You can use std::fixed and std::setprecision in the