I'm teaching "Programming in C" this semester. It's a short introductory course and it's fun to see how students find their way through it. Same time it gives me some interesting statistics on students' most typical mistakes and helps me think about programming from a different perspective.

In the last lab exercise, we had to multiply 3x3 matrix A by 3x1 vector x and print out the results. How do you do that?

For example, you can do it like this (1):

`y[0] = A[0][0]*x[0] + A[0][1]*x[1] + A[0][2]*x[2]; `

y[1] = A[1][0]*x[0] + A[1][1]*x[1] + A[1][2]*x[2];

y[2] = A[2][0]*x[0] + A[2][1]*x[1] + A[2][2]*x[2];

or like this (2):

`for(i=0;i<ROWS;i++)`

for(j=0;j<COLS;j++)

y[i] += A[i][j]*x[j];

I always thought, that (2) is a better solution. I'm not so sure anymore. 75% of my students forgot to explicitly initialize y[i]'s to 0. They've tested their program and it worked because the compiler initialized y for them, but still it is a potentially dangerous piece of code!

So, which solution is better? Rigid, but error proof and easy to read, or scalable, but more complex? Depends on the problem, but for this problem the guys who used (2) loose 2 points for not initializing their y's :-)

## No comments:

Post a Comment