Velocity Reviews > C++ > Simple Algorithm

# Simple Algorithm

savesdeday
Guest
Posts: n/a

 03-01-2004
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.

Victor Bazarov
Guest
Posts: n/a

 03-01-2004
"savesdeday" <(E-Mail Removed)> wrote...
> 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
first.

Mike Wahler
Guest
Posts: n/a

 03-01-2004

"savesdeday" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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.

as you show it to us, and ask specific questions about it,
that is.

-MIke

John Harrison
Guest
Posts: n/a

 03-01-2004

"savesdeday" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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

Stephen S M WONG
Guest
Posts: n/a

 03-01-2004
Hi "savesdeday" <(E-Mail Removed)>

If you were in my class, the next part of the assignment
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,

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

On Mon, 1 Mar 2004, John Harrison wrote:

>
> "savesdeday" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > 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
>
>
>

savesdeday
Guest
Posts: n/a

 03-01-2004
"Victor Bazarov" <(E-Mail Removed)> wrote in message news:<GAy0c.427406\$I06.4847449@attbi_s01>...
> "savesdeday" <(E-Mail Removed)> wrote...
> > 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
> 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.

Mike Wahler
Guest
Posts: n/a

 03-01-2004

"savesdeday" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> "Victor Bazarov" <(E-Mail Removed)> wrote in message

news:<GAy0c.427406\$I06.4847449@attbi_s01>...
> > "savesdeday" <(E-Mail Removed)> wrote...
> > > 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

Karl Heinz Buchegger
Guest
Posts: n/a

 03-01-2004
savesdeday wrote:
>
> "Victor Bazarov" <(E-Mail Removed)> wrote in message news:<GAy0c.427406\$I06.4847449@attbi_s01>...
> > "savesdeday" <(E-Mail Removed)> wrote...
> > > 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.

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

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.

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)

David Rubin
Guest
Posts: n/a

 03-01-2004
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

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown

Mike Wahler
Guest
Posts: n/a

 03-01-2004

"David Rubin" <(E-Mail Removed)> wrote in message
news:c2052t\$(E-Mail Removed)...
> 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