Simple Algorithm

S

savesdeday

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.
 
V

Victor Bazarov

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.
 
M

Mike Wahler

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.

We can and certainly will help you with your code. As soon
as you show it to us, and ask specific questions about it,
that is.

-MIke
 
J

John Harrison

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.

Which parts don't you know about? The program requires you to do simple
input, simple output and calculate a formula (I'd hesitate to call it an
algorithm), you've even been given the formulae to use. Presumably you've
covered all these in class. Usually its the 'putting it all together' part
which stumps newbies.

The best advice is simple to have a go, write some code, see if it compiles
and if it compiles see if it runs correctly. When you have some code and you
can't see why it isn't working, then post it here and you'll get help.

What you won't get is people doing the work for you, that wouldn't be fair.

john
 
S

Stephen S M WONG

Hi "savesdeday" <[email protected]>

If you were in my class, the next part of the assignment
will ask you to extend your assignment 1 code by implement
a checking account (which is not interest bearing) and then,
to implement a money management account (which has a minimum
deposit level, and has to observe a fixed withdraw date).
In doing so, you learn OO concept of inheritance,
overloading and overriding of functions / methods, etc.

You were told that if you don't start your work from the 1st
assignment, you won't be able to go further in assignment 2
and assignment 3, as you have to add functionality to your
own code.

And most importantly, you were told that your fellow
lecturers / tutors do read comp.lang.c++ several times a
day! Don't you remember that your professors are always
sitting in front of computers, and working so hard, you know
now how our lives are like!

Just joking, but I can't resist, that's such a so typical
assignment!

Stephen Wong @ Hong Kong

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.

Which parts don't you know about? The program requires you to do simple
input, simple output and calculate a formula (I'd hesitate to call it an
algorithm), you've even been given the formulae to use. Presumably you've
covered all these in class. Usually its the 'putting it all together' part
which stumps newbies.

The best advice is simple to have a go, write some code, see if it compiles
and if it compiles see if it runs correctly. When you have some code and you
can't see why it isn't working, then post it here and you'll get help.

What you won't get is people doing the work for you, that wouldn't be fair.

john
 
S

savesdeday

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.
 
M

Mike Wahler

savesdeday said:
"Victor Bazarov" <[email protected]> wrote in message
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

This step needs to be broken down into more steps.
I.e. define how to calculate that formula.
Step 3 : Set the value of Interest to FinalBalance - B
Step 4 : Set the value of FinalBalance to FinalBalance - S

I believe you need to think a bit more about these first four
steps.
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.

But you haven't shown us any C++. Show us some, and we'll help
you with it.

Here's something to get you started:

#include <iostream>

double tot_interest(double balance, double rate)
{
double result = 0;
/* some work for you to do here */
return result;
}

double final_balance(double begin_balance,
double total_interest,
double service_chg)
{
double result = 0;
/* some work for you to do here */
return result;
}

int main()
{
double balance = 0;
double rate = 0;
double svc_charge = 0;

std::cout << "Beginning balance: ";
std::cin >> balance;

std::cout << "Interest rate: ";
std::cin >> rate;

std::cout << "Service charge: ";
std::cin >> svc_charg;

std::cout << "Interest earned: "
<< tot_interest(balance, rate) << '\n';

std::cout << "Final balance: "
<< final_balance(balance, rate, svc_charge) << '\n';

std::cout << "Interest earned: "
<< tot_interest(balance, rate) << '\n';

return 0;
}

You needn't organize your code exactly as I've done, this is
just to give you some ideas.

-Mike
 
K

Karl Heinz Buchegger

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.
 
D

David Rubin

Mike Wahler wrote:

[snip]
std::cout << "Beginning balance: ";
std::cin >> balance;

Out of curiosity (and lazyness!), is data written to std::cout guaranteed to
appear (on the terminal) if it is not terminated by a newline? Is this not
similar to a common mistake in C:

printf("Beginning balance: ");
scanf("%lf", &balance);

where you need to insert an fflush() statement.

/david
 
M

Mike Wahler

David Rubin said:
Mike Wahler wrote:

[snip]
std::cout << "Beginning balance: ";
std::cin >> balance;

Out of curiosity (and lazyness!), is data written to std::cout guaranteed to
appear (on the terminal) if it is not terminated by a newline?

Yes. See 'tie()'.
Is this not
similar to a common mistake in C:

printf("Beginning balance: ");
scanf("%lf", &balance);

where you need to insert an fflush() statement.

No, std::cout and std::cin are guaranteed to be 'tied'
at startup. I'll look up C&V if you like.

-Mike
 
A

Andrey Tarasevich

David said:
[snip]
std::cout << "Beginning balance: ";
std::cin >> balance;

Out of curiosity (and lazyness!), is data written to std::cout guaranteed to
appear (on the terminal) if it is not terminated by a newline?

In general case it is not guaranteed to appear. But in this particular
case it is guaranteed to appear before program begins to wait for user
input. The C++ standard I/O library provides means to "tie" input and
output streams to each other. This means, among other things, that input
request on an input stream will automatically flush the tied output
stream (if any). By default 'std::cout' is tied to 'std::cin'.
 
S

savesdeday

This is what I have so far but there seems to be a problem because
when I compile and run the program, the output for final balance and
total interest are both values of 0. Please help.
#include <iostream>
#include <cmath>
double tot_interest(double balance, double rate)
{
double result = 0;
double tot_interest= pow (1+rate/12,12)-1*balance;
return result;
}

double final_balance(double balance,
double tot_interest,
double svc_charg)
{
double result = 0;
double final_balance=balance+tot_interest - svc_charg;
return result;
}
int main()
{
double balance = 0;
double rate = 0;
double svc_charg = 0;

std::cout << "Beginning balance: ";
std::cin >> balance;

std::cout << "Interest rate: ";
std::cin >> rate;

std::cout << "Service charge: ";
std::cin >> svc_charg;

std::cout << "Interest earned: "
<< tot_interest(balance, rate) << '\n';

std::cout << "Final balance: "
<< final_balance(balance, rate, svc_charg) << '\n';

std::cout << "Interest earned: "
<< tot_interest(balance, rate) << '\n';

return 0;
}
What am I doing wrong?
 
J

John Harrison

savesdeday said:
This is what I have so far but there seems to be a problem because
when I compile and run the program, the output for final balance and
total interest are both values of 0. Please help.
#include <iostream>
#include <cmath>
double tot_interest(double balance, double rate)
{
double result = 0;
double tot_interest= pow (1+rate/12,12)-1*balance;

This correctly calculates the value of the interest (I assume) and puts that
into a variable called tot_interest.
return result;

Then this returns the value of a different variable called result which has
a value of zero.

You need to get rid of one of the variables. It doesn't matter what the
variable is called, but if you use one variable for the calculation then you
use the same variable for the return.

Like this

{
double tot_interest= pow (1+rate/12,12)-1*balance;
return tot_interest;
}

If you like you can get rid of the variable entirely, like this

{
return pow (1+rate/12,12)-1*balance;
}

john
 
M

Mike Wahler

savesdeday said:
This is what I have so far but there seems to be a problem because
when I compile and run the program, the output for final balance and
total interest are both values of 0. Please help.
#include <iostream>
#include <cmath>
double tot_interest(double balance, double rate)
{
double result = 0;
double tot_interest= pow (1+rate/12,12)-1*balance;
return result;
}

This is what happens when you take example code, and
try to use it without actually trying to understand it. :)

See John's reply.

-Mike
 
S

savesdeday

John Harrison said:
This correctly calculates the value of the interest (I assume) and puts that
into a variable called tot_interest.


Then this returns the value of a different variable called result which has
a value of zero.

You need to get rid of one of the variables. It doesn't matter what the
variable is called, but if you use one variable for the calculation then you
use the same variable for the return.

Like this

{
double tot_interest= pow (1+rate/12,12)-1*balance;
return tot_interest;
}

If you like you can get rid of the variable entirely, like this

{
return pow (1+rate/12,12)-1*balance;
}

john

Last quick question, I am having trouble incorporating the equation to
find the interest earned into C++. The equation is:
Total interest earned = [(1+I/12)12 - 1]*B, and we are supposed to
use cmath to do this. If I try to type in :
tot_interest= (pow(1+rate/12, 12)-1)*balance
and try to compile it, I get an error. What is the correct format to
implement the equation [(1+I/12)12 - 1]*B (using <cmath>) ?
 
K

Karl Heinz Buchegger

savesdeday said:
Last quick question, I am having trouble incorporating the equation to
find the interest earned into C++. The equation is:
Total interest earned = [(1+I/12)12 - 1]*B, and we are supposed to
use cmath to do this. If I try to type in :
tot_interest= (pow(1+rate/12, 12)-1)*balance
and try to compile it, I get an error.

What error?

The compiler emits error messages to give you a hint on what is wrong
in your code. Read those error messages!
What is the correct format to
implement the equation [(1+I/12)12 - 1]*B (using <cmath>) ?

What you have seems fine.
So what does the error message say?
 
O

osmium

Karl said:
savesdeday said:
Last quick question, I am having trouble incorporating the equation to
find the interest earned into C++. The equation is:
Total interest earned = [(1+I/12)12 - 1]*B, and we are supposed to
use cmath to do this. If I try to type in :
tot_interest= (pow(1+rate/12, 12)-1)*balance
and try to compile it, I get an error.

What error?

The compiler emits error messages to give you a hint on what is wrong
in your code. Read those error messages!
What is the correct format to
implement the equation [(1+I/12)12 - 1]*B (using <cmath>) ?

What you have seems fine.

I think he needs some decimal points in his code, for starters.
 
K

Karl Heinz Buchegger

osmium said:
savesdeday said:
Last quick question, I am having trouble incorporating the equation to
find the interest earned into C++. The equation is:
Total interest earned = [(1+I/12)12 - 1]*B, and we are supposed to
use cmath to do this. If I try to type in :
tot_interest= (pow(1+rate/12, 12)-1)*balance
and try to compile it, I get an error.

What error?

The compiler emits error messages to give you a hint on what is wrong
in your code. Read those error messages!
What is the correct format to
implement the equation [(1+I/12)12 - 1]*B (using <cmath>) ?

What you have seems fine.

I think he needs some decimal points in his code, for starters.

The following compiles fine for me. It uses the exact
line the OP provides. It even comes up with a result which
seems to be plausible although I have not checked the exact
numbers.

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
double tot_interest;
double rate = 0.2;
double balance = 1.0;

tot_interest= (pow(1+rate/12, 12)-1)*balance;

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top