Category: C++


This week I’ve been busy marking Simulation and 3D Graphics coursework.
I like to try to challenge the students, and this assessment presents a formidible challenge. The coursework is worth 100% of the module, so it’s something the students can really get their teeth into.

Students have to use the OpenGL API to create some 3D graphics. They have to use C++, which a lot of them are still learning and so remains largely unfamiliar. They have to do some non trivial maths too implement some basic physics. On top of that, I’ve insisted that they hand in using SVN, which is a versioning system that some of them have no experience of at the beginning of the course.

The lecturer in the middle has the most balls!

I’m very pleased to be able to say that our students take to these challenges extremely well. This year the students were asked to simulation a probability machine – in which balls fall from the top to the bottom bouncing off of pins inbetween, and collected in buckets at the bottom. The theory is that the balls are more likely to end in the buckets in the middle of the machines, and less likely to end up at the edges. This is demonstrated using a visualization of the number of balls that have fallen into each bucket in the form of a barchart.

The best thing though, is that even though the work is hard the students really seem to enjoy it. They take a lot of pride in what they have achieved, and so they should!

Advertisements

Welcome to the first post for a hypocrites guide to comments!

My first piece of advice in my guide to comments is that you shouldn’t use comments.

That is, if you choose your variable names and method names well, lay out your code well and your code is doing something trivial ( I mean really trivial, not something that seems trivial now but won’t be in three months time when you’re no longer immersed in the project) then your code might not need comments.

To illustrate this consider have a look at this code:


float a(List <int> b)
 {
 int c = 0; float d = 0;
 for (int i = 0; i < b.Count; i++){
 if (b[i] != 0)
 {
 d += b[i];
 ++c;}}
 return d / c;}

Can you work out what that code does? How long did it take you to work it out?

Now look at this code:


float getAverageNonZeroMark(List <int> pListOfMarks)
{
   int numberOfNonZeroMarks = 0;
   float totalSumOfNonZeroMarks = 0;
   for (int i = 0; i < pListOfMarks.Count; i++)
   {
      if (pListOfMarks[i] != 0)
      {
         totalSumOfNonZeroMarks += pListOfMarks[i];
         ++numberOfNonZeroMarks;
      }
   }
   return totalSumOfNonZeroMarks / numberOfNonZeroMarks;
}

Can you work out what that code does?

From the machine’s perspective these two pieces of code are pretty much the same. I expect that the compiler will perform the same optimizations on both pieces of code and two end results may well be identical. From a programmers perspective though, the two pieces of code are far from equal.

Neat little C++ trick

A colleague emailed me this neat little trick that made me snodchortle.

Look at this C++ code:


int a = 1;
int b = 2;

a ^= b;
b ^= a;
a ^= b;

What does this code do?

What are the values of a and b respectively at the end?

What are the benefits of this approach and what are the disadvantages?