-
Problems with code
Hi, everyone. I am trying to solve a problem in C: given two accounts with different kinds of interests, both starting with $100,00, how many years does it take for one to have more money than the other? The code is
Code:
#include <stdio.h>
int main(void)
{
int year = 0;
float ammountDaphne = 100, ammountDeirdre = 99;
while (ammountDaphne > ammountDeirdre)
{
year++;
if (year == 1)
ammountDeirdre = 100;
ammountDaphne = ammountDaphne + 0.1*ammountDaphne;
ammountDeirdre = ammountDeirdre + 0.05*ammountDeirdre;
}
printf("It took %d years for Deirdre's investment to exceed Daphne's one. Now, Deirdre has $ %.2f and Daphne has $ %.2f.\n", year, ammountDeirdre, ammountDaphne);
return 0;
}
However, when I run the program, the final investiments of both accounts appear with strange symbols; besides, if I change the ammounts from float type to double type, the ammount of years found is different than before. Why are these two things hapenning?
Thanks in advance
-
Ummmm .... it might help if you could show us the strange symbols... a screen shot if possible...
As for the different results... when you changed to doubles did you change your printf() at the end to %lf as well?
-
1 Attachment(s)
Hi! I tried using %lf for double and float, and had strange symbols in the output in either case. I am sending two console executions of the code, the first for float, and the second for double:
-
Well, that looks the right answer -- given that Daphne gets 10% and Deirdre gets 5% interest, then Deirdre won't have more money until infinity, when they both have infinity dollars.
-
You are likely over flowing the double and the float values.
Why do YOU think that the amount increasing by 10% will ever be less that the amount increasing by 5%?
Tim S.
-
Okey dokey...
First... please don't back edit your messages. That deprives others of the chance to learn from this experience.
Now... I added a diagnostic print to you code...
Code:
#include <stdio.h>
#include <windows.h>
int main(void)
{
int year = 0;
float ammountDaphne = 100, ammountDeirdre = 99;
while (ammountDaphne > ammountDeirdre)
{
year++;
if (year == 1)
ammountDeirdre = 100;
ammountDaphne = ammountDaphne + 0.1*ammountDaphne;
ammountDeirdre = ammountDeirdre + 0.05*ammountDeirdre;
printf("%d %.2f %.2f\n",year,ammountDaphne,ammountDeirdre);
Sleep(100);
}
printf("It took %d years for Deirdre's investment to exceed Daphne's one. Now, Deirdre has $ %.2f and Daphne has $ %.2f.\n", year, ammountDeirdre, ammountDaphne);
return 0;
}
The special symbols you are seeing are #INF meaning infinite... indicating a floating point overflow because Deidre never does catch up to Daphne...
-
A float and a double with the same value, may print out slightly different amounts, because they are both (frequently), close approximations, rather than exact amounts.
That means that the float will round off before the double will, and will have a different value when you print it. Don't compare a float with a double, unless you absolutely have to.
If you change the data type to double (and you should), then use ONLY %lf with your printout for the amount.
If you have two accounts, with equal money in them, but they earn different interest, the amount of interest will be different, as soon as the interest that is compounded, is added to the account. What you have to do, I believe, is find the first time that the difference between the accounts is greater than one cent.
-
Actually, it was supposed to be
Code:
ammountDaphne = ammountDaphne + 10;
Now, when I run the program, it works fine, in either float and double versions. I appreciate the help, and apologize for my mistake.