### Coding Challenge: Why does it cout 1 ?

This is the challenge:

Doubled Pay
An employee working at a very bizzare company, earns one penny on their first day. However, for every day that passes, their base amount doubles, so they earn two pennies on the second day and four pennies on the third day (totalling 7 pennies). Given a number of days, return how many pennies the employee accumulates.

Examples
doubledPay(1) ➞ 1

doubledPay(2) ➞ 3

doubledPay(3) ➞ 7

 ``12345678910111213141516171819202122232425`` ``````#include using namespace std; int main() { int days; int moneyEarned; int penny=1; cout << "Enter the amount of days: "<> days; int aa= moneyEarned + days + 1; for(int i=0; i<=days; i++){ if(days>2){ moneyEarned= penny; moneyEarned+aa; }else if(days=1){ cout << 1; }else{ cout << 2; } } cout << "The amount of money earned: "< Oh yeah at else{} it should cout 3, not 2, my bad. ``123456789`` ``````#include int main() { std::cout << "Enter days: "; int days; std::cin >> days; std::cout << (1ul << days) - 1 << '\n'; }`````` Hello lazylife,

Look at: `int aa= moneyEarned + days + 1;` or another way`int aa= -858993460 + -858993460 + 1;`. Your first 2 numbers may be different, but they are still garbage.

ALWAYS initialize your variables when defined. From C++11 on you have the uniform initializer the {}s. It is so easy to type `int moneyEarned{};`. Empty the compiler will choose the best form of (0) zero to use or you can put something between the {}s.

The for loop needs some work.

Given your example:

doubledPay(1) ➞ 1 1 * 1 Total of 1

doubledPay(2) ➞ 2 1 * 2 + 1 Total of 3

doubledPay(3) ➞ 4 2 * 2 + 3 Total of 7

double Pay(4) ➞ 8 2 * 4 + 7 Total of 15

This is what you need to achieve.

Andy Hello lazylife,

dutch has a very nice way of bit shifting if you have studied that. If not try this:
 ``123456789101112131415161718192021222324252627282930313233`` ``````#include using namespace std; int main() { int days{}; unsigned long long moneyEarned{}; unsigned long long penny{ 1 }; cout << "\n Enter the amount of days: "; cin >> days; int aa = moneyEarned + days + 1; for (int i = 0; i <= days; i++) { moneyEarned += penny; penny *= 2; std::cout << ' ' << "The amount of money earned day " << i + 1 << " is: " << moneyEarned << '\n'; } cout << "\n\nThe total amount of money earned: " << moneyEarned << " or \$" << moneyEarned / 100 << '.' << moneyEarned % 100 << "\n\n"; // A fair C++ replacement for "system("pause")". Or a way to pause the program. // The next line may not be needed. If you have to press enter to see the prompt it is not needed. std::cin.ignore(std::numeric_limits::max(), '\n'); // <--- Requires header file . std::cout << "\n\n Press Enter to continue: "; std::cin.get(); return 0; // <--- Not required, but makes a good break point. }``````

I changed lines 8 and 9 because it does not take very long before either will become larger than an "int" can hold.

The above code produces this output:
 ``` Enter the amount of days: 4 The amount of money earned day 1 is: 1 The amount of money earned day 2 is: 3 The amount of money earned day 3 is: 7 The amount of money earned day 4 is: 15 The amount of money earned day 5 is: 31 The total amount of money earned: 31 or \$0.31 Press Enter to continue: ```

Once I could read your code this stuck out: `for(int i=0; i <= days; i++)`.

This is a good example of using the "<=" in a for loop. It produces 1 more loop than you want.

Your output should be:
 ``` Enter the amount of days: 4 The amount of money earned day 1 is: 1 The amount of money earned day 2 is: 3 The amount of money earned day 3 is: 7 The amount of money earned day 4 is: 15 The total amount of money earned: 15 or \$0.15 Press Enter to continue: ```

I do not know what monetary system you use, but this is based on US Dollars.

See what you think.

Andy

Edit: typos.
Last edited on Dutch's bit shifting is just the application of the standard formula for the summation of a geometric series.

For say day 5 the sum is 1 + 2 + 4 + 8 + 16 = 31

This is a geometric series with the start value of 1 (a) and a common ratio of 2 (r).

The standard result for the summation of a geometric series is:

a(1 - r^n)/(1 - r)

where r^n is r raised to the power n and n is the number of elements in the series to sum.

In this case a is 1 and r is 2. This then simplifies to :

(1 - 2^n)/(1 - 2) which is (1 - 2^n)/-1 which is 2^n -1

2^n in bits is simply 1 left shift n which in c++ is 1ul << n (where 1ul is 1 as type unsigned long)

n is simply the number of days. So the required answer is simply:

 `` `` ``(1ul << days) - 1``

The brackets are needed as - has a higher precedence than <<. So without the brackets it would be evaluated as 1ul << (days - 1) which is not what is required.
Last edited on
Topic archived. No new replies allowed.