Simple algorithm

Status
Not open for further replies.

Thunder

Wise, Aged Ars Veteran
108
In my intro to c++ class the professor gave us an assignment to write up an algorithm and she didnt tell us what an algorithm should look like. So I was wondering if someone could help me out, so here it goes. Mr thomas has negotiated a salary schedule for his new job. He will be paid one cent (.01) the first day with the daily rate doubling each day. What is his total earnings for 30 days?

Here is what I came up n=2(n-1) 1<=n=>30. Is there anything else I need?
 

scheherazade

Wise, Aged Ars Veteran
122
An algorithm is a written procedure for solving your problem. It should include the actions to be performed and the order in which these actions are executed. Consider it a recipe for your program - you need to list the ingredients (declare variables) then you need a detailed set of instructions for what to do with your ingredients (psuedocode).

The algortithm is usually in plain english, with some psuedocode where needed. For example, if I want a program to read in a student grade and print out "pass" or "fail", my algorithm might look something like:

Declare and Initialize Variables
read in student grade from keyboard

if student grade > 60
write "pass"
else
write "fail"

end.

Hope this helps :)
 

Midlands

Ars Praetorian
535
Subscriptor
Originally posted by Thunder:
Here is what I came up n=2(n-1) 1<=n=>30. Is there anything else I need?

It depends on how you look at it -- but I'd say you wrote a mathematical formula, not an algorithm. For example, there are well-known expansions for pi and e that are things like (1 + 1/2 - 1/3 + 1/4 - 1/5 + ...) -- but those are formulae, not algorithms. So an algorithm might look more like:

code:<HR>
Code:
INPUT: iterations-to-run<P>sum <- 0
denominator <- 1
is-addition? <- true<P>for (iterations-to-run)
  if (is-addition) then
     sum <- sum + (1 / denominator)
  else
     sum <- sum - (1 / denominator)<P>  denominator <- denominator * 2
  is-addition? <- (not is-addition?)<P>print sum

This clearly shows each step and what to set various variables to. It's not in a particular language, so you don't have to do things like set up an iteration counter on your loop or worry about syntax or variable types (for example, in C, I'd want "1.0 / denominator" instead), but it's easy to follow and very explicit.

Also, try picking up a book like Sedgewick's Algorithms (or just browse through it in the library). He has lots and lots and lots of algorithms in there.
 

petrov

Ars Tribunus Militum
2,714
Originally posted by Jackass JoeJoe:
I'm sorry but using a loop (O(n)) is a horible way to calculate something that only requires an O(c) algorithm.

mebbe. It all depends on the expected sizes of n, and coding complexity of each algorithm. Don't forget Rob Pike's comment that "Fancy algorithms are slow when n is small, and n is usually small". When faced with a coding problem, I nearly always code it in the fastest way for me to get the job done, and I worry about performance when I have a slow component, not during the initial coding phase. I'm interested in producing the most working code for my time, not necessarily the most elegant, or cleanest implementation. Sure, I won't deliberately choose a poor algorithm, but I don't usually worry too much about it, unless it's egregiously ugly.

--sam
 

petrov

Ars Tribunus Militum
2,714
is it cheaper in implementation time or execution time?

my point is just that implementation time dominates for most simple problems. Now that I'm not in school, I don't get extra points for correctness or elegance. I get extra points for producing lots of working code. (I like to call these points "dollars") :) I'm glad that I know how to produce elegant and fancy code when the situation calls for it, but my experience has led me to believe that the situation calls for it in a lot less cases than most of my peers believe.

this is getting pretty far off the original question, but I think that one has already been answered by others.

--sam
 

agilechen

Seniorius Lurkius
11
Originally posted by petrov:
is it cheaper in implementation time or execution time?

my point is just that implementation time dominates for most simple problems. Now that I'm not in school, I don't get extra points for correctness or elegance. I get extra points for producing lots of working code. (I like to call these points "dollars") :) I'm glad that I know how to produce elegant and fancy code when the situation calls for it, but my experience has led me to believe that the situation calls for it in a lot less cases than most of my peers believe.

this is getting pretty far off the original question, but I think that one has already been answered by others.

--sam

Given the crap I've seen under the hood of commercial software, it's directly indicative of the $$$ to correctness/elegance trade off. Elegance aside, I hope you at least get extra points for correctness.
 

bash666

Ars Legatus Legionis
24,417
Originally posted by petrov:


if you actually read what I wrote, I was referring to the choice between a O(n) algorithm vs. O(1).

To paraphrase your eloquence: "Learn to fucking read."

--sam

Huh? THat wasn't for you, it was for Thunder(the original poster)

Maybe if the question was a disguised hwo cna i aggange n unidentifiable balls in m unidentifiable containers(one of sterling's numbers IIRC) then it would be a appropriate to discuss an algorithm, but for something as trivial as this...
 

Prototyped

Ars Legatus Legionis
11,126
How much harder is implementing solution <- a x (r<sup>n</sup> - 1) / (r - 1) than
implementing a while loop?

I submit that the single computation is easier and faster to
implement, and only takes some education.

(Indeed, I suspect that the 'right' way to do things is often relatively
simple. There are some exceptions where more thought or knowledge is
required, e.g. implementing a decent pivot-based quicksort vs. a shell sort,
but the extra thought needed may not be much, and the implementation may not
be that much harder.)

[This message was edited by Prototyped on February 01, 2002 at 16:07.]
 
There's no algorithm here.

X = y*(b^(k+1)-1), where y=0.01, b=2, k=29, running in O(1) time.

I don't know man, once upon a time, programmers were associated with mathematical prowess. You wanted an algo for *that*?

I suggest a white loop. Im a C++ instructor in Cali

Haha. That's hilarious.... I don't know what's funnier: the fact that you can't spell such a simple word, the fact that you'd suggest a while loop, or your feeble attempt to provide support to a retarded suggestion.
 

2ooL

Ars Tribunus Angusticlavius
6,412
Im sure the exercise is just to come up with an algorithm to simulate the guy getting paid. Get the kids thinkin. sheesh.


In my intro to c++ class the professor gave us an assignment to write up an algorithm and she didnt tell us what an algorithm should look like.


Intro

[edit] Thunder youre question was fine. Forget the fact that most of the responses weren't.


Reading IS Fundamental.

[This message was edited by 2ooL on February 01, 2002 at 19:21.]
 

bash666

Ars Legatus Legionis
24,417
Originally posted by 2ooL:
Im sure the exercise is just to come up with an algorithm to simulate the guy getting paid. Get the kids thinkin. sheesh.
QUOTE]


Uhh no. The correct 'algorithm' is typically referred to as a formula, although O(1) is a class of algorithms. An algorithm should attempt to reach the lower bound (in O notation) with implementation detailes affecting the constant. I mean, I can invert a real, symmetric matrix in O(n^3) ops, but that doesn't mean I should.

-Kevin
 

lensam69

Ars Scholae Palatinae
746
You people go off-topic too easy.

Originally posted by Thunder:
In my intro to c++ class the professor gave us an assignment to write up an algorithm and she didnt tell us what an algorithm should look like. So I was wondering if someone could help me out, so here it goes. Mr thomas has negotiated a salary schedule for his new job. He will be paid one cent (.01) the first day with the daily rate doubling each day. What is his total earnings for 30 days?
Here is what I came up n=2(n-1) 1<=n=>30. Is there anything else I need?

You can present the algorithm in pseudo-code, This means that you basically say what you want to do, one instruccion at a time:

Beging Algorithm:
Total_money = 0
Current_pay_rate = 0.01

Repeat this 30 times:
Total_money = Total_money + Current_pay_rate
Current_pay_rate = Current_pay_rate * 2
Repeat

Show Total_money

End Algorithm

{Edit} : Sorry, the forum doesn't allow indentation.
 
Status
Not open for further replies.