Sunday, March 8, 2009

Teaching C programming

This week I gave my C programming class an assignment in which I was able to observe their comprehension of programming in C.

The assignment was as follows: Write a program that inputs two floating-point values from the keyboard and then displays their product.

I directed them to a sample piece of code as follows:

#include
int main(void)
{
float y;
int x;
puts("Enter a float, then an int");
scanf( "%f %d", &y, &x);
printf( "\nYou entered %f and %d ", y, x);
return 0;
}

I had the students think through each line of the above code so that they understood how this is taking in 2 numbers and printing them back out.

Then I had them consider the following:

Arguments in printf () can be any valid C expression. For example, to print the sum of x and y, you could write:

total = x + y;
printf("%d", total);

You also could write

printf("%d", x + y);


With the above information, they should be able to figure out how to complete the assignment. It was interesting watching them piece together these bits of information from different locations to come up with a complete solution. The students also felt the satisfaction of figuring out the solution on their own, since I gave them hints in the right direction, but they still had to make the connections on their own.

No comments:

Post a Comment