Changing program to use arrays.

May 31, 2017 at 3:27am
I'm pretty new to programing and quite honestly I am struggling to learn it. I have been working on beginner programs that I find online but I can't seem to get the hang of arrays. Im attempting to change this loan program I wrote to use arrays but not sure how or where to start.
Any and all help is greatly appreciated.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

double calculate_full_repayment(double p, int y, double i);
double calculate_monthly_repayment(double p, int y, double i);
void print_repayment(bool full_repayment, double p, int y, double i);

int main()
{
	int base_loan_years = 5;
	double base_loan_amount = 0;
	double base_loan_interest = 0.0;

	cout << "Welcome to Repayment of a Loan!\n\n";

	cout << "Please enter the dollar amount of your loan: ";
	cin >> base_loan_amount;

	cout << "Please enter the starting interest rate (e.g. 4.75 for 4.75%): ";
	cin >> base_loan_interest;

	cout << "\n\n";

	cout << fixed << setprecision(2) << "\t\t\t\tLoan of " << base_loan_amount << " dollars";

	cout << "\n\n";

	cout << "\t\t\t\t   Monthly Repayment";

	cout << "\n\n";

	cout << "\t\t\t\t    Interest Rates";

	cout << "\n";

	// Print header for years and rates.
	cout << "\tYears";
	for (int i = 0; i < 5; i++)
	{
		double interest = (base_loan_interest + (0.5 * i));
		cout << fixed << setprecision(2) << "\t" << setw(11) << right << interest << "%";
	}

	cout << "\n";

	// Print monthly payment.
	print_repayment(false, base_loan_amount, base_loan_years, base_loan_interest);

	cout << "\n";

	cout << "\t\t\t\tTotal Amount Paid in Repayments";

	cout << "\n";

	// Print full re-payment amount.
	print_repayment(true, base_loan_amount, base_loan_years, base_loan_interest);

	return 0;
}

void print_repayment(bool full_repayment, double p, int y, double i)
{
	// P = Base Loan Amount
	// I = Full Interest
	// Y = Base Year

	for (int row = 0; row < 6; row++)
	{
		for (int col = 0; col < 6; col++)
		{
			int years = y + (5 * row);
			if (col > 0)
			{
				double full_interest = (i + (0.5 * (col - 1)));
				double repayment;
				if (full_repayment)
				{
					repayment = calculate_full_repayment(p, years, full_interest / 100);
				}
				else
				{
					repayment = calculate_monthly_repayment(p, years, full_interest / 100);
				}

				cout << fixed << setprecision(2) << "\t$" << setw(11) << right << repayment;
			}
			else
			{
				cout << "\t" << years;
			}
		}

		cout << "\n";
	}
}

double calculate_full_repayment(double p, int y, double i)
{
	// P = Principal
	// I = Interest
	// Y = Years
	// R = Monthly Repayment
	// F = Full Repayment Amount

	// 12 monthly repayments * number of years paid across
	// F = R * 12 * Y
	double f = calculate_monthly_repayment(p, y, i) * 12 * y;
	return f;
}

double calculate_monthly_repayment(double p, int y, double i)
{	
	// P = principal
	// I = Interest
	// Y = Years
	// R = Monthly Repayment

	// R = (P * (i / 12)) * (((1 + (i / 12)) ^ (12 * y)) / (((1 + (i / 12)) ^ (12 * y)) - 1))

	// Calculate (1 + (i / 12)) ^ (12 * y) since it's used twice.
	double partial_work = pow((1 + (i / 12)), (12 * y));

	double r = (p * (i / 12)) * (partial_work / (partial_work - 1));
	return r;
}
May 31, 2017 at 5:58am
What is in your code that you want to implement as arrays? I don't understand your question.
Jun 1, 2017 at 2:56am
... change this loan program I wrote to use arrays ...

arrays (either of the C-style or std variety) are simply containers that hold data of a certain type. So think of where in your program arrays might come into play - perhaps around line 42 - if you wish to store the interest data for which you'd need an array of doubles. Needless to say you'll find numerous examples of arrays in Python online that will give you further illustrations of their uses
Topic archived. No new replies allowed.