Click here to Skip to main content
14,664,450 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am writing a code in the following function(detectHappy) that will check whether a number is a happy number or not, for 10 cycles (iterations) only. My function should :

-Find the sum of the digits of the number.

-Check the result obtained in point 1. If it is 1, assign value 1 to the variable 'finalNumber', else again execute point 1, till the number obtained is 1 or till the number of cycle increases to 10.

-Assign the iteration value to the variable 'cycle_no'.

I am not supposed to input, output or declare any additional functions. I just have to write the body of the function already declared there that will make it work on the shell offered in my course...

The programme I have attempted to write compiles but does not give the correct results (e.g number 6 generates the value 32765 after 10 cycles instead of the expected 145. I am now short of ideas and I will be grateful for any assistance. Thank you in advance.

What I have tried:

/*Question      : Write your code to find whether the number is a happy number or not (for max 10 cycles).
int number      : The number to be determined whether it is happy or not
int finalNumber : Store the resultant value in this variable
int cycle_no    : Store the number of iterations done to determine whether the 'number' is happy or not */

void detectHappy(int number, int &finalNumber, int &cycle_no) {

    //Write your solution code below this line
    int sum = 0;
    int i = 1;
    do{
        sum += (number%10)*(number % 10);
        number /=10;
        if (sum == 1){
            finalNumber = 1;
            break;
        }
        else {
            number = sum;
            ++number;
        }
        cycle_no = ++i;
    }
    while (i < 10);
}
Posted
Updated 2-Oct-17 1:50am
v4

Rate this:
Please Sign up or sign in to vote.

Solution 2

You missed the inner loop on number's digits. Try

void detectHappy(int number, int &finalNumber, int &cycle_no)
{
  for (cycle_no=1; cycle_no <= 10; ++cycle_no)
  {
    finalNumber = 0;
    while (number)
    {
      finalNumber += (number % 10)  * (number % 10);
      number /= 10;
    }
    if (finalNumber == 1)
      return;
    number = finalNumber;
  }
}
   
Comments
Member 13440232 2-Oct-17 7:42am
   
Thank you @CPallini...Thank you very much for the hint...Now I see what I omitted...With very little modification the code worked. This is how I did it:


void detectHappy(int number, int &finalNumber, int &cycle_no)
{
for (cycle_no=0; cycle_no < 10; cycle_no++)
{
finalNumber = 0;
while (number)
{
finalNumber += (number % 10) * (number % 10);
number /= 10;
}
if (finalNumber == 1)
return;
number = finalNumber;
}
}
CPallini 2-Oct-17 7:49am
   
You are welcome.
Rate this:
Please Sign up or sign in to vote.

Solution 1

When you don't understand what your code is doing, the answer is debugger.
Happy number - Wikipedia[^]
Get an example from this link and compare with what your code is doing.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100