m8swezey

10-23-2009, 10:00 AM

Hello, I just have a few questions about how to do this assignmnet.

I have the forumla for it already, just can't seem to wrap my mind around how I'm going to printout the Yearly total each year.

For 30 years,annual interest at .05, compounded monthly, depositing 200 a month, printout the total amount each year at jan 1.

I have my idea on how to write out the basic code, but what throws it down the drain is how i have to printout Jan 1 20XX each year for the total amount and still add 200 a month.

what would be better, a do while loop or just a plain while loop?

Not asking anyone to do it for me, i just need to get my mind a jump start so I can figure out the rest on my own.

thanks

m8swezey

10-23-2009, 10:41 AM

//This program will calculate Hulk Hogan's investment in a Roth IRA at

//5% interest annualy compounded monthly at the first of each month for 30 years or 360 months.

//Using the formual M=P(1+I)^N where I is .05/12 and N is the number of months (360). Deposits $200

//each month starting Jan 1 2010 ending Dec 1 2039.

//Wrote by Matt Swezey CMPS 1043-101

#include <fstream>

#include<cmath>

#include<iomanip>

using namespace std;

void main()

{

ofstream outfile;

ifstream infile;

outfile.open("yearlyamount.txt");

double finalAmount;

double principal;

double intperyr;

int years;

int months;

principal=200;

months=0;

intperyr=.00416666667;

do

{

finalAmount=principal*pow((1+intperyr),12);

principal=finalAmount+200;

outfile << fixed << setprecision(2) << principal << endl;

months++;

} while (months<360);

outfile.close();

}

this is my basic, raw code that ive written up so far. just need a way to print out the Jan 1 of every year amount: not all 360 months which is what i have. Ill continue to work on it, but much help is welcome!

m8swezey

10-23-2009, 11:28 AM

//This program will calculate Hulk Hogan's investment in a Roth IRA at

//5% interest annualy compounded monthly at the first of each month for 30 years or 360 months.

//Using the formual M=P(1+I)^N where I is .05/12 and N is the number of months (360). Deposits $200

//each month starting Jan 1 2010 ending Dec 1 2039.

//Wrote by Matt Swezey CMPS 1043-101

#include <fstream>

#include<cmath>

#include<iomanip>

using namespace std;

void main()

{

ofstream outfile;

ifstream infile;

outfile.open("yearlyamount.txt");

double finalAmount;

double principal;

double interestPerMonth;

int year;

int months;

double FinalAmount;

principal=0;

months=0;

interestPerMonth=.00416666667;

year=2010;

outfile << "Matt Swezey \n";

outfile << "CMPS 1043-101 \n";

outfile << "Due: October 28, 2009\n\n\n\n\n";

do

{

finalAmount=principal*pow((1+interestPerMonth),12);

principal=finalAmount+200;

finalAmount=principal;

months++;

if (months==1)

{

outfile << "Janurary 1, " << year << fixed << setprecision(2) << "....." << setw(15) << finalAmount

<< " *First Deposit*" << endl;

year++;

}

else

if(months==13||months==25||months==37||months==49||months==61||months==73||months==85||months==97||m onths==109||

months==121||months==133||months==145||months==157||months==169||months==181||months==193||months==2 05||months==217

||months==229||months==241||months==253||months==265||months==277||months==289||months==301||months= =313||months==325

||months==337||months==349)

{

outfile << "Janurary 1, " << year << fixed << setprecision(2) << "....." << setw(15) << finalAmount << endl;

year++;

}

else

if(months==361)

{

FinalAmount=finalAmount-200;

outfile << "Janurary 1, " << year << fixed << setprecision(2) << "....." << setw(15) << FinalAmount << endl;

}

} while (months<361);

outfile.close();

}

just need a counting system for the years if possible? ran out of ideas for the moment. Thanks

TheShaner

10-23-2009, 03:03 PM

First, get rid of all those if statements and replace it with the one I'll explain below.

Using your months counter, you want to check every 12 months. To check when to print, you'll divide the months counter by 12. If you have a remainder of zero (meaning the count is divisible by 12, i.e. 12, 24, 36, 48, etc), you'll print; else you won't.

To find the remainder, you must use the modulus operator. In C++ and many other languages, the modulus operator is the percent (%) sign. So you'll do:

if((months % 12) == 0)

{

// Print yearly amount

// Increment year

}

You'll increment the year inside the above if statement since you're printing every 12 months.

Now, to prevent having to do that last check to see if you've gone to 361 months, you should move your months increment to the very bottom of the loop before the while check. January will start out as month zero then and it'll print that month. 12 months from then will be the following January since the months counter will be at 12 and will print.

You'll also need to calculate the interest and add it to the principle at the end of the loop after you've done your 12 month check. This is because the there is no interest to add in the first month. Your first month's print out should be the initial deposit, $200.

Since your first month is month 0, your while loop will be while(months < 360), since starting at month 0 and going to month 359 is a total of 360 months.

Thus, the process will be:

Before loop, make principle equal to 200, since this is the initial deposit and set months equal to zero.

You'll make a check using the if statement I explained above. If it passes the check, print the balance and increment the year.

Add the interest to the principle to get a new balance.

Increment months.

The while loop will check if it's still less than 360. If so, loop. If not, exit loop and do one final print statement of the balance.

You were pretty close. You just need a little rearrangement and better efficiency to check on which months to print.

Good luck!

-Shane