0

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);
}
0

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?

;-)

If this isn't it, please post a comment with a link to the original pset descriptiion. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • I have tried to change the integer later, and I'm still off by a small amount. Any ideas? Thanks! – Josh Cowan Feb 16 '19 at 1:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .