CSCI240 Program 3 Loops and Decision Statements solution

$25.00

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

Description

5/5 - (5 votes)

Would you take a job where your starting pay is a random amount between $0.01 and $0.05, inclusive? How about if you could only work a random number of days (between 1 and 30, inclusive)? If your answer is “no” to either of those questions, what if your pay doubled each day that you worked and you received a bonus of 10 times your starting salary every 5th day? Would you take the job now?

For this assignment, write a program that will calculate how much a person would earn over a finite (random) period of time if his/her salary is a random amount for the first day and continues to double each day.

So if a person’s starting salary is $0.04, they would earn that $0.04 for the first day of work, $0.08 for the second day of work, $0.16 for the third day of work, etc…. Over three days, the person would earn $0.28.

If the example is carried on for a few more days, the person would earn $0.32 for the fourth day, a total of $1.04 ($0.64 in salary and a $0.40 bonus) for the fifth day, and $1.28 for the sixth day, resulting in a total of $2.92 for 6 days of work.

Random Number Generation

In the first two programs, the user has been asked for input. This program will be different. Rather than asking the user for input, the random number generator will be used to determine the starting salary and the number of days worked.

To use the random number generator, first add a #include statement for the cstdlib library to the top of the program:

#include <cstdlib>

Next, initialize the random number generator. This is done by calling the srand function and passing in an integer value (known as a seed value). This should only be done ONE time and it MUST be done before actually getting a random number. A value of 1 (or any integer literal) will generate the same sequence of “random” numbers every time the program is executed. This can be useful for debugging:

srand(1);

To get a different series of random numbers each time the program is run, the actual time that the program is run can be passed as the seed value for the random number generator:

srand(time(0));

If the time function is used, make sure to include the ctime library as well.

Finally, generate a random number by calling the rand function:

num = rand();

The above line of C++ code will generate a “random” integer between 0 and RAND_MAX and saves the value in an integer variable named num. RAND_MAX is a pre-defined constant that is equal to the maximum possible random number. It is implementation dependent but is guaranteed to be at least 32,767.

Modulus division can be used to restrict the “random” integer to a smaller range:

num = rand() % 7;

will produce a value between 0 and 6. To change the range to 1 through 7, simply add 1:

num = rand() % 7 + 1;

Basic Program Logic

Generate a random number between 1 and 5. This will be used to determine the user’s starting salary. A value of 1 sets the starting salary to $0.01, 2 sets the starting salary to $0.02, 3 to $0.03, etc…

Generate another random number. This time is should be between 1 and 30. It will represent the number of days to be worked.

Display the starting salary and the number of days to be worked.

In a loop that executes exactly one time for each day to be worked, calculate and display the user’s salary for each day, update the user’s total pay, and, if necessary, add the user’s bonus to the total pay and keep track of the number of bonuses that are earned. A reminder — the user’s bonus is 10 times the starting pay and it’s earned every 5th day.

After the loop has finished executing, display the user’s total pay and if bonuses were earned, the number of bonuses that were earned and the amount of a bonus. If the number of bonuses is to be displayed, make sure it is grammatically correct. If only 1 bonus is earned, then something like “… and earned 1 bonus of …” should be displayed. If more than 1 bonus was earned, then something like “… and earned 3 bonuses of …” should be displayed.

Processing Requirements

  1. At the top of your C++ source code, include a documentation box that resembles the one from programs 1 and 2. This will be a part of every program that is submitted during the semester and this will be the last reminder in the program write-ups.
  2. Include line documentation. There is no need to document every single line, but logical “chunks” of code should be preceded by a line or two that describes what the “chunk” of code does. This will also be a part of every program that is submitted for the remainder of the semester.
  3. To use the random number generator, you will need to add two additional #includes: #include <ctime> and #include <cstdlib> at the beginning of your program
  4. The dollar amounts should be displayed with exactly 2 digits after the decimal point.
  5. Hand in a copy of your source code (the CPP file) using Blackboard.

Output

Run 1

Starting salary: 0.05
Number of days to work: 5

Day  1:          0.05
Day  2:          0.10
Day  3:          0.20
Day  4:          0.40
Day  5:          0.80

You made $2.05 and earned 1 bonus of $0.50

Run 2

Starting salary: 0.01
Number of days to work: 19

Day  1:          0.01
Day  2:          0.02
Day  3:          0.04
Day  4:          0.08
Day  5:          0.16
Day  6:          0.32
Day  7:          0.64
Day  8:          1.28
Day  9:          2.56
Day 10:          5.12
Day 11:         10.24
Day 12:         20.48
Day 13:         40.96
Day 14:         81.92
Day 15:        163.84
Day 16:        327.68
Day 17:        655.36
Day 18:       1310.72
Day 19:       2621.44

You made $5243.17 and earned 3 bonuses of $0.10

Run 3 (using srand(1);)

Starting salary: 0.02
Number of days to work: 18

Day  1:          0.02
Day  2:          0.04
Day  3:          0.08
Day  4:          0.16
Day  5:          0.32
Day  6:          0.64
Day  7:          1.28
Day  8:          2.56
Day  9:          5.12
Day 10:         10.24
Day 11:         20.48
Day 12:         40.96
Day 13:         81.92
Day 14:        163.84
Day 15:        327.68
Day 16:        655.36
Day 17:       1310.72
Day 18:       2621.44

You made $5243.46 and earned 3 bonuses of $0.20

Extra Credit

For up to 5 points of extra credit, add code that will display the total amount that the user has earned each day along with the daily salary. The values should be displayed in neat columns.

Note about extra credit: the points will ONLY be awarded if the required portions of the assignment work correctly. In other words, don’t take short cuts in the rest of the program because it is assumed that 5 extra points will be awarded.

Extra Credit Output

Starting salary: 0.04
Number of days to work: 14

           Daily Salary     Amount Earned
Day  1:          0.04              0.04
Day  2:          0.08              0.12
Day  3:          0.16              0.28
Day  4:          0.32              0.60
Day  5:          0.64              1.64
Day  6:          1.28              2.92
Day  7:          2.56              5.48
Day  8:          5.12             10.60
Day  9:         10.24             20.84
Day 10:         20.48             41.72
Day 11:         40.96             82.68
Day 12:         81.92            164.60
Day 13:        163.84            328.44
Day 14:        327.68            656.12

You made $656.12 and earned 2 bonuses of $0.40