The Big Programming Thread - Page 180
Forum Index > General Forum |
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. | ||
![]()
tofucake
Hyrule19031 Posts
| ||
Thorakh
Netherlands1788 Posts
+ Show Spoiler +
and + Show Spoiler +
Because Java 1.6 can't switch on a string value I have to convert my code to something else and my solution isn't working. I'm probably missing the obvious here but it has me stumped. There probably is a better way to convert A to 0, B to 1, C to 2, etc. than a switch statement as well. The indentation is a bit fucked apparently, don't mind it :p edit: oh for fuck's sake, you can't use == for String comparison. I should've used if (stringVariable.equals("A")) etc. The solution is so obvious, just as I thought! edit2: and found a neater solution as well + Show Spoiler + String[] letters = {"A","B","C","D","E","F","G","H","I","J","K","L","M"}; | ||
Deleted User 101379
4849 Posts
On October 20 2012 02:00 Thorakh wrote: Quick question. What on earth is different between + Show Spoiler +
and + Show Spoiler +
Because Java 1.6 can't switch on a string value I have to convert my code to something else and my solution isn't working. I'm probably missing the obvious here but it has me stumped. There probably is a better way to convert A to 0, B to 1, C to 2, etc. than a switch statement as well. The indentation is a bit fucked apparently, don't mind it :p The main difference is that i would kill those that use the second solution where the first would work. Also, try this (not sure about exact syntax since i don't use java often):
| ||
Thorakh
Netherlands1788 Posts
The main difference is that i would kill those that use the second solution where the first would work. Yeah but as I already said, you can't switch on String values in Java 1.6, which is what version my program needs to run on :p | ||
Nitro68
France470 Posts
| ||
Jonrock
Germany80 Posts
Ideally for C++ / C / Assembler Maybe something more recent touching subjects like coding for multi-processor systems and single-operation-multiple-input I am mostly interested in optimizing numerical calculations not so much into hardware specific stuff Thanks in advance | ||
![]()
white_horse
1019 Posts
+ Show Spoiler + 1. Waldo starts his journey at location (x, y) = (0, 0), the front door of the Frat house, the center of the universe that Waldo is in. Waldo’s universe is a 2-dimensional plane that measures 20 × 20, that is, −10 <= x <= 10 and −10 <= y <= 10. Waldo’s home is a 2 × 2 square in the upper right hand corner, 8 <= x <= 10 and 8 <= y <= 10. 2. Declare a variable called atHome and give it an initial value of 0 (false) which means that Waldo has not yet found his way home. 3. Declare another variable called stepsToTry and give it an initial fixed value of 5000. Waldo hopes to get home before 5000 steps. If he doesn’t, he gives up and sleeps on the nearest park bench. PSEUDOCODE: FOR-LOOP until 5000 steps are taken and while Waldo has not yet reached home. DO (Loop for choosing the direction of the step) Take a proposed step of unit length in a random direction. (See note on the next page.) WHILE (endpoint of the step is outside of Waldo’s universe) Take the step. If Waldo’s new location is within the square defined as his home, set atHome to true. END FOR-LOOP IF (atHome), print out the total number of steps that Waldo has taken and stop the program. ELSE, print out a message that indicates that Waldo is lost and will not make it home that night. End the program. + Show Spoiler +
| ||
uSiN
United States208 Posts
On October 20 2012 02:09 Thorakh wrote: Yeah but as I already said, you can't switch on String values in Java 1.6, which is what version my program needs to run on :p Morfildur showed you an easier way to do it. Instead of switching, treat each character in the string as an integer and subtract 'A' or 65 (ASCII value of 'A'). If your still confused put the following few lines of code inside a main and run it. + Show Spoiler + char myA = 'A'; | ||
uSiN
United States208 Posts
On October 20 2012 07:39 white_horse wrote: Can you guys help me figure out whats wrong with my code? I don't understand whats supposed to go in the while condition in the do while loop. Our professor gave us a pseudocode to help us. + Show Spoiler + 1. Waldo starts his journey at location (x, y) = (0, 0), the front door of the Frat house, the center of the universe that Waldo is in. Waldo’s universe is a 2-dimensional plane that measures 20 × 20, that is, −10 <= x <= 10 and −10 <= y <= 10. Waldo’s home is a 2 × 2 square in the upper right hand corner, 8 <= x <= 10 and 8 <= y <= 10. 2. Declare a variable called atHome and give it an initial value of 0 (false) which means that Waldo has not yet found his way home. 3. Declare another variable called stepsToTry and give it an initial fixed value of 5000. Waldo hopes to get home before 5000 steps. If he doesn’t, he gives up and sleeps on the nearest park bench. PSEUDOCODE: FOR-LOOP until 5000 steps are taken and while Waldo has not yet reached home. DO (Loop for choosing the direction of the step) Take a proposed step of unit length in a random direction. (See note on the next page.) WHILE (endpoint of the step is outside of Waldo’s universe) Take the step. If Waldo’s new location is within the square defined as his home, set atHome to true. END FOR-LOOP IF (atHome), print out the total number of steps that Waldo has taken and stop the program. ELSE, print out a message that indicates that Waldo is lost and will not make it home that night. End the program. + Show Spoiler +
With the do while loop in the for loop what are you trying to prevent? The do in the do while is generating a random direction to step into. What if Waldo is at (10,5) and you generate randomly to step into square (11, 5)? How big is Waldo's universe? | ||
JeanLuc
Canada377 Posts
On October 20 2012 07:39 white_horse wrote: Can you guys help me figure out whats wrong with my code? I don't understand whats supposed to go in the while condition in the do while loop. Our professor gave us a pseudocode to help us. + Show Spoiler + 1. Waldo starts his journey at location (x, y) = (0, 0), the front door of the Frat house, the center of the universe that Waldo is in. Waldo’s universe is a 2-dimensional plane that measures 20 × 20, that is, −10 <= x <= 10 and −10 <= y <= 10. Waldo’s home is a 2 × 2 square in the upper right hand corner, 8 <= x <= 10 and 8 <= y <= 10. 2. Declare a variable called atHome and give it an initial value of 0 (false) which means that Waldo has not yet found his way home. 3. Declare another variable called stepsToTry and give it an initial fixed value of 5000. Waldo hopes to get home before 5000 steps. If he doesn’t, he gives up and sleeps on the nearest park bench. PSEUDOCODE: FOR-LOOP until 5000 steps are taken and while Waldo has not yet reached home. DO (Loop for choosing the direction of the step) Take a proposed step of unit length in a random direction. (See note on the next page.) WHILE (endpoint of the step is outside of Waldo’s universe) Take the step. If Waldo’s new location is within the square defined as his home, set atHome to true. END FOR-LOOP IF (atHome), print out the total number of steps that Waldo has taken and stop the program. ELSE, print out a message that indicates that Waldo is lost and will not make it home that night. End the program. + Show Spoiler +
if I'm not mistaken your do/while loop is gonna cycle endlessly if x and y are within the specified range. the exit condition of your do loop is based on where x and y is, and x and y are not being altered within the body of the loop. you should be changing x and y within the loop. and if x and y after random step is outside the range, you need to rescind the change you made to x and y and only then loop again. Edit: actually instead of making the step and taking it back, in the exit condition test whether x+dx and y + dy falls OUTSIDE the world. right now you are checking if x and y fall INSIDE the world. | ||
Thorakh
Netherlands1788 Posts
On October 20 2012 09:11 uSiN wrote: Ah yes, of course. So smart ^^Morfildur showed you an easier way to do it. Instead of switching, treat each character in the string as an integer and subtract 'A' or 65 (ASCII value of 'A'). If your still confused put the following few lines of code inside a main and run it. + Show Spoiler + char myA = 'A'; | ||
Fyodor
Canada971 Posts
On October 20 2012 07:12 Jonrock wrote: Can anybody recommend a good book on Code Performance Optimization? Ideally for C++ / C / Assembler Maybe something more recent touching subjects like coding for multi-processor systems and single-operation-multiple-input I am mostly interested in optimizing numerical calculations not so much into hardware specific stuff Thanks in advance http://www.amazon.ca/C-Concurrency-Action-Practical-Multithreading/dp/1933988770/ref=sr_1_1?ie=UTF8&qid=1350752722&sr=8-1 There's also a course on Coursera about heterogeneous computing if you're interested in that. Most optimization happens at the algorithm level anyways so you might want an advanced book on that too? | ||
LukeNukeEm
31 Posts
but my mind draws a blank when it comes to the intersection of the box and the transformed object | ||
heishe
Germany2284 Posts
On October 20 2012 07:12 Jonrock wrote: Can anybody recommend a good book on Code Performance Optimization? Ideally for C++ / C / Assembler Maybe something more recent touching subjects like coding for multi-processor systems and single-operation-multiple-input I am mostly interested in optimizing numerical calculations not so much into hardware specific stuff Thanks in advance There's really not that much to know. The big field is parallelism, cause that's where you will always be able to get the most performance. Just google "stackoverflow books parallelism". As for SIMD stuff, there's not much to learn other than how to actually use it. From then on it's just you identifying situations where you can apply it (every time where the same operation is executed lots of times on different elements which are close together). As for micro-optimization stuff: The three big things you need to know are keeping your stuff close together in cache, avoid branches (ifs and switches) like the plague, and write custom allocators if you notice slowdowns due to allocation patterns. That stuff should get you going with googling, but I don't know of any one book that covers them all. | ||
CecilSunkure
United States2829 Posts
On October 21 2012 04:34 LukeNukeEm wrote: Can anybody point me towards the right direction regarding an intersection-test between a transformed object and an axis-aligned bounding box? I have the ray - transformed object intersection working, i have the axis-aligned bounding box transformation/redefinition working, i have the non-transformed-object - axis aligned bounding box intersection working. but my mind draws a blank when it comes to the intersection of the box and the transformed object Intersection of box to transformed object? What is the transformed object? A circle, a square, a rect, a rhombus? What? | ||
mmp
United States2130 Posts
It works, but is terrible to read. Can someone help simplify this? | ||
teamamerica
United States958 Posts
On October 19 2012 18:20 billy5000 wrote: I actually have it set up on my eclipse, but the emulator runs unbearably slow on my computer. In addition, I have absolutely no handheld device experience, so there's not a clear distinction of what the consumer wants and does not want. Anyway, I was mainly curious because I want to finally do something fun and practical after 1 year at my university. It just hit me that I haven't thought about the web. Android/ios apps have that cutesy characteristic, which is probably the reason why I was interested. Make sure you turn on GPU emulation for the emulator - for some reason it's not on by default (@ adt tools v.20). Having a device is nicer then an emulator but if you have to use emulator, follow the instructions at: http://codebutler.com/2012/10/10/configuring-a-usable-android-emualtor/ And I had an emulator that was decent. I had some problem with partition size but I think I changed one of the emulator run flags. Stackoverflow should solve any problems you have setting up. I'd also recommend against buying a device right off the bat - friends stuff or emulator - but if you buy the galaxy player XDA forums can guide you into upgrading into 4.0 or installing a custom ROM. Smart people there ^^. Make an app that you want to use so at least if no one else in the world uses it, you'll have 1 user. | ||
LukeNukeEm
31 Posts
On October 22 2012 06:46 CecilSunkure wrote: That's the problem, it's unknown - all I know is that it has implemented the methods getAABB, intersectRay and intersectAABB. I think what I want to do is not possible. Right now I calculate the transformed AABB of the object and test for intersection against that. This approach returns false positives, which is bad, but it still works.Intersection of box to transformed object? What is the transformed object? A circle, a square, a rect, a rhombus? What? On to the next question! I have implemented an octree, with nodes like this: class Node However, I only need data on my leafnodes, so I have a bunch of nullpointers in my Octree. How would I change this Implementation so that it works something like this: class NonLeafNode | ||
CecilSunkure
United States2829 Posts
On October 22 2012 20:20 mmp wrote: The algorithm converts a string sentence "a b {c d} e" and converts it to a list of words and nested sentences, ["a", "b", "{c d}", "e"].
It works, but is terrible to read. Can someone help simplify this? I don't get how the nested sentences are supposed to work. How are multiple nested sentences supposed to work? | ||
Ian Ian Ian
915 Posts
+ Show Spoiler + n = 1 pmf = binomial(1,n,0.5) while pmf > 0 : n += 1 pmf = binomial(1,n,0.5) if pmf == 0: print n else: I'm supposed to find what value n needs to be to have pmf = 0. It is n = 1075. I don't want to see python print n every single time the loop is ran so I threw in an if statement. But it keeps giving me an error because the else statement has nothing there, but I don't want it to do anything either! Also, how are people writing their code in those white boxes? :S | ||
| ||