# CS50 AP: Pennies

I'm wondering why a lot of my answers are off by 0.01 or a very small amount. Here are the results from check50:

``````:) pennies.c exists
:) pennies.c compiles
:( 28 days, 1 penny on day one yields \$2684354.55
expected "\$2684354.55\n", not "2684354.56\n"
:( 31 days, 1 penny on day one yields \$21474836.47
expected "\$21474836.47\n", not "21474836.48\n"
:( 29 days, 2 pennies on day one yields \$10737418.22
expected "\$10737418.22\n", not "10737418.24\n"
:( 30 days, 30 pennies on day one yields \$322122546.90
expected "\$322122546.90\n", not "322122547.20\n"
:) rejects days < 28 or > 31
:) rejects pennies < 1
:) rejects days == "foo"
:) rejects pennies == "foo"
:) rejects a non-numeric input of "" for days
:) rejects a non-numeric input of "" for pennies
``````

And here is my code:

``````#include <cs50.h>
#include <stdio.h>

int main(void)
{
int days = 0; // days variable
int start = 0; // starting pennies
do
{
days = get_int("Days in the month: "); //Asks user for input
}
while(days < 28 || days > 31); //Checks if the user input is any of these values

do
{
start = get_int("Day 1 penny amount: ");
}
while(start <= 0);

long long total = start;
double totalindollars = (double) total / 100;
for (int i = 0; i < days; i++)
{
totalindollars = totalindollars * 2;
}

printf("%.2f\n", totalindollars);
}
``````

Thanks in Advance EDIT: I tried to change the code to fix the problem and I'm still 0.01 off. Any ideas? Heres my code: Thanks!

``````#include <cs50.h>
#include <stdio.h>

int main(void)
{
int days = 0; // days variable
int start = 0; // starting pennies
do
{
days = get_int("Days in the month: "); //Asks user for input
}
while(days < 28 || days > 31); //Checks if the user input is any of these values

do
{
start = get_int("Day 1 penny amount: ");
}
while(start <= 0);

long long total = start;

for (int i = 0; i < days; i++)
{
total = total * 2;
}

double totalindollars = (double) total / 100;
printf("%.2f\n", totalindollars);
}
``````

Since I haven't seen the AP class, I'm going to make a few assumptions, starting with this: this pset is about understanding float imprecision. It's very important when dealing with money. ;-)

This pset is basically an old problem. Start with a penny and double it each day for a month.

[EDIT: Completely misdiagnosed the issue, so here's an updated answer.]

I won't give any suggestions, just a very interesting observation.

Each incorrect result is off by exactly the number of pennies on day 1, and it is always less.

Hmmm................

Maybe something was missed in interpreting the original problem? Does it say to double the amount each day, or to also add the double to the previous day's total?

;-)