The problem: http://projecteuler.net/problem=22
note that there is a textfile that goes with it.
It seems that my program either is eternal or takes over 5 hours. If you can spot something that makes this slow, whithout changing the approach totally, please note me. I am a bit tired, that is why I need some extra help. Thanks in advance!
You aren't null terminating your c-strings, this would be the first thing. I don't think position() will be able to open the file, since it is already open in main. Also just noticed you are trying to do == with a c-string, this shouldn't even compile.
I don't think I want to change my hole approach cire, it doesn't take more than one minute anyways. I try to keep my programs under one minute and this does not go over 1min. I have tried my program now with the example name in the problem and I got the exact same result. The program compiles but give wrong answer.
I don't see you sort the names anywhere and the correct answer depends on them being sorted alphabetically. And as cire said already, you are opening the file thousands of times, very inefficient. Just open the file and parse once, store the names into an array. Sort it like
naraku9333 My solution is counting all the names that are before itself, in alphabetical order. It may not be the most efficient but it still should work. Even if I were to do it with another approach i still want to find the problem with my current program.
I think all of this is just reversing my progress, when I tested it 5 hours ago with one name it worked fine. I think the problem is in getting the names... Please try to help me spot mistakes in my current code. (the one above)
Traditionally a "compare" function works by returning 0 if the two parameters are equal to each other, greater than 0 if the first is greater than the second and less than 0 if the first is less than the second. Why didn't you just use strcmp?
What made you choose x as the variable name for your bool in main? Just that uncreative?
still think the problem may be in the main()
Afraid not. You should really stop guessing where the problem may be and reason about why the program behaves the way it does.
Now that you have a working compare function, you need to revisit position.
When you're comparing the parameter to names read from the file, at some point you're going to compare the name to itself. Given that the compare function returns true when you compare a string to itself (making it the equivalent of a greater-than-or-equal function) and lines 91-94 in your code, what do you think should be the initial value of pos in position?