2nd Challenge


#1

So i am trying to get creative and solve this but for some reason when trying to find the largest number i find the lowest when trying to find the lowest i find the largest and when trying to find unsigned i get stuck in a loop.


 
    // Second part
    
    // setting variable with startup values
    short x1 = 0;
    
    // Printing startup value
    
    printf("short x1 is currently %d\n", x1);
    
    // Loop to find highest number allowed for short
    printf("\nSigned Short:\n");
    x1  = 0;
    for (x1 = 0; x1 - 1 < !(abs(x1)) ; x1--){
        if (x1 - 1 == x1) {
            break;
        }
    }
    
    printf("The largest number the short x1 can hold is %d\n", x1);
    
    // Loop to find lowest number allowed for short
    x1 = 0;
    for (x1 = 0; x1 + 1 > abs(x1); x1++){
        if (x1 + 1 == x1){
            break;
        }
    }
    // i get a problem with the abs command, says implicit declaration of function although it works so ???
    printf("The smallest number the short x1 can hold is %d\n", x1);
    
    printf("\nUnsigned short:\n");
    
    
    // loop to find highest for unsigned short. Unfortunately unlike the first 2 i can't make it find it without inputting a numbered roof :( wanted it to solve itself but cant get it right.
    unsigned short x2  = 0;
    for (x2 = 0; x2 + 1 > x2; x2++) {
        if (x2 + 1 == 65536) {
            break ;
        }
    }
   
    printf("The largest numb er the signed short x2 can hold is %d\n", x2);
    
    for (x2 = 0; x2 - 1 > x2; x2--) {
        if (x2 - 1 == 65536) {
            break ;
        }
        printf("%d\n",x2);
    }
    
    printf("The largest numb er the signed short x2 can hold is %d\n", x2);

    

Any help on how i could have solved this without creating a cap and why the abs thing says implicit function but works none the less?


#2

To answer your first question, when working with loops, you need caps or the loop will not know to stop. As for the second part, the ‘abs’ function is part of the standard library. To get rid of the warning, you would have to include the <stdlib.h> because that’s where the function is specifically declared. Without it, the compiler is, in a way, implying what the function is supposed to do (implicit).