Positive, Negative, or Zero

The strcmp() function returns a value based on the comparison of two strings. The value is zero when the strings match, otherwise the value is positive or negative depending on how the strings compare. This result makes me wonder which other functions can return positive, negative, or zero values and whether the C library has a sign() function or similar that helps make such a determination.

Long story short: It doesn’t.

Back when I was a BASIC language wizard, I knew the SGN command. It returns -1, 0, or 1 based on the sign of a an integer. Such a function, absent from the standard C library seems rather simple to code, which is your task for this month’s Exercise: Write a sign() function.

The sign() function accepts an integer value as input. It returns -1, 0, or 1 depending on the value’s sign: negative, zero, or positive. This functionality is the same as the old BASIC SGN command.

To assist you with a solution, here is a code skeleton:

2021_03_01-Lesson.c

#include <stdio.h>

int sign(int a)
{
}

int main()
{
    int values[20] = { -34, 27, 0, 48, -25, 28, -55,
        66, -17, -78, 0, -20, 40, -98, 63, -44, 59,
        6, 65, 90
    };
    int x;

    for( x=0; x<20; x++ )
    {
        printf("Sign of %3d is ", values[x] );
        switch( sign( values[x] ))
        {
            case -1:
                puts("negative");
                break;
            case 1:
                puts("positive");
                break;
            case 0:
                puts("zero");
                break;
            default:
                puts("unknown");
        }
    }

    return(0);
}

The code above lacks guts for the sign() function. Your task is to code the guts.

In the sample code skeleton, each integer in the values[] array is passed to the sign() function. The result is used immediately in a switch-case structure, where text is output based on the value returned: negative, positive, or zero. A default condition is added Just In Case.

Please complete the Exercise by coding the sign() function. Upon success, the output looks like this:

Sign of -34 is negative
Sign of  27 is positive
Sign of   0 is zero
Sign of  48 is positive
Sign of -25 is negative
Sign of  28 is positive
Sign of -55 is negative
Sign of  66 is positive
Sign of -17 is negative
Sign of -78 is negative
Sign of   0 is zero
Sign of -20 is negative
Sign of  40 is positive
Sign of -98 is negative
Sign of  63 is positive
Sign of -44 is negative
Sign of  59 is positive
Sign of   6 is positive
Sign of  65 is positive
Sign of  90 is positive

Try this Exercise on your own before you check my answer.

2 thoughts on “Positive, Negative, or Zero

  1. Thanks for the found throwback to Basic SGN function! I walked into a room last night where my wife was streaming “To tell the truth” and the “I am a sexologist” caught my attention. I sat down and then promptly said well it can’t be # 2. My wife was curious why I was so adamant. So I said I was pretty sure Dan writing so many “… For Dummies” books would have time or the need for a second career. But your answers made you sound convincing. 🙂

Leave a Reply