Any explanations for "0" output? (the program asks for a loan increasing by an annual interest rate each month, and how much the man must pay on average monthly in order to pay off the debt)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <math.h>
usingnamespace std;
int main() {
//loan size P, interest rate R and length of time to pay out L in months. Want required monthly payment M rounded up to whole dollars.
double p=1;
int r, l;
int count=0;
int m=0;
cin>>p>>r>>l;
do{
p=p*(r/12/100);
m=p/l+m;
p=p-m;
l--;
count++;
} while (l>0);
cout<<round(m/count); /*because m=p/l+m calculates the entire monthly value paid, and count is how many months, so m/count must be the average monthly amount) */
}
I fixed that but now I got ridiculously large outputs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <math.h>
usingnamespace std;
int main() {
//loan size P, interest rate R and length of time to pay out L in months. Want required monthly payment M rounded up to whole dollars.
double p=1;
double r, l;
int m=0, sum=0;
cin>>p>>r>>l;
int months = l;
do{ p=p*(r/0.12);
m=p/l;
sum = sum+m;
p=p-m;
l--;
} while (l!=0);
cout<<(sum/months); //because sum calculates the entire monthly value paid, and that divided by length is average amount.
}
Sorry, my interpretation and poor math implementation resulted in a way-off answer. This is closer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <math.h>
usingnamespace std;
int main() {
//loan size P, interest rate R and length of time to pay out L in months. Want required monthly payment M rounded up to whole dollars.
double p=1;
double r, l;
int m=0, sum=0;
cin>>p>>r>>l;
int months = l;
do{ p=p* (1+(r/12/100));
m=p/l;
sum = sum+m;
p=p-m;
l--;
} while (l!=0);
cout<<sum/months;
}
intput: 4600000 12 120
output: 74219
expected: 65997
hmmm... getting closer... All my outputs seem a bit too high...
m is of type int, so on line 12, it gets truncated, not rounded up. This small numerical error can result is larger numerical errors. Be very wary of integer division.
In c+=11 there is the std::round function, which works with doubles.
Is there any reason all of your variables can't be doubles?
If the number of times to loop is known, then use a for loop.
Good Luck !!
Edit:
You should be able to set a warning in your compiler, about narrowing of your type information. With g++ routinely compile with -Wall -Wextra -pedantic. The cpp shell used on this site has these compiler flags. The shell can be used when a complete program with code tags is posted, via the gear icon on the top right of the code.
Edit2:
-Wconversion is the option I was thinking of, and apparently isn't turned on by the options I mentioned above. Hopefully there is a similar option for your compiler.
#include <iostream>
#include <math.h>
usingnamespace std;
int main() {
//loan size P, interest rate R and length of time to pay out L in months. Want required monthly payment M rounded up to whole dollars.
double p=1;
double r, l;
double m=0, sum=0;
cin>>p>>r>>l;
double months = l;
do{ p=p* (1+(r/12/100));
m=p/l;
sum = sum+m;
p=p-m;
l--;
} while (l!=0);
cout<<ceil(sum/months);
}
This isn't going to fix your problem, but can I recommend you give your variables more descriptive names, so that it's easy to see at a glance what they all represent? Rather than giving them cryptic, single-letter names, and then having to refer to a comment at the start of the program to see what they mean?
EDIT: Also, if the problem is that you're not getting the values you expect, then I recommend that you learn how to step through the code with a debugger. That way, you can see how the values of your variables change as the program executes each statement in turn, and you can see exactly when and how they start to differ from what you expect.