//Ashton Dreiling
//Pennies for pay exercise
#include <iostream>
#include <stdlib.h>
usingnamespace std;
//module prototype
void whichLoop (double days);
//global constants
constdouble ZERO_POINT_ZERO_ONE_FOR_CALCULATIONS = 0.01;
constdouble ZERO_FOR_CALCULATIONS = 0;
constdouble ONE_FOR_CALCULATIONS = 1;
constdouble HUNDRED_FOR_CALCULATIONS = 100;
constdouble TWO_FOR_PENNY_CAL = 2;
int main()
{
// some variables
double days;
//Display purpose of program and table headings
cout << "Today we are going to calculate the amount of money you'd earn over a period of time" << endl;
cout << "if your salary is one penny the first day and doubles from there." << endl;
cout << "Enter a number of days" << endl;
cin >> days;
cout << "Day\t\tPay\t\tTotal" << endl;
cout << "---------------------------------------" << endl;
whichLoop (days);
system("Pause");
return 0;
}//end main
void whichLoop (double days)
{
double penny = ZERO_POINT_ZERO_ONE_FOR_CALCULATIONS;
double counter = ONE_FOR_CALCULATIONS;
double salary;
double total;
double totalPay;
while(counter <= days)
{
salary = (penny );
total = total + salary;
penny = (penny * TWO_FOR_PENNY_CAL);
counter = (counter + ONE_FOR_CALCULATIONS);
cout << counter << "\t\t" << salary << "\t\t" << total << endl;
totalPay = total;
}//end of while loop
cout << "The total payment of your time working here will be $ " << totalPay << endl;
}//end of whichLoop module
Today we are going to calculate the amount of money you'd earn over a period of time
if your salary is one penny the first day and doubles from there.
Enter a number of days
31
Day Pay Total
---------------------------------------
2 0.01 0.01
3 0.02 0.03
4 0.04 0.07
5 0.08 0.15
6 0.16 0.31
7 0.32 0.63
8 0.64 1.27
9 1.28 2.55
10 2.56 5.11
11 5.12 10.23
12 10.24 20.47
13 20.48 40.95
14 40.96 81.91
15 81.92 163.83
16 163.84 327.67
17 327.68 655.35
18 655.36 1310.71
19 1310.72 2621.43
20 2621.44 5242.87
21 5242.88 10485.8
22 10485.8 20971.5
23 20971.5 41943
24 41943 83886.1
25 83886.1 167772
26 167772 335544
27 335544 671089
28 671089 1.34218e+06
29 1.34218e+06 2.68435e+06
30 2.68435e+06 5.36871e+06
31 5.36871e+06 1.07374e+07
32 1.07374e+07 2.14748e+07
The total payment of your time working here will be $ 2.14748e+07
I entered 31 and the loop goes form 2 to 32, so there's is some problem with your counter
also last 5 values were in mantissa form so you need to work them too
For some reason, my professor makes me do them. He says it's good to do in programming. I think it's not necessary once you get out of the beginner range, but for now, we lose points if there are any unnamed numeric literals in our code.
@FBHSIE, your instructor taught you some VERY GOOD advice, to use symbolic constants. :)
@p007,
why replace a symbolic constant with a literal/magic number? The OP declared a symbolic constant, just use that: double counter = ZERO_FOR_CALCULATIONS;
If you're going to use "ZERO_FOR_CALCULATIONS", you may as well just use 0. Symbolic constants can be useful, but not when they're given bad names. A name that contains the value of the constant is useless. It's like comments such as
1 2
// increment i by 1
++i;
It's the difference between saying what is happening and saying why it's happening.
A name can also be bad if it's irrelevant.
1 2
constdouble superman = log(2.0);
constdouble batman = log(3.0);
The OP said his instructor told him to use all those symbolic constants:
For some reason, my professor makes me do them. He says it's good to do in programming. I think it's not necessary once you get out of the beginner range, but for now, we lose points if there are any unnamed numeric literals in our code
I think names such as ZERO_FOR_CALCULATIONS and ZERO_POINT_ZERO_ONE_FOR_CALCULATIONS are what is known as following the letter rather than the spirit of the rule.
Sorry, I'm not very good with names. We just can't have any unnamed literals, and sometimes the values used are merely values with no other meaning than to serve as a number which leaves me with less choices.
You are following your instructor's rules, and that is what matters. :)
I might use different names, and not have everything be a symbolic constant, but that is me.
I would recommend you read the links I gave earlier to get a deeper understanding of the use of symbolic constants. And when NOT to use them in real C++ code.