savesdeday said:
Victor Bazarov said:
savesdeday said:
In my beginnning computer science class we were asked to translate a
simple interest problem. We are expected to write an algorithm that
gets values for the starting account balance B, annual interest rate
I, and annual service charge S. Your algorithm would then compute and
print out the total amount of interest earned during the year and the
final account balance at the end of the year (assuming that interest
is compounded monthly, and the service charge is deducted once, at the
end of the year). In this lab assignment you will convert this
algorithm into a C++ program.
A sample run of your program would look like the following:
Enter the beginning account balance: 300.00
Enter the annual interest rate: 5.5
Enter the annual service charge: 10
The total interest earned is: 16.9224
The final account balance is: 306.922
Here are some formulas for the quantities to be printed:
Total interest earned = [(1+I/12)12 - 1]*B
Final account balance = B + (total interest earned) - S
Can anyone help me out with this? Thank you kindly in advance.
No, we cannot help you here. If you need algorithms, post to newsgroup
'comp.programming' (although I am fairly certain they won't do your
homework for you either). Once you have the algorithm, we definitely
can help you with C++ part, but you need to read the FAQ section 5
first.
I'm sorry I actually did figure out the algorithm part of this
problem. Here it is:
Algorithm:
Step 1 : Get a value for B, I and S
Step 2 : Set the value of Final Balance to (1+I/12)12 B
Step 3 : Set the value of Interest to FinalBalance – B
Step 4 : Set the value of FinalBalance to FinalBalance – S
Step 5 : Print the message "Interest Earned: "
Step 6 : Print the value of Interest
Step 7 : Print the message "Final Balance: "
Step 8 : Print the value of FinalBalance
I'm sorry I didnt post that earlier, still new here. Any help with the
C++ would be appreciated. Thanks again.
So start with step 1!
Your first 'program' would be:
int main()
{
}
compile, link it and let it run. (Yes, I recommend to run such a simple
program. If for nothing else it is a simple test if you are familiar enough
with your development environment).
what was step 1?
Step 1 : Get a value for B, I and S
So you need some variables, called B, I and S. What data type should
they have? In your case it would be double, since there whole numbers
will not be good enough (Note: floating point numbers aren't a particular
good way to deal with moentary values, but for the moment ... )
Thus:
int main()
{
double B = 0.0, I = 0.0, S = 0.0;
}
compile it, link it, run it.
Now, step 1 asks for getting *values* for B, I and S. That means
from the user. Thus you extend your program:
#include <iostream>
using namespace std;
int main()
{
double B = 0.0, I = 0.0, S = 0.0;
cin >> B >> I >> S;
}
Again. Compile it, link it, run it. You expect your program to
read in 3 numbers (that's what the source code tells you). But
what happens in practice? The program waits for your input,
you enter some numbers and then the program ends. So your next
subgoal might be: check that the program has indeed read the numbers
I entered:
int main()
{
double B = 0.0, I = 0.0, S = 0.0;
cin >> B >> I >> S;
cout << "B: " << B << '\n';
cout << "I: " << I << '\n';
cout << "S: " << S << endl;
}
Again: compile it, link it, run it. Things should get more interesting
now. The program outputs something!
But then. Just having the program waiting for input isn't a good idea.
At least the program could prompt for what it expects from the user:
int main()
{
double B = 0.0, I = 0.0, S = 0.0;
cout << "Please enter 3 numbers: B, I, S" << endl;
cin >> B >> I >> S;
cout << "B: " << B << '\n';
cout << "I: " << I << '\n';
cout << "S: " << S << endl;
}
compile it, link it, run it.
....
Now you take over and write the rest of the program. Don't write
the program in one big rush. Instead use a subgoal and try to reach
that subgoal. Also think about ways to verify that the program is
doing what it is supposed to do. Eg. in steap 2 the program
will calculate something called a final balance. It is a good idea
to output that number and use some pocket calculator to verify that
the output number is correct.