Wednesday, October 5, 2022
HomeC ProgrammingCyclops Numbers – Resolution | C For Dummies Weblog

Cyclops Numbers – Resolution | C For Dummies Weblog


I hope this month’s Train didn’t terrify you. I keep in mind seeing The Seventh Voyage of Sinbad once I was a child. The cyclops bothered me a lot I requested my mother questions on him for a month.

A cyclops quantity is neither 10 meters tall nor impervious to javelins. It’s, nevertheless, straightforward to catch if you do as I did and use the sprintf() operate to transform the worth right into a string. Afterwards, you scan the string for the presence of a zero digit after which decide whether or not that single zero dwells within the middle of the odd-character string.

For my answer, I crafted the zero_count() operate. It consumes a string and returns the variety of zeros. This operate helped scale back my answer’s important loop to a handful of if exams that pluck out a cyclopean worth.

2022_09-Train.c


#embody <stdio.h>
#embody <string.h>


int zero_count(char *v)
{
    int a = 0;

    
    whereas( *v )
    {
        if( *v == '0' )
            a++;        
        v++;            
    }

    return(a);
}

int important()
{
    const char digits = 6;        
    const int whole = 100;        
    const int columns = 9;        
    int cyclops,depend,len,mid,x,y;
    char buffer[digits];
    int outcomes[total];            

    printf("The primary %d cyclops numbersn",whole);
    cyclops = depend = 0;
    whereas( depend<whole )
    {
        sprintf(buffer,"%d",cyclops);
        len = strlen(buffer);
        
        if( len>2 && lenpercent2 )
        {
            
            if( zero_count(buffer)==1 )
            {
                mid = len/2;            
                if(buffer[mid]=='0' )    
                {
                    
                    outcomes[count] = cyclops;
                    depend++;
                }
            }
        }

        
        cyclops++;
    }

    
    x = 0;
    whereas( depend )
    {
        for( y=0; y<columns; y++ )
        {
            printf("%5d",outcomes[(x*columns)+y]);
            count--;                
            if( !depend )            
                break;
            if( y<columns-1)
                putchar('t');
        }
        x++;
        putchar('n');
    }


    return(0);
}

The zero_count() operate plows by way of string v searching for zero digit characters. It counts the full and returns this worth.

Within the important() operate, a whereas loop makes use of variable depend to tally the primary 100 cyclops values. Three if exams cull the outcomes as variable cyclops increments from zero.

The primary if check ensures that the string size (len) worth has no less than three digits and an odd size: if( len>2 && lenpercent2 )

The second if check confirms that the worth has just one zero:

if( zero_count(buffer)==1 )

The ultimate if check makes use of variable mid to hunt out the string’s middle digit: mid = len/2; As these variables are integers, the result’s rounded. Then the ultimate if check checks the middle digit: if(buffer[mid]=='0' ) Upon success, the worth is saved within the outcomes[] array, and variable depend is incremented.

The ultimate whereas loop generates a multi-column desk displaying the outcomes. You may learn particulars on how this loop works in a future Lesson.

I hope your answer met with success. From the sooner submit on counting digits, clearly logarithms and division can be used to find out the variety of digits in a price. If you happen to used one in all these approaches in your answer, all the higher!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments