3

So I want to start by saying that I already solved the problem, but there is something that is bugging me,

Here is the code first:

#include <stdio.h>

int flag = 1;

int controlNumber(int);

int main() {    
    int array[10] = { 233, 45, 777, 81, 999999, 36, 90, 88, 11, 61 };   
    int i;  
    int c;  

    for (i = 0; i < 10; i++) {
        printf("%d >>  ", array[i]);
        c = controlNumber(array[i]);
        if (c == 1) {           
            printf("all digits are equal\n");
        } else {
            printf("not all digits are equal\n");
        }
    }
    return 0;
}

int controlNumber(int a) {
    int q = a;
    int r = a % 10;
    int temp;
    
    while (q != 0) {
        temp = q % 10;
        if (temp == r) {
            q = q / 10;
        } else {
            flag = 0;
            return flag;
        }
    }
    return flag;
}

The code works only if the global variable flag is made local inside the scope of the function controlNumber with a value of 1, and I can't really figure out why that is the case since the logic should still be the same.

Also, I'm still a beginner to some extend so I apologize for any indentation errors.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
blake
  • 107
  • 1
  • 7
  • This is one of these problems that are better to be solved in "reverse". You get one digit of the number, say it is `8`. Now just repeatedly check your number against `8`, `88`, `888` and so on, till it becomes less than it or equal. – Eugene Sh. Feb 12 '21 at 21:12

2 Answers2

4

For starters it is a very bad idea to define a function that depends on a global variable. In fact it is the reason of the bug of your program. You forgot to reset the global variable flag to 1 each time before calling the function.

The function can be defined the following way without any redundant global variable

int controlNumber( int n )
{
    const int Base = 10;

    int digit = n % Base;

    while ( ( n /= Base ) && ( n % Base == digit ) );

    return n == 0;
} 

Here is a demonstrative program.

#include <stdio.h>

int controlNumber( int n )
{
    const int Base = 10;

    int digit = n % Base;

    while ( ( n /= Base ) && ( n % Base == digit ) );

    return n == 0;
} 

int main(void) 
{
    int array[] = { 233, 45, 777, 81, 999999, 36, 90, 88, 11, 61 };
    const size_t N = sizeof( array ) / sizeof( *array );
    
    for ( size_t i = 0; i < N; i++ )
    {
        printf( "%d >>  ", array[i] );

        if ( controlNumber( array[i] ) )
        {
            printf( "all digits are equal\n");
        }
        else 
        {
            printf( "not all digits are equal\n" );
        }
    }
    
    return 0;
}

The program output is

233 >>  not all digits are equal
45 >>  not all digits are equal
777 >>  all digits are equal
81 >>  not all digits are equal
999999 >>  all digits are equal
36 >>  not all digits are equal
90 >>  not all digits are equal
88 >>  all digits are equal
11 >>  all digits are equal
61 >>  not all digits are equal

If for example you will change the constant Base in the function and make it equal to 8

int controlNumber( int n )
{
    const int Base = 8;  //10;

    int digit = n % Base;

    while ( ( n /= Base ) && ( n % Base == digit ) );

    return n == 0;
} 

then you will get the following result.

233 >>  not all digits are equal
45 >>  all digits are equal
777 >>  not all digits are equal
81 >>  not all digits are equal
999999 >>  not all digits are equal
36 >>  all digits are equal
90 >>  not all digits are equal
88 >>  not all digits are equal
11 >>  not all digits are equal
61 >>  not all digits are equal

In this case for example the number 45 has the same digits in the octal representation because in the octal representation the number is written like 055.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

Global variables retain their value. In your case, if flag is global, once set to 0 (within controlNumber) it will keep that value. There is no other place in the code to make it 1 again.

Making it a local variable to the function will cause it to initialize to 1 every time the function is called.

Edit: if you want to have it as global, you could set it to 1 every time controlNumber returns 0, i.e., before or after printf("not all digits are equal\n");.

Zois Tasoulas
  • 1,242
  • 1
  • 11
  • 23