简体   繁体   English

C++无限循环问题

[英]C++ infinite loop issue

I am not sure what the issue may be.我不确定可能是什么问题。 I added "cout << count;"我添加了“cout << count;” to the if statements to see why there is no return value 'val'.到 if 语句,看看为什么没有返回值 'val'。

But now I see there is an infinite loop occurring.但现在我看到有一个无限循环发生。

I'm essentially trying to count how many loops it takes for num = 0.我本质上是在计算 num = 0 需要多少个循环。

At first I thought it was num = count being in the loop.起初我以为是 num = count 在循环中。

Any suggestions?有什么建议? Thanks in advance.提前致谢。

#include <iostream>
using namespace std;

// hailstone function prototype
int hailstone(int &num);


int main()
{
    int val;

    cout << "Enter integer" << endl;
    cin >> val;

    hailstone(val);

    cout << val << endl;

    return 0;


}

// Pass values by reference to hailstone function header
int hailstone(int &num)
{   

    do
    {
        int count = 0;    

        // If num is even
        // Divide by two
        if(num % 2 == 0)
        {
            num = (num / 2);
            count++;
            cout << count;

        }

        // If num is odd
        // Multiply by 3 and add 1
        else if(num % 2 != 0)
        {
            num = (num * 3) + 1;
            count++;
            cout << count;

        }
    // Assign the number of steps to num
    num = count;

    } while(num > 0);

    // Return the number of steps
    return num;
}

There are a few issues here, but the most pertinent is this line:这里有一些问题,但最相关的是这一行:

// Assign the number of steps to num
num = count;

You're essentially resetting num after you update it based on the rules of the hailstone sequence.根据冰雹序列的规则更新 num 后,您实际上是在重置它。 Because 'count' is initialized at the start of each loop and then iterated once in either the 'if' or 'else if' block, it is always one.因为 'count' 在每个循环开始时初始化,然后在 'if' 或 'else if' 块中迭代一次,所以它始终为 1。 Therefore, at the end of each loop, num is always 1.因此,在每个循环结束时,num 始终为 1。

Whether you are doing num = num/2;你是否在做num = num/2; (for even numbers) or num = (num * 3) + 1 (for odd numbers), the number will never become 0 or less . (对于偶数)或num = (num * 3) + 1 (对于奇数),数字永远不会变成0 or less That's why your code runs for infinite time.这就是您的代码运行无限时间的原因。 So you need to change the line while (num > 0);所以你需要改变行while (num > 0); to while(num > 1);while(num > 1); inside the function hailstone (maybe it is line number 54).在函数hailstone内部(可能是第 54 行)。 As like as follow:如下所示:

Old code:旧代码:

int hailstone(int &num)
{   

do
{
    int count = 0;    

    // If num is even
    // Divide by two
    if(num % 2 == 0)
    {
        num = (num / 2);
        count++;
        cout << count;

    }

    // If num is odd
    // Multiply by 3 and add 1
    else if(num % 2 != 0)
    {
        num = (num * 3) + 1;
        count++;
        cout << count;

    }
// Assign the number of steps to num
num = count;

} while(num > 0); //You need to change this line

// Return the number of steps
return num;
}

New Code:新代码:

int hailstone(int &num)
{   

do
{
    int count = 0;    

    // If num is even
    // Divide by two
    if(num % 2 == 0)
    {
        num = (num / 2);
        count++;
        cout << count;

    }

    // If num is odd
    // Multiply by 3 and add 1
    else if(num % 2 != 0)
    {
        num = (num * 3) + 1;
        count++;
        cout << count;

    }
// Assign the number of steps to num
num = count;

} while(num > 1); //Here is the change

// Return the number of steps
return num;
}

Another issue: You declares num = count (in line 52), which will not return the actual value (if your code is for counting number of loops).另一个问题:您声明了num = count (在第 52 行),它不会返回实际值(如果您的代码用于计算循环次数)。 Example: If the input is 12 than the loop will run for 10 times but your code will print 11 , I don't think this line is required.示例:如果输入是12比循环将运行10次但您的代码将打印11 ,我认为不需要这一行。 Return the value of count variable after your do-while loop breaks.do-while循环中断后返回count变量的值。 Thanks谢谢

显然,'if' 或 'else if' 条件将被满足,这将使 count 增加到 1 ,并且分配 num =count 使 num =1 ,so(num>0) 总是 true.so 循环永远继续

do
{
  int count = 0;    

  if (num % 2 == 0)
  {
    num = (num / 2);
    count++;
    cout << count;
  }
  else if(num % 2 != 0)
  {
    num = (num * 3) + 1;
    count++;
    cout << count;
  }

  num = count;

} while (num > 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM