Help with random number generations

This is my assignment, the goal is to look like this but with 30 days:

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


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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 #include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
srand(1); 

srand(time());

double Salary, made, bonuses; 
int days;

days = rand() % 6;
Salary = rand() % 0.06 + 0.01;

	cout << "Starting Salary : ";
return 0;		     
} 
Last edited on
The modulo (%) operates on integers. Do not compute the salary as double or dollars ($). Use unsigned long long salary that is in cents. You can then, at end, divide the cents with 100.0 to show dollars.


What is "bonus"?
What is the allowed range of starting salary?
What is the allowed range of days? (you say 30, but your code says 0..5)
Last edited on
I have gotten the code correctly for the starting salary and number of days to work both being random as intended but how would I get it to break down into lists based on days worked?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
srand(1); 

srand(time(0));

double Salary, made, bonuses; 
int days, Day;

days = rand() % 30;
Salary = rand() % 6; 
Salary = Salary / 100;



	cout << "Starting Salary : " << Salary << endl
		 << "Number of days to work: " << days << endl
		 << "*************************************************" << endl
		 << "          Daily Salary          " << "Amount Earned" << endl
 		 << "Day  1: " << endl;
if (days >= 2)
	{cout << "Day  2: "  << endl;}
if (days >= 3)
	{cout << "Day  3: "  << endl;}
if (days >= 4)
	{cout << "Day  4: "  << endl;}
if (days >= 5)
	{cout << "Day  5: "  << endl;}
if (days >= 6)
	{cout << "Day  6: "  << endl;}
if (days >= 7)
	{cout << "Day  7: "  << endl;}
if (days >= 8)
	{cout << "Day  8: "  << endl;}
if (days >= 9)
	{cout << "Day  9: "  << endl;}
if (days >= 10)
	{cout << "Day  10: "  << endl;}
if (days >= 11)
	{cout << "Day  11: "  << endl;}
if (days >= 12)
	{cout << "Day  12: "  << endl;}
if (days >= 13)
	{cout << "Day  13: "  << endl;}
if (days >= 14)
	{cout << "Day  14: "  << endl;}
if (days >= 15)
	{cout << "Day  15: "  << endl;}
if (days >= 16)
	{cout << "Day  16: "  << endl;}
if (days >= 17)
	{cout << "Day  17: "  << endl;}
if (days >= 18)
	{cout << "Day  18: "  << endl;}
if (days >= 19)
	{cout << "Day  19: "  << endl;}
if (days >= 20)
	{cout << "Day  20: "  << endl;}
if (days >= 20)
	{cout << "Day  20: "  << endl;}
if (days >= 21)
	{cout << "Day  21: "  << endl;}
if (days >= 22)
	{cout << "Day  22: "  << endl;}
if (days >= 23)
	{cout << "Day  23: "  << endl;}
if (days >= 24)
	{cout << "Day  24: "  << endl;}
if (days >= 25)
	{cout << "Day  25: "  << endl;}
if (days >= 26)
	{cout << "Day  26: "  << endl;}
if (days >= 27)
	{cout << "Day  27: "  << endl;}
if (days >= 28)
	{cout << "Day  28: "  << endl;}
if (days >= 29)
	{cout << "Day  29: "  << endl;}
if (days >= 30)
	{cout << "Day  30: "  << endl;}
	


Idk if this is too complex?
Last edited on
Bump please!
Hi,
The biggest merit of programming is that you can do a repeated chore easily
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
if (days >= 2)
	{cout << "Day  2: "  << endl;}
if (days >= 3)
	{cout << "Day  3: "  << endl;}
if (days >= 4)
	{cout << "Day  4: "  << endl;}
if (days >= 5)
	{cout << "Day  5: "  << endl;}
if (days >= 6)
	{cout << "Day  6: "  << endl;}
if (days >= 7)
	{cout << "Day  7: "  << endl;}
if (days >= 8)
	{cout << "Day  8: "  << endl;}
if (days >= 9)
	{cout << "Day  9: "  << endl;}
if (days >= 10)
	{cout << "Day  10: "  << endl;}
if (days >= 11)
	{cout << "Day  11: "  << endl;}
if (days >= 12)
	{cout << "Day  12: "  << endl;}
if (days >= 13)
	{cout << "Day  13: "  << endl;}
if (days >= 14)
	{cout << "Day  14: "  << endl;}
if (days >= 15)
	{cout << "Day  15: "  << endl;}
if (days >= 16)
	{cout << "Day  16: "  << endl;}
if (days >= 17)
	{cout << "Day  17: "  << endl;}
if (days >= 18)
	{cout << "Day  18: "  << endl;}
if (days >= 19)
	{cout << "Day  19: "  << endl;}
if (days >= 20)
	{cout << "Day  20: "  << endl;}
if (days >= 20)
	{cout << "Day  20: "  << endl;}
if (days >= 21)
	{cout << "Day  21: "  << endl;}
if (days >= 22)
	{cout << "Day  22: "  << endl;}
if (days >= 23)
	{cout << "Day  23: "  << endl;}
if (days >= 24)
	{cout << "Day  24: "  << endl;}
if (days >= 25)
	{cout << "Day  25: "  << endl;}
if (days >= 26)
	{cout << "Day  26: "  << endl;}
if (days >= 27)
	{cout << "Day  27: "  << endl;}
if (days >= 28)
	{cout << "Day  28: "  << endl;}
if (days >= 29)
	{cout << "Day  29: "  << endl;}
if (days >= 30)
	{cout << "Day  30: "  << endl;}


can just be replaced by a for loop
http://www.cplusplus.com/doc/tutorial/control/
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    double salary = 0, bonus = 0, total_pay = 0.0;
    int days = 0;
    
    cout << "How many days? ";
    cin >> days;
    
    salary = (rand() % 10)/100.00;
    
    cout << "Starting Salary : " << salary << endl;
    total_pay = salary;
    
    cout << "DAY          PAY           BONUS           TOTAL\n";
    cout << "================================================\n";
    
    for (int i = 0; i < days; i++)
    {
        cout << i + 1 << '\t'
        << fixed << setprecision(2) << setw(12) << salary << '\t'
        << setw(12) << bonus << '\t'
        << setw(12) << total_pay + bonus << endl;
        
        salary *= 2;
        
        if ( (i+1) % 5 == 0)
            bonus = 0.50;
        else
            bonus = 0.00;
        
        total_pay += salary;
    }
    
    return 0;		     
}
Last edited on
Here's my code, It also has a few more "security measures".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <windows.h>
using namespace std;

int main()
{
    long double salary = 0, bonus = 0, total_pay = 0.0;
    long int days = 0;

    cout << "How many days? ";
    cin >> days;
if(days >46)
{
    cout<<"I am sorry but a number of days this big will break the program"<<endl;
}
else
{

    salary = (rand() % 10)/100.00;

    cout << "Starting Salary : " << salary << endl;
    total_pay = salary;

    cout << "DAY          PAY           BONUS           TOTAL\n";
    cout << "================================================\n";

    for (int i = 0; i < days; i++)
    {
        Sleep(20);
        cout << i + 1 << '\t'
        << fixed << setprecision(2) << setw(12) << salary << '\t'
        << setw(12) << bonus << '\t'
        << setw(12) << total_pay + bonus << endl;

        salary *= 2;

        if ( (i+1) % 5 == 0)
            bonus = 0.50;
        else
            bonus = 0.00;

        total_pay += salary;
    }

}
    return 0;
}

Cheers! And good luck!
WARNING: This program here works only on windows. Heres one which works on all platforms,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>
using namespace std;

int main()
{
    long double salary = 0, bonus = 0, total_pay = 0.0;
    long int days = 0;

    cout << "How many days? ";
    cin >> days;
if(days >46)
{
    cout<<"I am sorry but a number of days this big will break the program"<<endl;
}
else
{

    salary = (rand() % 10)/100.00;

    cout << "Starting Salary : " << salary << endl;
    total_pay = salary;

    cout << "DAY          PAY           BONUS           TOTAL\n";
    cout << "================================================\n";

    for (int i = 0; i < days; i++)
    {
        _sleep(20);
        cout << i + 1 << '\t'
        << fixed << setprecision(2) << setw(12) << salary << '\t'
        << setw(12) << bonus << '\t'
        << setw(12) << total_pay + bonus << endl;

        salary *= 2;

        if ( (i+1) % 5 == 0)
            bonus = 0.50;
        else
            bonus = 0.00;

        total_pay += salary;
    }

}
    return 0;
}

One of the methods is deprecated though,
:D Good luck!
Last edited on
closed account (48T7M4Gy)
Excellent, I like the Windows one, especially the buttons.
What exactly is this doing?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
   for (int i = 0; i < days; i++)
    {
        _sleep(20);
        cout << i + 1 << '\t'
        << fixed << setprecision(2) << setw(12) << salary << '\t'
        << setw(12) << bonus << '\t'
        << setw(12) << total_pay + bonus << endl;

        salary *= 2;

        if ( (i+1) % 5 == 0)
            bonus = 0.50;
        else
            bonus = 0.00;

        total_pay += salary;
    }


Sorry for the late reply and thanks for your help! I know what i++ does and if statements but mainly i am curious as to whats going on in the bold
http://www.cplusplus.com/reference/iomanip/

read that... its pretty useful
Last edited on
Thanks, what does _sleep(20) do?
closed account (48T7M4Gy)
https://social.msdn.microsoft.com/Forums/vstudio/en-US/70d6b00b-9708-4d94-893f-0f3ae7b3ef20/sleepint?forum=vcgeneral

sleep allows a pause in proceedings.

A waste of time, space and trouble in most cases and that's why you don't see it much. The purists will probably be more condemnatory and express shock/horror at the mere thought of using it.

Maybe it has some use but not much. You don't need it here. :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
srand(1);

srand(time(0)); 

double salary, bonuses, basepay; 
int Day, bearned, total; // delcaring intergers

Day = rand() % 30; // randomizing days
basepay >= 1; // making sure basepay isn't less then 1
basepay = rand() % 6; //basepay randomized but never more then 6
basepay = basepay / 100; //basepay divided by 100 to get 0.01





	cout << "Starting basepay : " << basepay << endl
		 << "Number of days to work: " << Day << endl //displaying the basepay and day
		 << "*************************************************" << endl
		 << setw(42)<< "Daily Salary" << setw(30)<< "Amount Earned" << endl; //formatting
		 
	for (int d = 1; d <= Day; d++) //beginning my loop, d representing day which is less then or equal to the interger of Day and incrementing it by 1 until it reaches the integer Day
		{
			
			cout << "Day " << d << '\t' // outputting d
			<< fixed << setprecision(2) << setw(30) << salary << '\t' //formatting and displaying the basepay
			<< setw(30) <<  bonuses + salary << endl; //displaying amount earned
			
			salary = basepay *= 2; // Incrementing basepay by *2

			
			if ( (d+1)% 5 == 0)
				bonuses = salary * 10; 
			else 
				bonuses = 0.00; // if else statement for if theres a bonus or not
		}
		bearned = Day / 5;
		total = bonuses + basepay; 
				
			if ( bearned > 1)
				cout << endl << "You made " << total << " and earned " << bearned << " bonuses of $" << bonuses; 
			else 
				cout << endl << "You made " << total << " and earned " << bearned << " bonus of $" << bonuses; // if else statement to display proper grammar as well as how many bonuses earned


	



 return 0;		     
} 


sometimes I will get all 0s, sometimes I get nothing, sometimes I get incorrect math, can someone help me optimize this? I know it was posted above but I am trying to put it in my own "words" persay. I know its unfinished as well, I still need to display the amount of bonuses earned and total earned and how much the bonuses were and for it to actually display correctly and consistently
Last edited on
A compiler says:
 In function 'int main()':
18:9: warning: statement has no effect [-Wunused-value]
47:19: warning: 'bonuses' may be used uninitialized in this function [-Wmaybe-uninitialized]
36:30: warning: 'salary' may be used uninitialized in this function [-Wmaybe-uninitialized] 

uninitialized == How much is unknown+2 ? Unknown

There are other things, like line 10, that the compiler does not complain about.

Your "bonuses" is either 0 or 10*salary(of the last day).
Last edited on
Topic archived. No new replies allowed.