0

I know the answer is probably very simple, but I've nearly exhausted myself trying everything I know in c++, which isn't much. The first function works, to my knowledge, I just can't get the loop to terminate.

#include <iostream>
#include <cstdlib>

using namespace std;

unsigned int reverseInt(unsigned int num)
{
    unsigned int rem, reverse;

    while(num > 0)
    {
        rem = num % 10;
        reverse = reverse * 10 + rem;
        num /= 10;
    }
    return reverse;
}

unsigned int generateSequence(unsigned int num, ostream& out)
{
    unsigned int k = 0;

    while(num != 1)
    {
        num = num + 4;
        num = reverseInt(num);
        k++;
        out  << ", " << num;
    }
    out << endl;
    return k;
}
4
  • 1
    Please make it a minimal reproducible example. Not much is missing, but a main would be nice and you have to tell us what input you used Commented Feb 21, 2020 at 16:16
  • also explain what the code is supposed to do. Why do you expect the loop to terminate? How can adding 4 to num eventually make it equal to 1? What if num starts as 5? Commented Feb 21, 2020 at 16:18
  • @idclev463035818 It's not just adding 4, it's also reversing the number. Without the bug in reverse, it would reach 1 after 52 steps when num starts as 5. Commented Feb 21, 2020 at 16:30
  • Read this article for tips on debugging your code. Commented Feb 21, 2020 at 16:58

3 Answers 3

2

There are few problems with the code

  1. reverse variable is not getting initialized even once in function reverseInt.

  2. num variable is not getting reduced to 1 and loop will not terminate.

Sign up to request clarification or add additional context in comments.

1 Comment

"So, i would say please describe what you want to do in each function then we will be able to help you better." The comment section would be a better place to put this remark. I know you are trying to help, but it is advised not to answer a question that isn't yet well-defined.
1

While analyzing your logic it looks like you have a finite loop with higher outcome as number of digits increase.

Your application reduces number of digit when sum of 4 reaches a digit ending with 6, But since you have unsigned int type which has higher range.

Application will get timeout if you go reaches threshold number of digits which generate more outcome for compiler to exit, say 5 digit number as threshold Eg: 11111.

Comments

0

Your issue is mainly that reverse is not initialized, on at least clang and gcc this means reverse will be initialized with its maximum value (2^64).

This means your loop never finishes as num just gets bigger and bigger and bigger, you will have a lot of integer overflows but since num is assigned from the return value of reverse, num will never hit 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.