Description
Would you take a job where your starting pay is a random amount between $0.01 and $0.10, 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? 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 their salary is a random amount for the first day and continues to double each day for three different jobs.
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.
Random Number Generation
As with program 4, 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.
This assignment will use:
- #include <cstdlib>
- srand
- rand
Please refer back to Program 4 for reminders about how to work with random numbers. http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm4.htm
Basic Program Logic
Use a value of 15 to seed the random number generator.
For the first job, generate a random number between 1 and 10. 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 the number should be between 1 and 30. It will represent the number of days to be worked.
Note: since the random number generator is being used, it is important that the values are generated in the order listed above: salary and then number of days.
Display the starting salary and the number of days to be worked for Job 1.
Using a for loop that executes exactly one time for each day to be worked, display the user’s salary for each day, update the user’s total pay, and calculate the salary for the next day of work.
After the for loop has finished executing, display the user’s total pay and the number of days that it took to earn the pay. The total pay should be preceded by a dollar sign($) and have exactly two digits after the decimal point.
For the second job, repeat the basic process that was laid out for Job 1. However, rather than using a for loop, use a while loop.
For the third job, repeat the basic process that was laid out for Job 1. However, rather than using a for loop, use a do while loop.
Symbolic Constants
This program requires the use of 2 symbolic constants.
The first constant is an integer that represents the maximum number of days that will be worked. The value should be 30.
The second constant is an integer that represents the maximum starting salary. The value should be 10.
Programming Requirements
- Make sure to seed the random number generator by calling the srand function. This MUST be done in main before calling the rand function. The file that is handed in for grading must use srand(15);.
- 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 be a part of every program that is submitted during the semester and this will be the last reminder in the program write-ups.
- To use the random number generator, add an additional #include for the cstdlib library at the beginning of the program
- The dollar amounts should be displayed with exactly 2 digits after the decimal point.
- The program MUST use the two symbolic constants that are specified above.
- Hand in a copy of the source code (the CPP file) using Blackboard.
Output
Using srand(15); on Windows PC
******** Job 1 ******** Starting Salary: $0.08 Number of days to work: 6 Day 1: 0.08 Day 2: 0.16 Day 3: 0.32 Day 4: 0.64 Day 5: 1.28 Day 6: 2.56 For Job 1, you earned $5.04 in 6 day(s). ******** Job 2 ******** Starting Salary: $0.07 Number of days to work: 29 Day 1: 0.07 Day 2: 0.14 Day 3: 0.28 Day 4: 0.56 Day 5: 1.12 Day 6: 2.24 Day 7: 4.48 Day 8: 8.96 Day 9: 17.92 Day 10: 35.84 Day 11: 71.68 Day 12: 143.36 Day 13: 286.72 Day 14: 573.44 Day 15: 1146.88 Day 16: 2293.76 Day 17: 4587.52 Day 18: 9175.04 Day 19: 18350.08 Day 20: 36700.16 Day 21: 73400.32 Day 22: 146800.64 Day 23: 293601.28 Day 24: 587202.56 Day 25: 1174405.12 Day 26: 2348810.24 Day 27: 4697620.48 Day 28: 9395240.96 Day 29: 18790481.92 For Job 2, you earned $37580963.77 in 29 day(s). ******** Job 3 ******** Starting Salary: $0.02 Number of days to work: 3 Day 1: 0.02 Day 2: 0.04 Day 3: 0.08 For Job 3, you earned $0.14 in 3 day(s).
Using srand(15); on a Mac
******** Job 1 ******** Starting Salary: $0.06 Number of days to work: 9 Day 1: 0.06 Day 2: 0.12 Day 3: 0.24 Day 4: 0.48 Day 5: 0.96 Day 6: 1.92 Day 7: 3.84 Day 8: 7.68 Day 9: 15.36 For Job 1, you earned $30.66 in 9 day(s). ******** Job 2 ******** Starting Salary: $0.09 Number of days to work: 19 Day 1: 0.09 Day 2: 0.18 Day 3: 0.36 Day 4: 0.72 Day 5: 1.44 Day 6: 2.88 Day 7: 5.76 Day 8: 11.52 Day 9: 23.04 Day 10: 46.08 Day 11: 92.16 Day 12: 184.32 Day 13: 368.64 Day 14: 737.28 Day 15: 1474.56 Day 16: 2949.12 Day 17: 5898.24 Day 18: 11796.48 Day 19: 23592.96 For Job 2, you earned $47185.83 in 19 day(s). ******** Job 3 ******** Starting Salary: $0.02 Number of days to work: 10 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 For Job 3, you earned $20.46 in 10 day(s).
Using srand(15); on CodeChef
******** Job 1 ******** Starting Salary: $0.04 Number of days to work: 1 Day 1: 0.04 For Job 1, you earned $0.04 in 1 day(s). ******** Job 2 ******** Starting Salary: $0.08 Number of days to work: 6 Day 1: 0.08 Day 2: 0.16 Day 3: 0.32 Day 4: 0.64 Day 5: 1.28 Day 6: 2.56 For Job 2, you earned $5.04 in 6 day(s). ******** Job 3 ******** Starting Salary: $0.07 Number of days to work: 16 Day 1: 0.07 Day 2: 0.14 Day 3: 0.28 Day 4: 0.56 Day 5: 1.12 Day 6: 2.24 Day 7: 4.48 Day 8: 8.96 Day 9: 17.92 Day 10: 35.84 Day 11: 71.68 Day 12: 143.36 Day 13: 286.72 Day 14: 573.44 Day 15: 1146.88 Day 16: 2293.76 For Job 3, you earned $4587.45 in 16 day(s).
Extra Credit 1
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 1 Output (on Windows PC)
******** Job 1 ******** Starting Salary: $0.08 Number of days to work: 6 Daily Salary Amount Earned Day 1: 0.08 0.08 Day 2: 0.16 0.24 Day 3: 0.32 0.56 Day 4: 0.64 1.20 Day 5: 1.28 2.48 Day 6: 2.56 5.04 For Job 1, you earned $5.04 in 6 day(s). ******** Job 2 ******** Starting Salary: $0.07 Number of days to work: 29 Daily Salary Amount Earned Day 1: 0.07 0.07 Day 2: 0.14 0.21 Day 3: 0.28 0.49 Day 4: 0.56 1.05 Day 5: 1.12 2.17 Day 6: 2.24 4.41 Day 7: 4.48 8.89 Day 8: 8.96 17.85 Day 9: 17.92 35.77 Day 10: 35.84 71.61 Day 11: 71.68 143.29 Day 12: 143.36 286.65 Day 13: 286.72 573.37 Day 14: 573.44 1146.81 Day 15: 1146.88 2293.69 Day 16: 2293.76 4587.45 Day 17: 4587.52 9174.97 Day 18: 9175.04 18350.01 Day 19: 18350.08 36700.09 Day 20: 36700.16 73400.25 Day 21: 73400.32 146800.57 Day 22: 146800.64 293601.21 Day 23: 293601.28 587202.49 Day 24: 587202.56 1174405.05 Day 25: 1174405.12 2348810.17 Day 26: 2348810.24 4697620.41 Day 27: 4697620.48 9395240.89 Day 28: 9395240.96 18790481.85 Day 29: 18790481.92 37580963.77 For Job 2, you earned $37580963.77 in 29 day(s). ******** Job 3 ******** Starting Salary: $0.02 Number of days to work: 3 Daily Salary Amount Earned Day 1: 0.02 0.02 Day 2: 0.04 0.06 Day 3: 0.08 0.14 For Job 3, you earned $0.14 in 3 day(s).
Extra Credit 2
For up to 5 points of extra credit, add code to determine which job paid the most money and display a message that tells the user which job paid the most.
Extra Credit 1 DOES NOT have to be attempted to receive the extra credit points for Extra Credit 2.
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 2 Output (on Windows PC)
******** Job 1 ******** Starting Salary: $0.08 Number of days to work: 6 Daily Salary Amount Earned Day 1: 0.08 0.08 Day 2: 0.16 0.24 Day 3: 0.32 0.56 Day 4: 0.64 1.20 Day 5: 1.28 2.48 Day 6: 2.56 5.04 For Job 1, you earned $5.04 in 6 day(s). ******** Job 2 ******** Starting Salary: $0.07 Number of days to work: 29 Daily Salary Amount Earned Day 1: 0.07 0.07 Day 2: 0.14 0.21 Day 3: 0.28 0.49 Day 4: 0.56 1.05 Day 5: 1.12 2.17 Day 6: 2.24 4.41 Day 7: 4.48 8.89 Day 8: 8.96 17.85 Day 9: 17.92 35.77 Day 10: 35.84 71.61 Day 11: 71.68 143.29 Day 12: 143.36 286.65 Day 13: 286.72 573.37 Day 14: 573.44 1146.81 Day 15: 1146.88 2293.69 Day 16: 2293.76 4587.45 Day 17: 4587.52 9174.97 Day 18: 9175.04 18350.01 Day 19: 18350.08 36700.09 Day 20: 36700.16 73400.25 Day 21: 73400.32 146800.57 Day 22: 146800.64 293601.21 Day 23: 293601.28 587202.49 Day 24: 587202.56 1174405.05 Day 25: 1174405.12 2348810.17 Day 26: 2348810.24 4697620.41 Day 27: 4697620.48 9395240.89 Day 28: 9395240.96 18790481.85 Day 29: 18790481.92 37580963.77 For Job 2, you earned $37580963.77 in 29 day(s). ******** Job 3 ******** Starting Salary: $0.02 Number of days to work: 3 Daily Salary Amount Earned Day 1: 0.02 0.02 Day 2: 0.04 0.06 Day 3: 0.08 0.14 For Job 3, you earned $0.14 in 3 day(s). The highest paying job was job 2 paying $37580963.77 The lowest paying job was job 3 paying $0.14