Help with functions in a loop

I have this code that works and i am trying to do this:
You must add a function to your program called CalcInterest. This function will take as its ONLY parameter an Account, and return the Interest calculated as shown in Part 1. Your main program should now use this function instead


I have done all the previous parts but am really struggling with this part for hours! if someone could provide an example or show me how to do it i would be eternally greatful

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
#include <iostream>
using namespace std;
const int MAXACCOUNTS = 8;
int interest(int Balance, int MAXACCOUNTS);
struct Account
{
int Number;
double Balance;
int DaysSinceDebited;
};

int main()

{
int Accountnumber;
double Balance;
int DaysSinceDebited;
double Total[MAXACCOUNTS] = {};


Account accounts[MAXACCOUNTS];

accounts[0].Number = 1001;
accounts[0].Balance = 4254.40;
accounts[0].DaysSinceDebited = 20;

accounts[1].Number = 7940;
accounts[1].Balance = 270006.25;
accounts[1].DaysSinceDebited = 35;

accounts[2].Number = 4382;
accounts[2].Balance = 123.50;
accounts[2].DaysSinceDebited = 2;

accounts[3].Number = 2651;
accounts[3].Balance = 85326.92;
accounts[3].DaysSinceDebited = 14;

accounts[4].Number = 3020;
accounts[4].Balance = 657.0;
accounts[4].DaysSinceDebited = 5;

accounts[5].Number = 7168;
accounts[5].Balance = 7423.34;
accounts[5].DaysSinceDebited = 360;

accounts[6].Number = 6285;
accounts[6].Balance = 4.99;
accounts[6].DaysSinceDebited = 1;

accounts[7].Number = 9342;
accounts[7].Balance = 107964.44;
accounts[7].DaysSinceDebited = 45;





for (int i = 0; i < MAXACCOUNTS; i++)
{

if ((accounts[i].Balance > 10000) || (accounts[i].DaysSinceDebited>30))
    Total[i] = accounts[i].Balance * 1.06; //6% interest added
else Total[i] = accounts[i].Balance * 1.03; //3% interest added
cout << accounts[i].Number << " has a balance of " << accounts[i].Balance <<  ". The amount with interest is: " << Total[i] << endl;

system("pause");
}
}
> You must add a function to your program called CalcInterest.
> This function will take as its ONLY parameter an Account, and return the Interest calculated

1
2
3
4
5
6
7
8
double CalcInterest( Account a )
{
    double interest_rate = 0.03 ; // 3%

    if ( ( a.Balance > 10000 ) || ( a.DaysSinceDebited > 30 ) ) interest_rate = 0.06 ; // 6%

    return a.Balance * interest_rate ;
}


> Your main program should now use this function instead

1
2
3
4
5
6
7
8
// ...
for ( int i = 0; i < MAXACCOUNTS; ++i )
{
    std::cout << accounts[i].Number << " has a balance of " << accounts[i].Balance
              << ". The amount with interest is: "
              << accounts[i].Balance + CalcInterest( accounts[i] ) << '\n' ;
}
// ... 
Topic archived. No new replies allowed.