Find the hidden Infinite Loop - c++

I'm working on this problem and my solution seems to work for every case I can imagine to try, and do so in well under the 3 second time limit, but when I submit online it still exceeds 3 seconds. I think there must be a case that causes this while loop to go on indefinitely:
while (!equals(availableChars, testChars)){
next = getNextAlphaString(high, next, availableChars, it);
fillCharSet(next, testChars);
}
but I've tested my functions a bunch and I can't figure out what it is...hoping you guys might see something. Here are the helper functions:
bool equals(multiset<char>& availableChars, multiset<char>& test){
multiset<char>::iterator it;
for (it = availableChars.begin(); it != availableChars.end(); it++){
if ((int)availableChars.count(*it) != (int)test.count(*it)) return false;
}
return true;
}
*
string getNextAlphaString(char& high, string next, multiset<char>& availableChars, multiset<char>::iterator& it){
for (int i=next.size()-1; i>=0; i--){
if (next[i] != high){
it = availableChars.find(next[i]);
while(*it == next[i]){
it++;
if (it == availableChars.end()){it = availableChars.begin(); break;}
}
next[i] = *it;
break;}
else{
it = availableChars.begin();
next[i] = *it;
}
}
return next;
}
*
void fillCharSet(string in, multiset<char>& chars){
chars.clear();
for (int i=0; i<in.size(); i++){chars.insert(in[i]);}
}

Here is an article to detect infinite loops using GDB:
http://www.unknownroad.com/rtfm/gdbtut/gdbinfloop.html
Hopefully this will help you with the problem.

Well...here I go answering my own question. I rewrote it with next_permutation, and it was massively easier and more efficient. I still haven't solved the actual looping problem with the other solution, but now I don't need to. Thanks everyone for the tips :)

Related

unordered_set erase not working in C++

Consider the following situation
void first(){
unordered_set<int> validPorts;
int roundNum=0, preFunctionSize, postFunctionSize,j=0 ;
while(j <100){
if(some_condition_A){
validPorts.insert(some_int_value);
}
j++;
}
do{
preFunctionSize = validPorts.size();
second( validPorts, some_int_value );
postFunctionSize = validPorts.size();
}while(roundNum<12);
}
void second( unordered_set<int> & validPorts, int some_int_value ){
for (auto it = validPorts.begin(); it != validPorts.end();) {
if (it == validPorts.find(some_int_value)) {
validPorts.erase(it++); // <== CODE enters here, I checked
} else {
++it;
}
}
}
So I expect that the postFunctionSize should be less than the preFunctionSize since I know that it went till the erase function. But it looks like the erase function does not work since i get the same value for the two of them. I am not really sure whats happening here and what is causing it. Can you guys please help me out on what might be wrong with this?
Your code is pseudo of course in places but you need to do:
it = validPorts.erase( it );
in a loop where you are iterating through a collection erasing some of them.
However that is also not really what you want to do. You are trying to erase a value from your unordered_set so just do
validPorts.erase( some_int_value );
and no loop.

Trouble removing elements from C++ vector

I'm trying to remove 'dead' bullets from my vector of bullets. Every frame, I'm calling the Bullet::update() function which looks like this:
void Bullet::update()
{
for(int i = 0; i != mAmmo.size(); i++)
{
if(mAmmo[i].sprite.getPosition().x > 700)
mAmmo[i].mAlive = false;
if(mAmmo[i].mAlive == false)
{
// I get a Debug Assertion Failed at runtime from this piece of code
mAmmo.erase(mAmmo.begin()+i);
}
if(mAmmo[i].mAlive == true)
{
mAmmo[i].sprite.move(mMovement);
}
}
}
Am I doing this completely incorrectly? This is the first time I've really used vectors more than just following through a tutorial. If I need to post any more code, just tell me. I've been working on this for the past few hours, so I'm a wee bit desperate to get this to work.
Thanks in advance!
You're easily walking into undefined behavior as soon as the ith element is the last element in your list. Use iterators, and pay special attention to the return value of erase(), as it automatically advances the iterator for you so your loop doesn't have to.
void Bullet::update()
{
for (auto it = mAmmo.begin(); it != mAmmo.end();)
{
if(it->sprite.getPosition().x > 700)
it->mAlive = false;
if (!it->mAlive)
{
// erase and get next iterator
it = mAmmo.erase(it);
}
else
{ // move and increment
it->sprite.move(mMovement);
++it;
}
}
}

Vector int iterators

I'm currently trying to run through a vector of ints with an iterator. Every time I run through it, I'm only getting a value of 0. Both vectors do have valid data in them.
OffenseName and Down are private member data that the user inputs.
vector <string> :: iterator itr;
vector <int> :: iterator itrdown;
int count = 0;
for (itr = dataOffenseName.begin(); itr!= dataOffenseName.end(); ++itr)
{
if ( OffenseName == *itr )
{
for (itrdown = dataDown.begin(); itrdown != dataDown.end(); ++itrdown)
{
//Here itrdown is always coming at 0. The dataDown vector
//does have valid data in it
if (Down == *itrdown)
{
count++;
}
else
{
break;
}
}
}
}
return count;
if (Down = *itrdown)
Come on, the oldest trick in the book :)
if (Down == *itrdown)
will be correct.
if (Down = *itrdown)
should be
if (Down == *itrdown)
The former is not a comparison, just an assignment, meanwhile you meant second: comparison.
However, there is a more important lesson in here: turn the warnings on when building. If you had done, you could not have missed this one unless you disregard warnings without evaluating them which is not a good idea.
That is probably a more important lesson to learn out of this situation because you would not need to debug issues like this at all then. ;-)

MAgic Square function C++

This is my last function for my magic square and for some reason it's giving me an error that there is "'[int]' for array subscript" but I don't know what that means, if someone could help explain what I have to do.
bool Square::is_magic()
{
for (i = 0; i < size-1; i++)
{
if (sum_row[i] != sum_row[i+1])
return false;
if (sum_col[i] != sum_col[i+1])
return false;
}
if (sum_row[0] != sum_col[0])
return false;
if (sum_row[0] != sum_maindiag[0])
return false;
if (sum_row[0] != sum_other[0])
return false;
return true;
}
Ok everybody was beginer at some time. I really recommend you to read one or two books focused on c++. (Personally I learned programming with "Learn c++ in 21 days", many complain but it was good start for me).
And for the code. Not sure that it's what you need, it should go like this:
bool Square::is_magic()
{
int i;
for (i = 0; i < size-1; i++)
{
if (sum_row[i] != sum_row[i+1])
return false;
if (sum_col[i] != sum_col[i+1])
return false;
}
if (sum_row[0] != sum_col[0])
return false;
if (sum_row[0] != sum_maindiag[0])
return false;
if (sum_row[0] != sum_other[0])
return false;
return true;
}
Some comments:
You don't need brackets for 1 command after if,for,while statement
Suggest using if -> else if -> else. Here it doesn't matter because you jump out of function as soon as you find something not correct, but in case you would continue in code you would check other statements even if it wasn't necessary.
Get used to some style, make your own or copy someone's. Personally I use brackets this way:
if (something != somethingElse){
doSomeNastyThings();
doEvenMore();
}
Good luck..
Edit: added variable declaration int for statement, updated brackets (clever idea as last 3 if-s aren't using index)
If statements are formatted like this:
if (condition) {
do_this()
}
not like this:
{
if (condition)
do_this()
}
They way you're formatting your code you're closing your for loop after two lines, which I imagine is not what you're trying to do (since you're referring to var i afterwards).

Can you rewrite this snippet without goto

Guys, I have the following code that is inside a big while loop that iterates over a tree. This is as fast as I can get this routine but I have to use a goto. I am not fundamentally against goto but if I can avoid them I would like to. (I am not trying to start a flame war, please.)
The constraints:
The current=current->child() is expensive (it's a shared_ptr) so I'd like to minimize the use of that operation at all cost.
After the operation current should be the last child it found.
cnt must count each child it encounters.
cnt++ will be replaced by some other operation (or several operations) and should only appear once :)
the code:
insideloopy:
cnt++;
if ( current->hasChild() )
{
current = current->child();
goto insideloopy;
}
Edit: Sorry guys, originally forgot to mention cnt++ should only appear once. It will be some kind of operation on the node, and should thus only be there one time. I'm also trying to avoid making that another function call.
Original answer
Assuming this is C or C++:
while (cnt++, current->hasChild())
{
current = current->child();
}
I'm not a big fan of the comma operator usually, but I don't like repeating myself either :)
Updated 'fun' answer
After learning that cnt++ is actually some multiline operation, this particular syntax would be less than ideal. Something more along the lines of your accepted answer would be better.
If you want to be really funky, this would also work:
do
{
cnt++;
} while (current->hasChild() && (current = current->child()));
Now I feel really dirty though, with my abusing the short circuiting on the && operator :)
Sane answer
Exercises in compactness aside and striving for readable code, I'm forced to conclude that one of the existing answers is best suited (I'm just including this for completeness' sake):
while (true)
{
cnt++;
if (!current->hasChild()) break;
current = current->child();
}
The while (true) will be optimized by the compiler into a regular infinite loop, so there is only one conditional statement (if you care about that).
The only thing going against this solution is if your node operation was a long piece of code. I don't mind infinite loops so much, as long as I can see where they terminate at a glance. Then again, if it were really long, it should be a function anyway.
cnt++;
while(current->hasChild())
{
cnt++;
current = current->child();
}
EDIT:
If you only want cnt++ to be in your code once:
while(true)
{
cnt++;
if(current->hasChild())
current = current->child();
else
break;
}
insideloopy:
cnt++;
if ( current->hasChild() )
{
current = current->child();
goto insideloopy;
}
I love infinite loops.
while (true) {
cnt++;
if (!current->hasChild()) break;
current = current->child();
}
Of course you can do it in many other ways (see other answers). do while, put the check in the while, etc. In my solution, I wanted to map nearly to what you are doing (an infinite goto, unless break)
You can use break to get out of the loop in the middle of the code:
while (true) {
cnt++;
if (!current->hasChild()) break;
current = current->child();
}
while (current->hasChild())
{
cnt++;
current = current->child();
}
Or am I missing something?
for(cnt++ ; current->hasChild() ; cnt++) {
current = current->child();
}
I'd investigate the possibility of making current->child() return NULL when it has no child if it doesn't already -- that seems the best possible result and leaving it undefined in this case seems error prone -- and then use:
for (; current; current = current->child())
{
cnt++;
}
No break statements:
notDone=true;
while(notDone){
cnt++;
if ( current->hasChild() ){
current = current->child();
} else {
notDone=false;
}
}