-5
#include <stdio.h>

int count_cycle(int n);

int main() {
    int N; scanf("%d", &N);
    printf("%d",count_cycle(N));
    return 0;
}

int count_cycle(int n) {
    int N = n; int _N;
    int count = 0;
    int x, y;
    while(_N!=N) {
        x = N / 10;
        y = N % 10;
        _N = 10 * y + ((x+y)%10);
        count++;
    }    
    return count;
}

I've been just practicing the basic algorithm about the specific natural numbers that have cycle which is represented above. But this code's result is infinite loop. My intended pseudo-code is like this.

function count_cycle    
count = 0
    ==loop==
    N = 10x + y (0 <= x,y <= 9)
    N' = 10 * y + (x+y) mod 10 
    if N' == N then 
        break;
    else then 
        N = N' 
        count++ 
        continue;
    ====
return count

How should I make this code work properly?

user2736738
  • 30,591
  • 5
  • 42
  • 56
robin eu
  • 39
  • 5
  • 9
    You do not initialize `_N` – Gaurav Sehgal Jan 11 '18 at 07:24
  • 2
    What is the initial value of `_N`? In C, uninitialized local variables are really uninitialized, and their values will be *indeterminate* (and seemingly random). – Some programmer dude Jan 11 '18 at 07:24
  • 1
    `int _N` is equal to what when you compare it to `N` the first time? Also, choose another name. An underscore followed by a capital letter is reserved to the implementation for **any use**. – StoryTeller - Unslander Monica Jan 11 '18 at 07:25
  • 4
    Also (but unrelated to your problem) symbols starting with an underscore and followed by an upper-case letter (like e.g. `_N`) are *reserved* in all scopes. You should not define any such names anywhere. – Some programmer dude Jan 11 '18 at 07:25
  • As a way to solve your problem, perhaps you should consider a `do ... while` loop instead? – Some programmer dude Jan 11 '18 at 07:26
  • 2
    Furthermore, your question belongs to a category described [here](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – StoryTeller - Unslander Monica Jan 11 '18 at 07:26
  • read compiler warnings first – phuclv Jan 11 '18 at 07:26
  • Check return value of scanf. Turn on compiler warnings (*-Wall -Wextra* for clang and gcc) and fix them. Add debug prints to see values of variables every loop iteration. Basic debugging... – hyde Jan 11 '18 at 07:27
  • [(Why) is using an uninitialized variable undefined behavior?](https://stackoverflow.com/q/11962457/694733) – user694733 Jan 11 '18 at 07:30
  • 3
    Ignoring the uninitialized variable and the other bugs there is nothing that changes in the loop. Either it will end after the first time, or keep doing the same calculation over and over again. N never changes, x and y get their values from N, _N gets its value from x and y. The exact same calcuation is done over and over. Why would you expect the loop to not be infinite after one loop? – Art Jan 11 '18 at 07:40
  • Maybe you could rephrase what you try to do with that code. What is your definition of a cycle? – Gerhardh Jan 11 '18 at 07:54
  • Most likely not the issue, but the behaviour on using the token `_N` is undefined (don't start a variable with a single underscore followed by a capital letter). – Bathsheba Jan 11 '18 at 08:10

2 Answers2

-1

You miss the initialization of _N, so it will never be equal to N and you will rest into the while loop forever

S-Wing
  • 485
  • 6
  • 25
  • _N will hold an indeterminate value at the first lap of the loop, but after that it gets assigned a value. It would seem that the root of the problem is the algorithm, not the initialization bug. – Lundin Jan 11 '18 at 07:45
  • Question: in the pseudo code what does it mean this syntax (0 <= x,y <= 9) ? – S-Wing Jan 11 '18 at 07:58
  • I think the result can be anything instead of just while loop forever. e.g. if you use clang with -O2, the count_cycle() do nothing. https://godbolt.org/g/6Md2tt – Petar Petrovic Jan 11 '18 at 08:05
  • This isn't the reason. `N` never changes in the loop, so `x` and `y` never change either, so `_N` also never changes. So either loop is never entered, or will be infinite loop. – hyde Jan 12 '18 at 17:16
-1

_N isn't initialized it might be having a garbage value

  • Garbage value isn't reason for infinite loop, because `_N` is assigned in the loop, so it will have valid value 2nd time. Garbage value could only make the loop to be never entered, or optimized out, but it can not really cause an infinite loop (ok, undefined behavior, theoretically it can do that too, but that's not in any way a reasonable result from a compiler). – hyde Jan 12 '18 at 17:17