943,749 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1644
  • C++ RSS
Feb 29th, 2008
0

Confused on which loop to use

Expand Post »
You have just purchased a stereo system that cost $1000 on the following credit
plan: No down payment, an interest rate of 18% per year (and hence 1.5% per
month), and monthly payments of $50. The monthly payment of $50 is used to
pay the interest and whatever is left is used to pay part of the remaining debt.
Hence, the first month you pay 1.5% of $1000 in interest. That is $15 in interest.
So, the remaining $35 is deducted from your debt, which leaves you with a debt
of $965.00. The next month you pay interest of 1.5% of $965.00, which is
$14.48. Hence, you can deduct $35.52 (which is $50 - $14.48) from the amount
you owe. Write a program that will tell you how many months it will take you to
pay off the loan, as well as the total amount of interest paid over the life of the
loan. Use a loop to calculate the amount of interest and the size of the debt after
each month. Use a variable to count the number of loop iterations and hence the
number of months until the debt is zero. The last payment may be less than $50,
if the debt is small, but do not forget the interest. If you owe $50, then your
monthly payment of $50 will not pay off your debt, although it will come to close.
One month’s interest on $50 is only 75 cents


That is the problem i have to do for my C++ class. I'm confused as to which kind of loop to use for this. We've ben taught, while, do-while, and for loops. I never really understood do-while or for loops, and I probably couldn't even do the above problem math wise let alone programming it. If someone could help me as to which kind of loop would be best used for this or point me in the right direction that would help a lot.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Spencicle is offline Offline
7 posts
since Feb 2008
Feb 29th, 2008
0

Re: Confused on which loop to use

My rule of thumb is to use for loops if I have control over when the loop will stop---for example if I want it to work X times or if I want it loop while X < Y, etc, AND if the increment I want to make is the same each time through the loop. If those criteria can't be met then I use a while or do/while loop. I usually use a while loop unless I want the while loop to run at least once, no matter what, in which case I use a do/while loop.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Feb 29th, 2008
0

Re: Confused on which loop to use

okay i am entirely confused on how to even do this problem.

I try things for the sake of trying them, making sure i understand the for loop, but it always loops infintely.

I don't get it when our teacher guves us an example like this:

j = 2;
for (int i = 0; i <= 5; i++)
{
cout << j << “ ”;
j = 2 * j + 3;
}
cout << j << “ ” << endl;

That was one of the written questions i had to answer for our homework, and I only know the answer because I cheated and stuck in a program and ran it. I don't understand why there is an i variable in there.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Spencicle is offline Offline
7 posts
since Feb 2008
Feb 29th, 2008
0

Re: Confused on which loop to use

That loop is saying that you are going to start the loop with i initialized to 0, and increment i by 1 for each iteration. You will keep looping as long as i is less than 5.
So, the first time through the loop i will be 0, then 1, then 2, then 3, then 4.
Then, i will be set to 5, and the condition "i <= 5" will fail so the loop will be exited. So, you are doing j = 2*j + 3; five times.
Reputation Points: 10
Solved Threads: 6
Junior Poster
plgriffith is offline Offline
100 posts
since Jan 2008
Feb 29th, 2008
0

Re: Confused on which loop to use

Okay I couldn't get it to work as a for loop so i used a while.

#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{
// declaring vairables //
double debt = 1000;
double interest;
double pay;
double tinterest = 0;
int month = 0;

cout << "You have bought a stereo that costs $1000 with the following payment plan:" << endl;
cout << "$0 down payment, 18% annual interest rate, and monthly payments of $50." << endl;
// loop to determine how many months to pay off debt, and the total interest paid //
while (debt>0)
{
interest = debt * .015;
pay = 50 - interest;
debt = debt - pay;
month = month + 1;
tinterest = tinterest + interest;
}

cout << "It will take you " << month << " months to pay off your debt." << endl;
cout << "The total amount of interest paid is $" << fixed << setprecision(2) << tinterest << endl;
}

I think it works right.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Spencicle is offline Offline
7 posts
since Feb 2008
Feb 29th, 2008
0

Re: Confused on which loop to use

Click to Expand / Collapse  Quote originally posted by plgriffith ...
That loop is saying that you are going to start the loop with i initialized to 0, and increment i by 1 for each iteration. You will keep looping as long as i is less than 5.
So, the first time through the loop i will be 0, then 1, then 2, then 3, then 4.
Then, i will be set to 5, and the condition "i <= 5" will fail so the loop will be exited. So, you are doing j = 2*j + 3; five times.
I think you mean when i is set to 6 it will fail, thus 5 iterations.

In relation to the original question, I would use a while loop with the test condition checking against the ever decreasing balance owed. Each iteration equating to a month.
Reputation Points: 11
Solved Threads: 3
Junior Poster in Training
superjacent is offline Offline
66 posts
since Nov 2007
Feb 29th, 2008
0

Re: Confused on which loop to use

Quote ...
I think you mean when i is set to 6 it will fail, thus 5 iterations.
Oops, lol. I did mean 6, but that would make it 6 iterations.
Reputation Points: 10
Solved Threads: 6
Junior Poster
plgriffith is offline Offline
100 posts
since Jan 2008
Feb 29th, 2008
0

Re: Confused on which loop to use

Click to Expand / Collapse  Quote originally posted by plgriffith ...
Oops, lol. I did mean 6, but that would make it 6 iterations.
Oops straight back at ya.......lol
Reputation Points: 11
Solved Threads: 3
Junior Poster in Training
superjacent is offline Offline
66 posts
since Nov 2007
Aug 22nd, 2010
-2
Re: Confused on which loop to use
You have just purchased a stereo system that cost $1000 on the following credit
plan: No down payment, an interest rate of 18% per year (and hence 1.5% per
month), and monthly payments of $50. The monthly payment of $50 is used to
pay the interest and whatever is left is used to pay part of the remaining debt.
Hence, the first month you pay 1.5% of $1000 in interest. That is $15 in interest.
So, the remaining $35 is deducted from your debt, which leaves you with a debt
of $965.00. The next month you pay interest of 1.5% of $965.00, which is
$14.48. Hence, you can deduct $35.52 (which is $50 - $14.48) from the amount
you owe. Write a program that will tell you how many months it will take you to
pay off the loan, as well as the total amount of interest paid over the life of the
loan. Use a loop to calculate the amount of interest and the size of the debt after
each month. Use a variable to count the number of loop iterations and hence the
number of months until the debt is zero. The last payment may be less than $50,
if the debt is small, but do not forget the interest. If you owe $50, then your
monthly payment of $50 will not pay off your debt, although it will come to close.
One month’s interest on $50 is only 75 cents
Reputation Points: 10
Solved Threads: 0
Newbie Poster
menalyn is offline Offline
1 posts
since Aug 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Plz help! Segmentation error in vector
Next Thread in C++ Forum Timeline: arrays in c++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC