Latest Article

Contrast training is a unique way to optimize results. Read this article by Lee Boyce about how to incorporate it into your training to pack on lean muscle mass.

 By: Lee Boyce Added: March 25th, 2013
More Recent Articles
 An Interview with Marianne Kane of Girls Gone Strong By: Jordan Syatt
 What Supplements Should I be Taking? By: Jay Wainwright
 Bench Like a Girl By: Julia Ladewski
 Some Thoughts on Building a Big Pull By: Christopher Mason
 Shoulders Like Boulders By: Jay Wainwright

# Thread: For all you programmers/problem solvers...

1. ## For all you programmers/problem solvers...

I'm taking my first programming course and I can already tell it isnt my thing. I dont think the way I need to, and I suck at this type of stuff. For some of you I'm sure this comes natural and is very easy, so if you wanna help a brotha out it'd be much appreciated!

Heres a few problems to solve for anyone of you who enjoy this stuff or just bored at work. Assume you are giving instructions to a small child and that they basically will do only what you tell them to so the directions have to be precise, yet simple.

Exercise 2.5. Write down instructions for finding the median number in a pile of papers. You may assume that you can compare numbers such as which is larger or smaller. Recall that the median of a set of numbers is the number in the middle: half of the numbers are bigger and half are smaller. If there are an even number of numbers, then the median is the average of the middle two values. For example, the median of (32; 4; 7; 65;8 ; 56; 1; 0;-9, 78, -15, 55) is the average of 7 and 8, which is 7.5.

Exercise 2.7. Assuming that the child cannot even multiply single digit integers, write down instructions for multiplying two single-digit integers using no arithmetic operations except for addition and comparison
of integers.

Exercise 2.11. Given a rough draft of an article and a dictionary, write down instructions for marking each
misspelled word in the article.

Exercise 2.13. Given a list of recipes that must be prepared, write down instructions for creating a shopping list. Assume for simplicity that all ingredient amounts are given in cups. For instance, if there are two recipes:
Fried cheese sticks
Ingredients:
0.5 cups flour
1 cup mozzarella cheese
1 cup canola oil
Directions: Heat the oil to medium heat. Coat the cheese sticks in oil,
flour, and breading. Fry the cheese sticks in the hot oil until brown.
Pizza Soup
Ingredients:
1.5 cups flour
0.75 cups water
0.01 cups salt
0.01 cups yeast
0.25 cups mozzarella cheese
0.2 cups tomato sauce
Directions: Mix all ingredients together in bowl and place in microwave.
Then the shopping list should be
Shopping List
2 cups flour
1.25 cups mozzarella cheese
1 cup canola oil
0.75 cups water
0.01 cups salt
0.01 cups yeast
0.2 cups tomato sauce

Haha this seems like a lot, but I'm having such a hard time even though this stuff really isnt hard. I'm desperate.

2. Are you using any particular language, or are you supposed to write out pseudo code?

I'll give you some hints:

2.5: If they were sorted in order it would be much easier.

2.7: Forget the computers for a sec, this one is a math problem. Work out some simple examples on paper: for example 8x6 = 8+8+8+8+8+8

2.11 & 2.13: Think of what you would do if you did this yourself.

3. Please define "instructions." Is it, as mentioned above, pseudo code? Do you have an example of another problem that you solved?

4. Originally Posted by RedSpikeyThing
Are you using any particular language, or are you supposed to write out pseudo code?

I'll give you some hints:

2.5: If they were sorted in order it would be much easier.

2.7: Forget the computers for a sec, this one is a math problem. Work out some simple examples on paper: for example 8x6 = 8+8+8+8+8+8

2.11 & 2.13: Think of what you would do if you did this yourself.
We're using pseudocode, with basically no limitations. We dont have to include loops, variables or anything. just some type of explanation, which he always uses the example of explaining this to a small child.

I figured out 2.11. With your hints I'm understanding 2.7 and 2.13. Only one I'm still really lost on is 2.5. Assuming I write directions on how to get them in order, where do I go from there? I dont know how to indicate how many papers to count to find the median (depending apon if its an odd or even amount of papers).

Just these simple hints are helping alot. I dont know why I cant think of these things myself lol.

5. Heres an example we did in class.

Steps for finding which number is bigger

1. Count digits, if there are more digits, number is greater
2. If digits are the same, look at the first digit in the number
3. If previous digits are equal, go to next digit
4. Repeat step 3 for all digits
5. If all digits equal, numbers are equal

Very basic stuff here.

So hopefully something like this works for 2.13..
2. Compare ingredient to other ingreidents in other recipes, if match is found add together amounts
3. MOve to next ingredient
4. When no more matches are found, write down amount and ingredient onto shopping list.
5. Repeat steps 2-4 until no ingredients remain.

And like he told us in class, if we define something in a previous exercise (such as comparing words) we dont have to explain how to do it again. We can simply put "compare".

6. For 2.5, once you get the papers sorted, use the following:
If an even number of papers:
Take the number of papers, and divide in half. The number you get is the first paper in the series that you want to take a value from. Add one to the series number to get the second paper that you want to take a value from. Then add the values of the two papers, and divide in half to get the median.
If an odd number of papers:
Take the number of papers, and divide in half. Select that paper. It's value is the median.

Using your earlier example, we have 12 papers. Sorted in order: (-15, -9, 0,1,4,7,8,32,55,56,65,78). 12/2=6. 6+1=7. So we select the 6th and 7th papers (values of 7 and 8). 7+8=15. 15/2=7.5=median. Hopefully that helps.

7. Originally Posted by AKMass
For 2.5, once you get the papers sorted, use the following:
If an even number of papers:
Take the number of papers, and divide in half. The number you get is the first paper in the series that you want to take a value from. Add one to the series number to get the second paper that you want to take a value from. Then add the values of the two papers, and divide in half to get the median.
If an odd number of papers:
Take the number of papers, and divide in half. Select that paper. It's value is the median.

Using your earlier example, we have 12 papers. Sorted in order: (-15, -9, 0,1,4,7,8,32,55,56,65,78). 12/2=6. 6+1=7. So we select the 6th and 7th papers (values of 7 and 8). 7+8=15. 15/2=7.5=median. Hopefully that helps.

Thanks alot guys this helps alot.

Heres another one that came up that is alittle more challenging.

Exercise 2.9. Assume that you have already written down instructions for doing the phone book exercise in Section 2.3. Now, the child can be given a stack of phone books. He should write down the list of phone numbers for each town represented by each phone book, along with the name of the town first (the name of the town is written on the front of each phone book). For example, the child may write down something that starts like
Ames
----
(515) 555-1234
(515) 555-4567
(515) 555-9854
Boone
-----
(515) 555-8462
(515) 555-8913
Des Moines
----------
(515) 555-6854
...

Here is the 2.3 exercise in which you're suppose to find all the McDonalds in the phone book.
1 A phone book entry is a listing of information about a business in
the phone book, including at least
2 - the name,
3 - the phone number, and
5 of the business, with possible some extra information like the hours
it is open, although the extra information may be missing.
6 To find the phone number of the first McDonalds in the phone book,
starting at a given entry, do the following:
7 Let the ``current entry'' be the given starting entry.
8 While the name of the current entry is not ``McDonalds'',
9 let the ``current entry'' be the entry after the current entry.
10 (At this point we have just found an entry with the name
``McDonalds''.)
11 Write down the phone number of the current entry.
12 To find the phone numbers of all the McDonalds in town, do the following:
13 Let the ``starting entry'' be the first entry in the phone book
14 While you have not read off the end of the phone book:
15 write down the phone number of the first McDonalds in the
phone book, starting at the starting entry, and then
16 let the ``starting entry'' be the entry immediately after
the entry that we just wrote down.

I dont quite get it?

8. For 2.9 is he supposed to write down all the McDonalds numbers, or all numbers period?

9. Writing algorithms, basically an instruction set to be able to be translated into any other code.

I'll give you another good hint before you go and start writing programs. It's a very logical style of thinking... if you go and start writing without making an algorithm to fall back on your going to make tons of logic errors and possibly even a few compile/runtime errors.

Sit and think about what your doing and what you need to do before you start to write the code.

The idea of using a little kid is a good one. Things can't be assumed in programming, you can just assume the machine or even the person that may end up working with your algorithm can just assume correctly what it is your doing in your work.

10. This thread makes my stomach churn, thinking back to my college programming classes. Good luck, man.

11. i didnt realize we had so many programmers on board here?? kinda cool i guess, i have a VB class and a java class i'm takin this semester. after last semesters programming class, the biggest thing i learned was to write everything out first in plain english, as simple as possible. Break everything down into as many simple steps as possible, and then start.

but i definately know where i can come now for help

12. I'm a hybrid nerd jock. I can reinstall your OS and box squat 500+ pounds in the same evening.

But yea a lot of people on this forum have quite the savvy about computers. I'm going to be a software engineer myself, unless I find another much more worthwhile venture in the computer field before I graduate.

13. Originally Posted by WBBIRL
I'm a hybrid nerd jock. I can reinstall your OS and box squat 500+ pounds in the same evening.
Same here, minus the 500 pound squat haha
I'm planning on starting my master's in parallel computing in the fall

14. Originally Posted by jAy_Dub
Thanks alot guys this helps alot.
Glad you're making progress Some of these problems can be very frustrating.
I dont quite get it?
Me neither what the hell is the question asking?

15. To me it sounds like it's asking for the kid to copy the phone book numbers. But I think it's actually asking for the phone numbers of all the McDonald's in each town. Either way it seems too simple. The only difference is, in the McDonald's version, you'd say "Refer to instructions from 2.3" near the end of the algorithm.

16. I'm not sure what i want to go into with my Computer Science degree... but i am leaning towards networks/servers/software programming.

oh to the OP: give it some time you'll get the hang of it. I took a logic/philosophy class that like tapped into another realm of my brain i had. It really helped me with programming.

17. Yeah I'd actually recommend a logic class for all people, let alone programmers.

18. Originally Posted by AKMass
Yeah I'd actually recommend a logic class for all people, let alone programmers.
I really do think that logic is a trait you either have or don't. Everyone has some level of logical reasoning, but I don't think you can improve what you have very much.

Theres a difference between logic and knowledge, logic is connecting that knowledge together to provide another answer.

But yea, a lot of people in my comp 1 class had a lot of trouble with the logical thinking in their code. They were doing calculations in their code before they had even gotten values yet.

19. I was taking a visual basic class, and halfway through the semester this girl exclaims: "Oh, so you tell the computer what to do, and it does it!"

20. Since you guys made sense of all that jazz before, maybe you can help me out with this but this is actually writing out some code in java. I never imagined programming took so damn long. I've literally been working on my homework for 4 hours and thankfully this is all I have left.

Exercise 4.16. Convert the follow mathematical expressions into Java expressions (using the same names for Java variables to represent the mathematical variables, and assuming that they are double variables)

1. cube root of (x*y) * square root of (y + (2 cubed))
2. x raised to y raised to z
3. absolute value of (x squared) * absolute value of (y * 2)

Now for the absolute values i can use math.abs() but I dont know which method to use for cube roots, square roots, numbers/letters raised to a power and so on.

21. Originally Posted by jAy_Dub
Now for the absolute values i can use math.abs() but I dont know which method to use for cube roots, square roots, numbers/letters raised to a power and so on.
double Math.pow(double x, double y) - raises x to the power of y. If you remember your math identities, roots shouldn't be a problem

Here's the API for the Math class: http://java.sun.com/j2se/1.5.0/docs/...lang/Math.html

And the complete java API is:http://java.sun.com/j2se/1.5.0/docs/api/

They are very hard to read at first but it is worth trying to figure out. Check out the Arrays and Collections classes when you start working with them.

EDIT: do NOT use the ^ symbol for powers....it doesn't do what you think it would!

22. Thanks again RST, I'll definitely use this table from now on. This stuff is tough, and its hard to get a grasp on but when it all clicks, it makes it fun and worth it. I'm actually enjoying some of it surprisingly.

23. Originally Posted by jAy_Dub
Thanks again RST, I'll definitely use this table from now on. This stuff is tough, and its hard to get a grasp on but when it all clicks, it makes it fun and worth it. I'm actually enjoying some of it surprisingly.
feel free to PM me if you want

24. Originally Posted by AKMass
Yeah I'd actually recommend a logic class for all people, let alone programmers.
You should specify. Logic is a philosophcal discipline as well as a scientific/mathematical one, and they do not at all overlap.

I ****ing hated programming. I'm a hardware and networking geek.