Ancient Dragon is correct, NULL is not the same as a null terminator.

I think you're right. You'd want to compare a char with a char. Now I'm thinking 0 is an int by default so you'd need a cast. Example, in:
[CODE]'\0' == 0[/CODE]
is there an implicit cast? Probably.
For the original poster, you're reading a two dimensional array as one and fetching uninitialized data, start by removing the {} around your string (see at your k counter).

jonsca, in C++ you don't need the implicit character conversion involved with [B]'\0'[/B], you just need [B]0[/B].

Is the body of the function missing?

try [CODE] 1.
for(j=0; line[k]!=' '; k++)[/CODE]
just a guess

What about Python inspired iteration?
[CODE]byte range[] = new byte[5];
for (byte nul : range) {
System.out.println("do stuff");
}[/CODE]

[QUOTE=llianne;1435492]how does the try catch and finally work in a run time program???[/QUOTE]
A try/catch/finally block resembles a if/if else/else block in structure, however in functionality it it quite different. The code scoped within the try block attempts to execute, and if an error (exception) is thrown, the stack will begin to unwind, and will stop unwinding if the catch parameter names the thrown error. Exception is the superclass to all exceptions and catches everything. The catch block takes over then and only then. The finally block always executes, it is good for closing files and ports, for example. If there is no matching exception the application will quit, leaving your system in a potentially arbitrary state. The catch block is a good place for stack trace printouts and otherwise error reporting.

PS: Relying on exceptions is poor programming style. Never pass an exception silently.

Here is the try/catch/finally format:

[CODE]try {
// ...
} catch (Exception e) {
// ...
} finally {
// ...
}[/CODE]

I've read several C++ books in my life, and "C++ in a Nutshell", written by Ray Lischner, is in my opinion by far the most coherent and complete C++ book I've ever read or browsed. On the other hand I've read a lot of good things about Accelerated C++, and it's named twice here so far.

[QUOTE=griswolf;1428950]If you are new to Python threads, be aware that they are often not very efficient (better conceptually, maybe, but probably not very fast). See, for instance [url]http://www.grouplens.org/node/244[/url][/QUOTE]
It would seem this is better:
[URL="http://docs.python.org/library/multiprocessing.html"]"The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads."[/URL]

[QUOTE=griswolf;1428496]Why do you need locking?[/QUOTE]
I don't, particularly. Rather I wanted to know if there's an automatic form of locking. I've discovered only single-step operations don't require a lock.
[ICODE]lock = Lock()
lock.acquire()

...

lock.release()[/ICODE]
Of extending Thread or passing Thread a callable, what is generally better?

Are there any steps I can take while coding any function that will make it better? Is functional programming better? Or is it just safer in some environments. Is a recursive function faster than a few loops? Should I use new variables when passing values around or should I stick with pointers. There's probably no easy answer to these questions, but I'm hoping I'm wrong about that.

[QUOTE=Jelte12345;1427454]has to write and erase a couple of gigs to the memory every time the program runs[/QUOTE]

My solution:
Read the file one burst at a time, like 1024 characters. Add 1 to a counter each time you reach a newline character, and also keep track of your position in the file (e.g. each file read [B]pos += 1024[/B]. When your newline counter reaches your desired line, your position in the file will be your [B]pos[/B] - offset from the end of your 1024 file buffer, where you read the proper newline character. You can make this code slightly shorter by reading one character at a time, but that is not recommended unless you don't care about speed. And speaking about speed, declare the buffer and the two counters before your read loop.

Currently the information is stored directly without the [use] of objects.
ArrayList myArr = new ArrayList();
Not true. String is a class.

I also want to know the special use of this ArrayList, in this particular scenario.
Integer vacationIndex = nameLength % myArr.size();
ArrayList is a generic typed list, declared with a class name as its type and containing standard methods like get_at, remove_at, and insert. If you want [I]anything[/I] specialized you'll probably end up making your own list class.

Some thoughts:

What are you accomplishing with:
[CODE]for i in range(n):
open('wpdocs\file' + str(i) + '.wp', 'wb').close()[/CODE]
"f" is your bitstream.
This reads up to the end of the stream:
[CODE]out_block = f.read(int(carved))[/CODE]
Is "path" supposed to be quotated?
[CODE] doc_wp = open('path', 'wb')[/CODE]
[B]doc_wp[/B] writes to itelf.

[QUOTE][CODE]startTime = time.time() ## I'm not sure how this works, just learned the trick.[/CODE][/QUOTE]
This line gets the current system time (a form which doesn't reset to zero). The script calculates the difference between the program start time and the current time.

I'm assuming there's no easier way to do this. Is thread locking really automatic? Am I misreading the documentation?

[CODE]from threading import Thread

make_thread = lambda fn, *args: Thread(None, fn, None, args).start()

def my_fn(*args):
for arg in args: print (arg)

make_thread(my_fn, "toaster", "ovens")
[/CODE]

[QUOTE=shoemoodoshaloo;1427360]You can sort a dictionary by key or value with the sorted() function[/QUOTE]
My book is wrong.

Awesome advice. Thank you.

Is that the only way to go?
I have no idea. I've never had the need.
[CODE]
string[offset] -= 1;
[/CODE]

[QUOTE=pankaj37marwal;1426463]#include or #include[/QUOTE]
Have you tried removing the ".h"?

You could obscure the strings by changing the value of their characters.

[QUOTE=WaltP;1426573]No I didn't. I said:
"[I]Every time you find a ( remember it's position in the string.[/I]." You may have to remember 2 or more positions.[/QUOTE]
Yes, that's right. My mistake.

[QUOTE=WaltP;1426566]Start at the beginning of the string and look at each character.
Every time you find a ( remember it's position in the string.
When you find a ), evaluate the equation from the last ( to the just found )
Remove that equation replacing it with the answer just found.
Continue...[/QUOTE]
You forgot one step: If a "(" is found instead of a ")" after the first "(", then remember the new position and look for the next ")".

[QUOTE=vincentjohn;1426383]the one that can almost do anything a programmer wants to do with[/QUOTE]
Python, because it's high-level, has a fantastic memory manager/recycler, a library that supports most anything anybody can ask of it, and has almost as little restrictions as C++. With Python formatting a programmer can practice coding style by understanding not only what works, but what looks elegant/pretty. Still, C++ is probably the most efficient because it is one of the lowest levels commonly programmed at. Alternatively, C++ teaches you the inner workings of the computer. Finally, Java is in my opinion a great middle ground between Python and C++.

What are some hints or tips on writing efficient functions? I read about converting the code to assembly, and I've also read about the stack and functions. However, I fail to understand how data stored higher can be accessed before the end on a FIFO stack, as any local variable can be accessed at any point in scope. Obviously I'm misunderstanding something.

I did a project like this in Java. The goal was to write the entire calculator using the functional programming style. I ran into quite a few problems involving parsing the negative numbers versus minus operators, and ended up adding a bunch of if statements checking for "-" after "e" and silly things like that. I probably should have re-thought the entire application. I did parsing of parentheses recursively until string had no more parentheses. That was the easy part. The difficult part was figuring out every possible input for error checking. Maybe you could persuade this community to post some input for your program. Also, I'm interested in seeing your approach. Would you attach some source code to your next post if you have some?

[QUOTE=tonyjv;1426369]Keys aren't ordered in dict, nothing to do with sorting the values, however, which are lists.[/QUOTE]
Not true.
If the values were lists the dictionary would be sortable. However, the keys converted to a list can be read in any order when organized. But then you'll have a list or two. A [B]dictionary[/B] is mostly like a keyed [B]set[/B], not a list.

Thank you.
It wasn't posted to be better than or equal to what's readily available. I want to get some feedback on my code to facilitate improvement.

  • Sean

It is impossible to sort a dictionary. They aren't ordered.

This one is recursive.
[CODE]static void bubble_sort(int* arr, const int len,
const bool done = true, const int i = 0)
{
if (i == len) {
if (done) return;
return bubble_sort(arr, len);
}
if (arr[i] > arr[i+1]) {
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
return bubble_sort(arr, len, false, i+1);
} else return bubble_sort(arr, len, done, i+1);
}
[/CODE]