Computer Not Following Formula Right

My remaining balance should be constantly going down all the way to 0 but for some reason acts like a quadratic formula.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
#include<fstream>
#include<iomanip>
#include<math.h>
using namespace std;
int main ()
{
	int LCVRows, LCVColumns, Month, MaxRows = 9, MaxColumns = 4;
	float AnnualInterestRate = 0.09;
	float MonthlyPayment = 165.25;
	float Payments;
	float PaidSoFar;
	float MonthlyInterestRate = AnnualInterestRate / 12;
	int PaymentsToBeMade;
	int TotalPayments = 36, PaymentsMade;
	cout<<fixed<<setprecision(2);
	cout<<"Month #\tMonthly Payment\tTotal Paid\tRemaining Balance"<<endl;
	for (LCVColumns = 1; LCVColumns <= MaxColumns; LCVColumns++)
	{
		for (LCVRows = 1; LCVRows <= MaxRows; LCVRows++)	
		{ 
			Month = Month + 1;
			Payments = MonthlyPayment * (( 1 - ( pow(1 + MonthlyInterestRate, (PaymentsToBeMade - TotalPayments)))) / MonthlyInterestRate);
			PaymentsToBeMade = TotalPayments - Month;
			PaidSoFar = Month * MonthlyPayment;
			cout<<Month<<"\t$"<<MonthlyPayment<<"\t\t$"<<PaidSoFar<<"\t\t$"<<Payments<<endl;
		}
	}
	return 0;
}


Output:

Month # Monthly Payment Total Paid      Remaining Balance
1       165.25          165.25          5070.34
2       165.25          330.50          164.02
3       165.25          495.75          326.82
4       165.25          661.00          488.41
5       165.25          826.25          648.79
6       165.25          991.50          807.99
7       165.25          1156.75         965.99
8       165.25          1322.00         1122.82
9       165.25          1487.25         1278.48
10      165.25          1652.50         1432.99
11      165.25          1817.75         1586.34
12      165.25          1983.00         1738.55
13      165.25          2148.25         1889.63
14      165.25          2313.50         2039.59
15      165.25          2478.75         2188.42
16      165.25          2644.00         2336.15
17      165.25          2809.25         2482.78
18      165.25          2974.50         2628.32
19      165.25          3139.75         2772.78
20      165.25          3305.00         2916.16
21      165.25          3470.25         3058.47
22      165.25          3635.50         3199.72
23      165.25          3800.75         3339.92
24      165.25          3966.00         3479.08
25      165.25          4131.25         3617.20
26      165.25          4296.50         3754.30
27      165.25          4461.75         3890.37
28      165.25          4627.00         4025.43
29      165.25          4792.25         4159.49
30      165.25          4957.50         4292.54
31      165.25          5122.75         4424.61
32      165.25          5288.00         4555.69
33      165.25          5453.25         4685.80
34      165.25          5618.50         4814.94
35      165.25          5783.75         4943.12
36      165.25          5949.00         5070.34

--------------------------------
Process exited after 2.933 seconds with return value 0
Press any key to continue . . .
Last edited on
Hello ThatGrayRock,

First I will mention that "MaxRows" and "MaxColumns" should be defined as
1
2
3
4
5
6
7
constexpr int MaxRows{ 9 };
constexpr int MaxColoums{ 4 };

or at least

const int MaxRows{ 9 };
const int MaxColoums{ 4 };

So that they can not be changed.

I would change the floats to doubles.

In line 26 you use the variable "payments" when it should be something like "remainingBalance" and subtract "payment" from "remainingBalance" to get the decreasing total. You will also need to define the variable "remainingBalance" and set the value to a starting balance.

This comes from looking at the program without testing.

Hope that helps,

Andy
Iv'e tried it out and my output is still strange.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream>
#include<fstream>
#include<iomanip>
#include<math.h>
using namespace std;
int main ()
{
	int LCVRows, LCVColumns, Month;
	const int MaxRows = 9; 
	const int MaxColumns = 4;
	double AnnualInterestRate = 0.09;
	double MonthlyPayment = 165.25;
	double RemainingBalance;
	double Balance = 5784.73;
	double Payments;
	double PaidSoFar;
	double MonthlyInterestRate = AnnualInterestRate / 12;
	int PaymentsToBeMade;
	int TotalPayments = 36, PaymentsMade;
	cout<<"Month #\tMonthly Payment\tTotal Paid\tRemaining Balance"<<endl;
	for (LCVColumns = 1; LCVColumns <= MaxColumns; LCVColumns++)
	{
		for (LCVRows = 1; LCVRows <= MaxRows; LCVRows++)	
		{ 
			const double MonthlyInterestRate = 0.0075;
			Month = Month + 1;
			Payments = MonthlyPayment * (( 1 - ( pow(1 + MonthlyInterestRate, (PaymentsToBeMade - TotalPayments)))) / MonthlyInterestRate);
			PaymentsToBeMade = TotalPayments - Month;
			PaidSoFar = Month * MonthlyPayment;
			RemainingBalance = Balance - Payments;
			cout<<fixed<<setprecision(2);
			cout<<Month<<"\t$"<<MonthlyPayment<<"\t\t$"<<PaidSoFar<<"\t\t$"<<RemainingBalance<<endl;
		}
	}
	return 0;
}

Month # Monthly Payment Total Paid      Remaining Balance
1       $165.25         $165.25         $969.82
2       $165.25         $330.50         $5620.71
3       $165.25         $495.75         $5457.91
4       $165.25         $661.00         $5296.32
5       $165.25         $826.25         $5135.94
6       $165.25         $991.50         $4976.75
7       $165.25         $1156.75                $4818.74
8       $165.25         $1322.00                $4661.92
9       $165.25         $1487.25                $4506.25
10      $165.25         $1652.50                $4351.75
11      $165.25         $1817.75                $4198.40
12      $165.25         $1983.00                $4046.19
13      $165.25         $2148.25                $3895.11
14      $165.25         $2313.50                $3745.16
15      $165.25         $2478.75                $3596.32
16      $165.25         $2644.00                $3448.59
17      $165.25         $2809.25                $3301.96
18      $165.25         $2974.50                $3156.42
19      $165.25         $3139.75                $3011.97
20      $165.25         $3305.00                $2868.59
21      $165.25         $3470.25                $2726.28
22      $165.25         $3635.50                $2585.03
23      $165.25         $3800.75                $2444.83
24      $165.25         $3966.00                $2305.67
25      $165.25         $4131.25                $2167.55
26      $165.25         $4296.50                $2030.46
27      $165.25         $4461.75                $1894.38
28      $165.25         $4627.00                $1759.32
29      $165.25         $4792.25                $1625.27
30      $165.25         $4957.50                $1492.21
31      $165.25         $5122.75                $1360.15
32      $165.25         $5288.00                $1229.07
33      $165.25         $5453.25                $1098.96
34      $165.25         $5618.50                $969.82
35      $165.25         $5783.75                $841.64
36      $165.25         $5949.00                $714.42

--------------------------------
Process exited after 0.3311 seconds with return value 0
Press any key to continue . . .
Hello ThatGrayRock,

Sorry I got into something after dinner and did not get back to this.

Computer Not Following Formula Right
Actually it is. It is your code that is not quite right.

Lines 26 - 32, with the exception of line 31, is going about what you want in the wrong way.

Given your output of:
Month # Monthly Payment Total Paid      Remaining Balance
1       $165.25         $165.25         $969.82
2       $165.25         $330.50         $5620.71
What do you expect to see for these numbers? And should I consider the interest as being part of the $165.25 amount?

I would make this change at the start of the program:
1
2
	double Balance = 5784.73;
	double RemainingBalance = Balance;

Changing the order does not make any difference right now, but a more logical way of doing these lines. In the future it should make a difference.

Try changing to this code. I think you will see that it is not what you want.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	//std::cout << "Month #\tMonthly Payment\tTotal Paid\tRemaining Balance" << std::endl;
	std::cout << "Month #\tMonthly Payment\t\tInterest\tTotal Paid\tRemaining Balance" << std::endl;

	for (int LCVColumns = 1; LCVColumns <= MaxColumns; LCVColumns++)
	{
		for (int LCVRows = 1; LCVRows <= MaxRows; LCVRows++)
		{
			const double MonthlyInterestRate = 0.0075;
			Month = Month + 1;
			double test = ((1 - (pow(1 + MonthlyInterestRate, (PaymentsToBeMade - TotalPayments)))) / MonthlyInterestRate);
			Payments = MonthlyPayment * test;
			PaymentsToBeMade = TotalPayments - Month;
			PaidSoFar = Month * MonthlyPayment;
			RemainingBalance = Balance - Payments;
			std::cout << std::fixed << std::setprecision(2);
			std::cout << Month << "\t$" << MonthlyPayment << "\t\t\t" << Payments << "\t\t$" << PaidSoFar << "\t\t$" << RemainingBalance << std::endl;
			//std::cout << Month << "\t$" << MonthlyPayment << "\t\t$" << PaidSoFar << "\t\t$" << RemainingBalance << std::endl;
		}
	}

I put the big formula on a line by its-self so you can stop the program and see what its value is for any given line. The changes I made are for testing and do not have to stay in the final program.

On line 10 when you calculate the value of "Payments" I do not think that is a number that you want. On the first run through the for loops "Payments" has a value of 5195.58. I do not think that is what you wanted. Part of the formula "(PaymentsToBeMade - TotalPayments)" the first time through the loops this has a value of -36 and "test" has a value of 31.45. Is the value of test what you expected?

In my above code line 8 does not need to be here. What you can do when you first define "MonthlyInterestRate" make it a "const".

I also have to question the use of "pow()". I do not follow the need or use of this. I believe all you need to do is multiply the "RemainingBalance" by the "MonthlyInterestRate" to get the interest for the month. Still something I have to work on.

Do you have any sample output that is correct? It is a big help to figure out what is going wrong.

Hope that helps,

Andy
Last edited on
Hello ThatGrayRock,

I have played with the program a bit and this is what I came up with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
	// All variables should be initialized before use.

	constexpr int MaxRows = 9;
	constexpr int MaxColumns = 4;
	constexpr double AnnualInterestRate = 0.09;
	double MonthlyPayment = 165.25;
	double Balance = 5784.73;
	double RemainingBalance = Balance;
	double interest{ 0.0 };  // <--- Added.
	double Payments{ 0.0 };
	double PaidSoFar{ 0.0 };
	constexpr double MonthlyInterestRate = AnnualInterestRate / 12;  // <--- Not needed in the for loop and once set should not be changed.
	int PaymentsToBeMade{ 0 }, Month{ 0 };
	int TotalPayments = 36, PaymentsMade{ 0 };

	//std::cout << "Month #\tMonthly Payment\tTotal Paid\tRemaining Balance" << std::endl;
	std::cout << "Month #\tMonthly Payment\t\tInterest\tTotal Paid\tRemaining Balance" << std::endl;

	for (int LCVColumns = 1; LCVColumns <= MaxColumns; LCVColumns++)  // <--- Add the "int" here because LCV?? was undefined.
	{
		for (int LCVRows = 1; LCVRows <= MaxRows; LCVRows++)  // <--- Add the "int" here because LCV?? was undefined.
		{
			//Month = Month + 1;
			//double test = ((1 - (pow(1 + MonthlyInterestRate, (PaymentsToBeMade - TotalPayments)))) / MonthlyInterestRate);
			//Payments = MonthlyPayment * test;
			//PaymentsToBeMade = TotalPayments - Month;
			//PaidSoFar = Month * MonthlyPayment;
			//RemainingBalance = Balance - Payments;
			//std::cout << std::fixed << std::setprecision(2);
			//std::cout << Month << "\t$" << MonthlyPayment << "\t\t\t" << Payments << "\t\t$" << PaidSoFar << "\t\t$" << RemainingBalance << std::endl;
			////std::cout << Month << "\t$" << MonthlyPayment << "\t\t$" << PaidSoFar << "\t\t$" << RemainingBalance << std::endl;
			Month++;  // <--- Same as Month = Monyh + 1.
			interest = RemainingBalance * MonthlyInterestRate;  // <--- Added the variable "interest". It makes it re understandable.
			PaidSoFar = Month * MonthlyPayment;
			RemainingBalance -= MonthlyPayment;
			//if (RemainingBalance < 0.0) continue;  // <--- To not show a negative balance. Uncomment to see how it works.
			std::cout << std::fixed << std::showpoint << std::setprecision(2);
			std::cout << Month << "\t$" << MonthlyPayment << "\t\t\t" << interest << "\t\t$" << std::setw(7)
				      << PaidSoFar << std::setw(15) << "$" << RemainingBalance << std::endl;
		}
	}


This is everything inside of main. Notice the comments on the changes I made. The "showpoint" will print a number like "100.00", without it would just print "100". I used the "setw()" to line up all the numbers. "setw()" can be used in place of the "\t"s.

Hope that helps,

Andy
Thank you Andy. You've been quite helpful.
Hello ThatGrayRock,

You are welcome. Any time. I would be interested in knowing what you finally came up with.

Andy
Topic archived. No new replies allowed.