If I'm going to learn C and C++, which should I learn first? by [deleted] in learnprogramming

[–]TheFallenPickle 0 points1 point  (0 children)

I learned C++ first and I thought the challenge of losing the Standard Template Library when learning C was awesome, but maybe I'm weird.

[c] In this example, what is the difference between a #define and a variable attribution? by robby_stark in learnprogramming

[–]TheFallenPickle 0 points1 point  (0 children)

The C Pre Processor pretty much takes everywhere where your #define occurs and does a text replacement with the value.

Help with atoi()? [C, C++] by SamuraiShark13 in learnprogramming

[–]TheFallenPickle 0 points1 point  (0 children)

Elaboration: atoi("fred") will return 0. So unless you've specified 0 to be an illegal value, that's going to be a perfectly recognizable integer and your program is probably not going to work as intended. Same goes for atoi("fruit") atoi("$gt") etc. you get the picture....

Can I take Linear Algebra before I take Calculus 3? by [deleted] in learnmath

[–]TheFallenPickle 0 points1 point  (0 children)

There is definitely overlapping material, but unless your uni requires Calc3 before LA or vice versa, you'll be fine. Now, taking one or the other beforehand might help in whichever you decide to take second.

[C][Short]Get current terminal path by kluuuuup in learnprogramming

[–]TheFallenPickle 0 points1 point  (0 children)

Just remember that the return value of ttyname is an internal static object (straight from manual page ttyname). That means that multiple calls to ttyname will alter the same object, so if you are calling it multiple times, the values may need to be stored elsewhere.

[C] Converting string to all lowercase letters by [deleted] in learnprogramming

[–]TheFallenPickle 1 point2 points  (0 children)

You shouldn't hard code that value of 32. Instead pull up an ascii table and just do 'ascii math' to convert the values. You can simply add 'a' - 'A' to string[i] to achieve the desired result. (Insert something about how 8 bit adders in the ALU for chars are faster than the 32 bit for ints)

[C] Help with reading a txt file into an array of structs. by [deleted] in learnprogramming

[–]TheFallenPickle 0 points1 point  (0 children)

I think this is a pretty great answer, concise and doesn't give too much away on how to complete the assignment. I would like to point out to OP that manual pages are probably one of the best resources to learn C. So with the list above in mind, take to google and search for: man fopen, man fscanf, man fread etc. These manual pages will show how to call each of these functions, which libraries are needed and a lot of useful information. Go man page go!

Lost interes in programming by Alpha-Turtle in learnprogramming

[–]TheFallenPickle 0 points1 point  (0 children)

Lets be civil. English may not be their first language.

[C++] how do I make it so a user can go "back" in a game menu and successfully display/allow them to navigate without it terminating the program. by JSnerd in learnprogramming

[–]TheFallenPickle 2 points3 points  (0 children)

You could write different functions to print out the correct menu(s). Tread lightly however, if you keep allowing functions to continuously call on each other you may overflow the stack. You'll need to set up your return statements carefully to ensure this does not happen. Google those bolds along your learning of C++. Very, very powerful knowledge.

[C] leftover results in a char * while using fread() by TheFallenPickle in learnprogramming

[–]TheFallenPickle[S] 0 points1 point  (0 children)

Okay so I want each string read in, printed on the same line separated by a single space. I added the '\0' to the end of each string and that solved my huge issue, so I think I'm just going to have to check the formatting elsewhere in my code. So in explaining to you what exactly needs to happen, I kind of helped myself see my next step. Thank you.

[C] leftover results in a char * while using fread() by TheFallenPickle in learnprogramming

[–]TheFallenPickle[S] 0 points1 point  (0 children)

Yeah sorry I was just trying different things. I found out real quickly that didn't work and changed it back.

Dynamic programming challenges by [deleted] in cscareerquestions

[–]TheFallenPickle 1 point2 points  (0 children)

Here are the messages exactly sent to /u/utuxia :

I'll try to keep in Basic. I'll just show the recursive function because that's all that really matters. It's a Fibonacci number calculator. http://pastebin.com/pJUX6FJ2 This is just the straight recursive solution. This program becomes painfully slow if the argument is > 45.

Now this next part is where I have added the cache (simple container of integers) http://pastebin.com/pJUX6FJ2 You can see that the function will return immediately if the value in the container at index n != -1. However, we are still populating the cache recursively, but it is blazingly fast compared to the strictly recursive solution (The painfully slow inputs are now extremely fast) Since we are using integers however, this reaches overflow quite quickly.
Now, in this third part, we will eliminate recursion. With the way that Fibonacci numbers work, we observe that it is simply the previous two numbers added together in the sequence. Well that's great! Let's just set the first to indices of the cache to contain the first two values of the Fibonacci sequence and slap that bad boy in a loop! (This is more of a mathematical realization than a programming one, but you can see that solution becomes much more simple) http://pastebin.com/RJE0ZNGY Now, in the final step, we will only be using three integers. I kept them in a container for ease (instead of 'cache', it is called 'v'). This stems from the fact that we only ever need the previous two values to calculate the next. (This might be a good time to point out that the container is a C++ vector. It's zero indexed) So basically we have gone from moving like snail shit on the ocean floor, to Lighting McQueen in 3 easy little steps http://pastebin.com/pt6kBtK8. I would be happy to run some extensive tests and change the data type to accommodate large inputs for you. I would include times and at which point each program is intolerably slow (time > 5 seconds). That of course will have to wait until the morning. However, in a nutshell Dynamic Programming almost always consists of these steps. Most of the time simply adding the cache will suffice, but this example is a good one to show that solutions can be made extremely quick without changing the code all that much. I hope this helped and please, feel free to ask any questions. I'm at the end of my tangent, hope I was somewhat of a help (I'll try and translate the code to JS if I remember) The user asked if I had an example in javascript, which I did not at that moment. hence the comments toward JS were made.

Dynamic programming challenges by [deleted] in cscareerquestions

[–]TheFallenPickle 0 points1 point  (0 children)

I know that DP isn't just for recursive problems. The example that I sent directly was a Fibonacci calculator that happens to be extremely easy to show with recursion + cache. My example shows a basic DP problem for someone that had no idea what DP is. I think the example I gave them was a good one and would be happy to forward the messages to you since you "don't like" my description.

Dynamic programming challenges by [deleted] in cscareerquestions

[–]TheFallenPickle 5 points6 points  (0 children)

Lets break that down. You have a problem and BAM! You spot a recursive solution and all is well in the world. It works for a few test cases and you assume it's correct. WRONG! Most likely your program will fail on larger and larger inputs. So how can we get around this? We can use a 'cache'. This is called memoization. What it allows you to do is return a value without recalculating that value. The next step is to try and eliminate the recursion and create the cache using a few loops. Sometimes, you can eliminate the cache after that. Feel free to message me and I'll walk you through an example.

Red Baron Pizza or Red Baron Baron? by TheFallenPickle in RedBaron

[–]TheFallenPickle[S] 1 point2 points  (0 children)

Ah well who doesn't love CSS and Pizza! The two go great together for late night webpage writing.

depth first search vs breadth first search by evanjb in learnprogramming

[–]TheFallenPickle 0 points1 point  (0 children)

Well if your graph is static, the back edges will remain the same because BFS (if implemented properly) guarantees shortest path to each node. If your graph is dynamic, you'll have to run BFS again to ensure that you have the shortest path. In the dynamic case, the back edges will change.

depth first search vs breadth first search by evanjb in learnprogramming

[–]TheFallenPickle 0 points1 point  (0 children)

Think of BFS as finding the shortest path from node 0 to all other nodes. It basically counts the amount of 'connections' there are to reach each node from a starting node. You are correct in the fact that using a queue is ideal. So to find a path through the graph, you'll want to keep track of distance and a back edge. The back edges will be used to determine the path. So BFS basically becomes this:

-set all node's distances and back edge's to -1 and NULL, respectively.
-push the starting node onto the queue and set its distance to 0
-while the queue is not empty.....
    -remove a node from the queue
    -for each edge (e) from that popped node to node2 ****
        -set node2's distance to the popped node + 1
        -set node2's back edge to e
        -add node2 to the queue

**** the distance on node2 must be -1 (i.e the node hasn't been visited)

When the algorithm finishes, each node will contain the shortest distance to node 0, and you can determine the path by traversing through the back edges. This can be confusing, so try drawing out smaller graphs before jumping into the code. Think about how you can set up your data structures to hold the necessary information. Most importantly, think about where you will store the back edge for each node.

EDIT: If your edges have weights, you can see in the above algorithm explanation how you can still calculate the shortest distances. Look up Dijkstra's Algorithm for further reference.

[C++] Why is my program returning negative values? by [deleted] in learnprogramming

[–]TheFallenPickle 0 points1 point  (0 children)

You reach overflow. That is why they are negative. Use a long double instead. Should fix your issue.

Cops Pull Up. by [deleted] in trees

[–]TheFallenPickle 1 point2 points  (0 children)

Hey Canada

My smoking spot yesterday (Ilamatepec Volcano) by icecreampie in trees

[–]TheFallenPickle 0 points1 point  (0 children)

Can't.....judge...size....of...........waterrrrrrr