Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Simple Algorithm

Reply
Thread Tools

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.
 
Reply With Quote
 
 
 
 
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
can help you with C++ part, but you need to read the FAQ section 5
first.


 
Reply With Quote
 
 
 
 
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.


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


 
Reply With Quote
 
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


 
Reply With Quote
 
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
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

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

 
Reply With Quote
 
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
> 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.
 
Reply With Quote
 
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


 
Reply With Quote
 
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.


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.

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
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
 
Reply With Quote
 
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple Python Based Genetic Algorithm Framework Vishal Patil Python 0 11-08-2006 04:25 AM
Filtered Back Projection Algorithm (FBP Algorithm) Bapaiah Katepalli VHDL 1 06-23-2006 04:50 PM
Simple algorithm for combining differently exposed images? 223rem Digital Photography 2 11-16-2005 03:26 PM
simple algorithm for finding primes someone else C Programming 32 12-12-2003 08:07 PM
Key generation algorithm and Cipher algorithm Ahmed Moustafa Java 0 11-15-2003 06:35 AM



Advertisments