0

I've been trying to finish an assignment that requires me to run a compound interest formula, but I can only use iomainip and iostream libraries.

do (cout << BaseMembershipFee * pow((ONE+(GoldRate/CompoundMonthly),(CompoundMonthly*years)) << years++) << endl;
    while (years < 11);

This works with a math library (I think), but I can't figure out how to make it without pow.

Variables:

const float BaseMembershipFee = 1200, GoldRate = 0.01, CompoundMonthly = 12, ONE = 1;
float years = 1;
bolov
  • 72,283
  • 15
  • 145
  • 224
  • 2
    Write your own `pow()`. There is no magic. –  Oct 29 '18 at 02:52
  • 1
    No magic, but trickier than it looks to get it right. Mind you, if the exponent is whole numbers, and it looks like it could be in your case, @J.Doe 's right. Just multiply the sucker out. – user4581301 Oct 29 '18 at 03:01
  • I doubt this duplicate link: the question asks for floating point exponentiation, but the linked duplicate is about integer exponentiation – M.M Oct 29 '18 at 03:12
  • Did you mean `float years = 10`, which would match your do/while loop? – Gardener Oct 29 '18 at 03:55

1 Answers1

1

In your question, it states that years = 1. Based on the do while() loop, I suspect that you meant to show years = 10.

The following code has one function to calculate the compounding every period, which in your case is every month.

Then, because it looks like you are being asked to calculate the interim amount at the end of each year, we have the second for loop.

#include<iostream>

float CompountInterestMultiplier(float ratePerPeriod, float periods) {
    float multiplier = 1.0;
    for (int i = 0; i < periods; i++) {
        multiplier *= (1.0 + ratePerPeriod);
    }
    return multiplier;
}

int main() {
    const float BaseMembershipFee = 1200;
    const float GoldRate = 0.01;  // represents annual rate at 1.0%
    const float periodsPerYear = 12.0;  // number of compounds per year.  Changed the name of this from compoundsMonthly
    const float ONE = 1.0;              // Did not use this
    const float years = 10.0;     // I think you have a typo 
                                  // and wanted this to be 10
    float ratePerPeriod = GoldRate / periodsPerYear;

    // If you want to print the value at the end of each year, 
    // then you would do this:
    float interimValue = BaseMembershipFee;
    for (int year = 0; year < 11; year++) {
        interimValue = BaseMembershipFee *
            CompountInterestMultiplier(ratePerPeriod, periodsPerYear * year);
        std::cout << "Value at end of year " << year << ": $" << interimValue << std::endl;
    }
}
Here is the output:

Value at end of year 0: $1200
Value at end of year 1: $1212.06
Value at end of year 2: $1224.23
Value at end of year 3: $1236.53
Value at end of year 4: $1248.95
Value at end of year 5: $1261.5
Value at end of year 6: $1274.17
Value at end of year 7: $1286.97
Value at end of year 8: $1299.9
Value at end of year 9: $1312.96
Value at end of year 10: $1326.15

Process finished with exit code 0
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Gardener
  • 2,591
  • 1
  • 13
  • 22