Your Ad Here

Tuesday, May 11, 2010

Fizz Buzz Busted!?

On the site Computer Science 101, Erik Oosterwal presents two different solutions to the FizzBuzz problem and demonstrates why it is important for developers on larger software teams to write code that is developer-friendly even when the developer-friendly code is less efficient.

Here's a preview of one of the two solutions:


void FizzBuzz2(void)
{
    int i = 0;
    char output[10];
    for(i = 1; i<=100; i++)
    {
        strcpy(output, "");
        if(i%3==0)
        {
            strcpy(output, "Fizz");
        }
        if(i%5==0)
        {
            strncat(output, "Buzz", 4);
        }
        if(strcmp(output, "")==0)
        {
            printf("%d\n", i);
        }
        else
        {
            printf("%s\n", output);
        }
    }
}

No comments: