need algorithm help
Sep 13, 2006 at 11:05 PM Thread Starter Post #1 of 13

seanohue

500+ Head-Fier
Joined
Jan 11, 2006
Posts
755
Likes
10
How do I write an algorithm in C++? My teacher assigned us HW tonight in which we need to write an algorithm for interest (10000 at 6% yearly interest, taking out 500 a month, how much long will it take to run out (I did it by hand and got 1 3/4 years, but he wants it in code)) and then one to compute pi/4 to 6 significant digits (it says like 1 - (1/3) + (1/5) - (1/7) + (1/9)....) and the fat screw didnt teach us how to do it and it is nowhere in this book. (Sorry for the anger, this is immensly frustrating).
 
Sep 13, 2006 at 11:18 PM Post #2 of 13
Quote:

Originally Posted by seanohue
How do I write an algorithm in C++? My teacher assigned us HW tonight in which we need to write an algorithm for interest (10000 at 6% yearly interest, taking out 500 a month, how much long will it take to run out (I did it by hand and got 1 3/4 years, but he wants it in code)) and then one to compute pi/4 to 6 significant digits (it says like 1 - (1/3) + (1/5) - (1/7) + (1/9)....) and the fat screw didnt teach us how to do it and it is nowhere in this book. (Sorry for the anger, this is immensly frustrating).


Some pseudo-code:

Code:

Code:
[left]monthCount = 0 amount = 10000 do { amount = amount * 1.06 for (12 iterations) { if (amount < 500), break monthCount++ decrement amount by 500 } } while(amount >= 500) return monthCount[/left]

For the pi/4 one, there's some library that has pi hardcoded as a double, so just use that and divide by four? math.h? cmath.h? I don't remember.
 
Sep 13, 2006 at 11:27 PM Post #3 of 13
what classes do I need to include with that? its rediculous, the most the entire class can do is add and multiply integers.
 
Sep 13, 2006 at 11:30 PM Post #4 of 13
Classes for? The pseudo-code? It's just a general guideline as to how you should write your C++ code.

So the general idea is:

Keep iterating infinitely until you run out of cash, and every year, multiply the amount by itself and 1.06. Also, every year, you have 12 months, so iterate 12 times where each instance you take away 500.

That's about it. There's that little if (amount < 500) guard inside the month loop so that you don't try to take out more money than you have.

Quote:

what classes do I need to include with that? its rediculous, the most the entire class can do is add and multiply integers.


Umm, perhaps there's a formula you just use and return that? I haven't a clue what it is. I was just under the assumption that a naive algorithm was what you were looking for.
 
Sep 13, 2006 at 11:42 PM Post #5 of 13
Quote:

Originally Posted by akwok
Classes for? The pseudo-code? It's just a general guideline as to how you should write your C++ code.

So the general idea is:

Keep iterating infinitely until you run out of cash, and every year, multiply the amount by itself and 1.06. Also, every year, you have 12 months, so iterate 12 times where each instance you take away 500.

That's about it. There's that little if (amount < 500) guard inside the month loop so that you don't try to take out more money than you have.



Umm, perhaps there's a formula you just use and return that? I haven't a clue what it is. I was just under the assumption that a naive algorithm was what you were looking for.



Yea the book says to make an algorithm. I looked all throughout the book though and there is nothing about writing algorithms in it. And what I mean by classes is that line #include <class>.

And this is my 500th post!
 
Sep 13, 2006 at 11:57 PM Post #6 of 13
Quote:

Originally Posted by seanohue
Yea the book says to make an algorithm. I looked all throughout the book though and there is nothing about writing algorithms in it. And what I mean by classes is that line #include <class>.


An algorithm is just a fancy way of saying 'a solution to a problem' , and there are multiple ways to solve a problem. Just like how you would choose to tackle an essay topic.

I remember one of my first CS exercises in school was to write an algorithm to play an audio CD on the computer. So, the algorithm would be to retrieve the CD jewel case, open it (my teacher wanted us to go into detail as to -how- one would open the jewel case), take out the CD, open the computer CDROM tray, put in the CD, close the computer CDROM tray, and run a music player.

What I'm trying to say is, it's probably not in the book because it's something that's supposed to be intuitive as you go on, as all programs and code are algorithms, however simple or complex they may be. If you wanted to print out "Hello World" in a program, a simple cout << "Hello World" is still an algorithm. Likewise, if you were to just print out "1.75 years", it'd still be considered correct, as it is an algorithm solving the problem, but you'd get 0 marks for it.
 
Sep 14, 2006 at 12:16 AM Post #7 of 13
wow, I had similar questions from what you ddi. One was to explain to your younger brother how to back up data, step by step. I cheated and said I have a RAID 1 array
tongue.gif
. My first project was to print Hello World too. I think I'm gonna leave them blank and just have him explain this stuff, because I'm sure its frustrating for you to explain it to me. Thanks for the help though
smily_headphones1.gif
 
Sep 14, 2006 at 12:18 AM Post #8 of 13
Quote:

Originally Posted by akwok
Some pseudo-code:

Code:

Code:
[left]monthCount = 0 amount = 10000 do { amount = amount * 1.06 for (12 iterations) { if (amount < 500), break monthCount++ decrement amount by 500 } } while(amount >= 500) return monthCount[/left]





That works perfectly as long as the interest is added at the start of the year - what the teacher might be after is adding interest monthly after each 500 is subtracted which is how the banks like to work it. Otherwise you are earning interest instantly again not something the banks like to do.
biggrin.gif


Code:

Code:
[left]monthCount = 0 amount = 10000 do { for (12 iterations) { if (amount < 500), break monthCount++ decrement amount by 500 amount = amount * (1 + (0.06/12) ) } } while(amount >= 500) return monthCount[/left]

[/QUOTE]

I think
confused.gif
- I havent programmed in a very long time
 
Sep 14, 2006 at 1:06 AM Post #9 of 13
Quote:

Originally Posted by seanohue
How do I write an algorithm in C++? My teacher assigned us HW tonight in which we need to write an algorithm for interest (10000 at 6% yearly interest, taking out 500 a month, how much long will it take to run out (I did it by hand and got 1 3/4 years, but he wants it in code)) and then one to compute pi/4 to 6 significant digits (it says like 1 - (1/3) + (1/5) - (1/7) + (1/9)....) and the fat screw didnt teach us how to do it and it is nowhere in this book. (Sorry for the anger, this is immensly frustrating).


An algorithim is a way to do something, which is put in a form which a computer can understand. Akwok already covered this, but I'll give another example. Say you have 10 numbers and you wish to determine which of them is the biggest. One algorithim is to start at the first number, record it. Then go to the next number and if it is bigger than the recorded number, scratch out the recorded number and put the current number in its spot. And then continue until you get to the 10th number. That is one algorithim. Another algorithim is to split it into 2 groups of numbers. First find the biggest number in the first group using the aforementioned method. Then find the biggest number in the second group using the aforementioned. Then compare the these two numbers and see which is bigger. That is another algorithims. There are many ways to go about this task of determining which is the biggest number, some of which are easier to write, some harder, some are better for when there aren't many numbers, some for when there are very many, some require the numbers to be sorted etc.

As for computing pi/4, if it's really as you said (1 - 1/3 + 1/5 - 1/7 + 1/9...), then this is how you do it. Read it and try to understand how I did it. Also note that I do not know how to make sure it is accurate to 6 decimal places. I assume the method is to take the current result, subtract pi/4 from it, then take the absolute value of that and see if it is < 0.00000001. But I would not bet anything on that. Now, the code (adapt it to whatever language):

double currentAnswer = 1
boolean subtract = true;
int divider = 3;
int const NUMBER_OF_TIMES = x; // where x is the number of times you want to go through this

for (int i = 0; i < NUMBER_OF_TIMES; i++)
{
if (subtract == true) {
currentAnswer = currentAnswer - (1 / divider);
subtract = false;
}
else {
currentAnswer = currentAnswer + (1 / divider);
subtract = true;
}

divider = divider + 2;
}

cout << currentAnswer << endl;

BTW, if you try to copy and paste that you will likely fail since like I said I don't actually know how to calculate pi/4, so this code only works as well as the algorithim you gave in your post
smily_headphones1.gif
 
Sep 14, 2006 at 1:08 AM Post #10 of 13
Quote:

Originally Posted by hciman77
That works perfectly as long as the interest is added at the start of the year - what the teacher might be after is adding interest monthly after each 500 is subtracted which is how the banks like to work it. Otherwise you are earning interest instantly again not something the banks like to do.
biggrin.gif


Code:

Code:
[left]monthCount = 0 amount = 10000 do { for (12 iterations) { if (amount < 500), break monthCount++ decrement amount by 500 amount = amount * (1 + (0.06/12) ) } } while(amount >= 500) return monthCount[/left]


I think
confused.gif
- I havent programmed in a very long time



I have no clue how all that interest rate stuff works, just got what I could grasp from the OP's explaination. o_O

A shorter variation of K2Grey's code to compute 1 - 1/3 + 1/5 - 1/7 .. up to 1/(2*NUMBER_OF_TIMES+1):
Code:

Code:
[left]double currentAnswer = 1; int const NUMBER_OF_TIMES = x; for (int i = 1; i < NUMBER_OF_TIMES; i++) { currentAnswer = currentAnswer + ((-1)^(i%2)) / (2*i+1); } cout << currentAnswer << std::endl;[/left]

 
Sep 14, 2006 at 2:21 AM Post #11 of 13
Oh wow, I just finished some of my numerical methods homework last week (graduate level) that required us to formulate expressions for pi numerically to 1e-6. Here's one I rather like based off a newton approximation, that has double factorials in the numerator and denominator (I wrote this - sorry if you can't understand it, but it's an idea; of course you'll probably want to use a simpler expansion that you understand):

For the expression:
pi = 3 + 6*sum_{n=0}^{inf} [ (2n-1)!!/((2n)!!(2n+1)*2^(2n+1)) ]

Code:

Code:
[left]double cpi = 0, sum = 0, pi = 3.14159265; double numer = 1.0, evenfac = 1.0, err6 = 0.000001/6.0, tpi = pi/6.0-0.5; int n = 2, pw = 8; // Starts at 2^3 while(tpi-cpi > err6) { numer *= (n-1); evenfac *= n; cpi += numer/((n+1) * evenfac * pw); pw <<= 2; n+=2; } cpi = (cpi+0.5)*6.0; printf("%d iterations required, pi is %.15g\n", n/2-1, cpi);[/left]

Essentially what you need to do is write your expression out, find the pattern and write in summation form, then write the code in a loop. Then in the loop, you compute pi with the summation you've found, and check that your pi is within the error specified.
 
Sep 14, 2006 at 3:57 AM Post #12 of 13
If all you need is "an algorithm" don't bother learning C++. This is a simple problem; you don't need anything speedy or powerful. Try Perl or Python, you can learn enough in a couple hours to start writing simple stuff on your own, without having to learn about compiling or classes, or copy from another user's pseudocode. Seriously, the class is there to learn, take it as an opportunity to do so and get feedback for your work.

Python has a nice online tutorial, is free and open source, and included on pretty much any Linux distrubution by default.

Link to tutorial written by the author himself (Guido van Rossum)
http://docs.python.org/tut/
 
Sep 27, 2006 at 2:29 AM Post #13 of 13
Not going into a formal definition of an algorithm one could say that:

An Algorithm is set of actions perfromed in certtain order to achieve a certain goal or solve a problem.

This by no means is a strict and formal definition.

Example:

This is you could say empty algorithm - nothing happens.
There's start and End and no body in between

Start
End

Let's take something nontrivial:

Start
Print AdamCalifornia
End

This algorithm just printis, for that matter my name.
Note that I am not using any parnethses, "" ..." etc because this is just
a Pseudo code which is understood to anyone who speaks basic English.
The action is here to print something.

See You on the Algorithmic
blink.gif
Side of the Moon

Adam
 

Users who are viewing this thread

Back
Top