Whiteboarding C Programming

image11

Today after pivoting from pseudocode on the whiteboard, I had J do FizzBuzz on the whiteboard. I set my timer for 20 minutes. I was surprised when he said he was finished. 3 minutes had passed. Somebody is getting good at this🙂

Question Posed:

How would you code in C to make the computer fizz when it finds an even number and buzz when it finds an odd number?

But did he get it right? What about checking the input? An hour later I had the code from J after it had been put into Eclipse and tested:


#include <stdio.h>
#include <stdlib.h>

int fizbiz(int num)
{
    if(num%2==0)
    {
        printf("fiz \n");
        return 0;
    }
    else
    {
        printf("biz \n");
        return 1;
    }

}

int main()
{
    int number;
    printf("Type in a number: \n");
    scanf("%d", &number);
    int result = fizbiz(number);
    printf("Remainder is: %d",result);
    return 0;
}

Next we’ll build from this simple little code snippet into a programming adventure that will reveal the importance of pointers and addresses, compiler specific caveats, and data type reveals!

Question Posed:

How would you update the code in C to have the computer take in an array of numbers?

UPDATE: I asked J to update the code to take in an array of numbers and he sent this back to me:


#include <stdio.h>
#include <stdlib.h>

void fizbiz(int num[])
{
  int i = 0;
  while(i<10)
  {
    if(num[i]%2==0)
    {
        printf("fiz \n");
        printf("Remainder is: 0 \n");
        i++;
    }
    else
    {
        printf("biz \n");
        printf("Remainder is: 1 \n");
        i++;
    }
  }
}

int main()
{
    int number[] = {11, 22, 33, 44, 55, 66, 77, 88, 99};
    fizbiz(number);
    return 0;
}

I want the problem of data types in C to reveal itself in the exercise. Just asked him to update the code to fizz on chars and buzz on ints. Very interested to see how he approaches this!

Question Posed:

How would you code in C to have the computer fizz on chars and buzz on ints?

UPDATE: Five minutes after emailing J the new request he emailed me his updated code:


#include <stdio.h>
#include <stdlib.h>

void fizbiz(int num[])
{
  int i = 0;
  while(i<10)
  {
    if(num[i]%2==0)
    {
        printf("fiz \n");
        printf("Remainder is: 0 \n");
        i++;
    }
    else
    {
        printf("biz \n");
        printf("Remainder is: 1 \n");
        i++;
    }
  }
}

int main()
{
    int number[] = {11, 'b', 'd', 55, 77, 'f', 'h', 99, 33};
    fizbiz(number);
    return 0;
}

Reading over the code, the concept started to open itself up. I remember reading somewhere, “In order to recreate you must first break it into pieces.” If you listen closely you can hear the problem cracking. I emailed J the following.

Question Posed:

How would you code in C to have the computer fizz for a char and buzz for an int? We are no longer checking for even or odd. We are checking datatypes.

Very interested in seeing the code that comes from this request. Will he get it?

UPDATE 3: With a single import of ctype.h, J was able to check to see if the current element in the array is an alpha character or a numeric character. Not exactly what I was looking for but who’s at fault when the problem is not clearly defined?


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

void fizbiz(int num[])
{
  int i = 0;
  while(i<10)
  {
    if(isalpha(num[i]))
    {
        printf("Character: fiz \n");
        i++;
    }
    else
    {
        printf("Integer: biz \n");
        i++;
    }
  }
}

int main()
{
    int number[] = {11, 52, 'b', 'd', 51, 23, 'f', 'h', 45, 13};
    fizbiz(number);
    return 0;
}

I decided to be clearer in my instructions. How can I phrase the question without having to indicate how to solve it?

Question Posed:

How would you code in C to make the computer create an array of ints and longs then fizz for ints and buzz for longs?

Interested to see how he updates the code🙂

Update 4: J sent me the following email:

Hi David,

I just found that the size of an int and long are both 4 bytes. Did you want me to compare another datatype?
Int – 4 bytes
long – 4 bytes
float – 4 bytes
double – 8 bytes
char – 1 byte
J
Exactly what I was looking for! Now I’m interested in showing him the difference between a signed int and an unsigned int. I sent him the following question:
Hi J,

Good going! Please update code to fizz for int and buzz for unsigned int.
David
I’m looking forward to what he discovers along the way!

Author: David Neely

Professional Software Developer. Technology and Web Coordinator at the University of Hawaii's Manoa Career Center.