A while back, my mom had mentioned that Dara, my oldest niece, who is about to turn 12 soon, had started a computer science class at school and was having some problems with C++ programming that no one in the family could help her with. So, when I was at home visiting them a few months ago, I asked Dara if she wanted me to see if I can help her with that, and she mentioned she had problems understanding arrays.
We went through what the teacher had told them and I did my best to explain them a bit better and give her some examples, which seemed to have helped. However, I suggested that we try to do one of her homework exercises together and asked her if she had any that we could look at. She told me about this website that is used by students all over Romania (http://campion.edu.ro/) which has programming exercises grouped by age/level and by the topic they cover, and that there are a few there that their teacher had suggested they try if they want to practice the use of arrays. So we chose the first one.
Here’s what it said:
Ana and Maria are playing a game with cards with 5 digit numbers written on them. For each card, Ana will get a number of points equal to the number formed by the first 2 digits on the card, Maria will get a number of points equal to the number formed by the last 2 digits on the card, and the one who has less points also gets the middle part added to their points. If the numbers formed by the first and last 2 digits are equal, then no one gets the middle part.
So, for example, if the card has the number 23146, Ana gets 23, Maria gets 46, and because 23 is smaller than 46, Ana gets 1 more point.
Their points are summed up after each round/card.
The task is to write a program that will take as a parameter a sum.in file that contains the number of cards on the first row and the actual numbers on the cards with spaces between on the second row, and produces a sum.out file that contains the total number of points for each of the girls after playing all the cards.
The exercise also had an example:
73256 14715 51251 32576
and some restrictions:
0 < number of cards < 10000
She opened her C++ Editor (some application I had never seen before that they were using in class) and when she started typing, I was amused by the big bright pink font she was using… but she started coding confidently, reading the sum.in file and storing her variables.
We stored the following:
n – number of cards
numbers[n] – the numbers on the cards
a – Ana’s sum
m – Maria’s sum
and we wrote functions for getting the first 2, the last 2 and the middle digit of a 5 digit number.
Then we talked about arrays again and I explained how we parse them and use them etc. so we wrote the last part of the code: we parsed the array and did something similar to this:
for each number in numbers[n]
a = a + first2digits(number)
m = m + last2digits(number)
if a < m
a = a + middledigit(number)
if m < a
m = m + middledigit(number)
We were done! So we tried the example provided in the exercise, with 4 cards.
It was wrong. We were not getting the correct sums for a and m.
Dara said: “Oh-oh… Now what?” and I was about to suggest the debugger and teach her how to use it, but the C++ editor they were using, though it obviously supported multiple colored fonts, did not include a debugger. We looked at the code again, but it gave us no clues.
I asked her what she would normally do in this case, if she were alone. She explained that this was one of the hardest parts: if something doesn’t work as expected when she’s done coding, she can check the code again, but doesn’t know what else to do if she doesn’t figure it out from the code, so she just submits her solution to the website and sees how many points she gets… usually not many in these cases.
I told her that there were a couple of things I thought we could do. We could try different numbers/cards and try to come up with simple examples that would give us clues as to where the problem would be, and we could add more printouts in the code at different steps to check the results.
We added the printouts, then I asked her if she had any suggestions of what numbers we should try that would be simple enough to give us some clues.
She said: “I know! Let’s try one card only!”
I said that was a very good idea, and we tried the first one in the example, 73256. The result was correct: a=73, m=58.
Dara said: “Let’s try another one!”. We tried a few, but the result was always correct. She kept wanting to try different numbers, but I explained that, judging by the results, one card worked just fine, so there was no need to try more similar examples. I was trying to get her to come up with a new example of what we could use, but she was stuck.
I suggested we try 2 cards this time, so she picked the first two in the example:
73256 and 14715
We got the wrong result: a = 87, m = 80, when we should have gotten a = 96, m = 71.
Dara said: “It must be because of 14715”, so we tried just that alone, but the result was correct. She said: “There’s something we’re not adding up the right way…” but then she didn’t know what else to try.
I suggested she thinks of other simple cases, and told her that she could choose her own numbers instead of picking them from the ones in the example, and choose ones that were simple enough that she could follow the math easily and maybe see where the mistake was. She asked: “Like what?” and I said: “Like 00000, or 11111, or those two together, or 11211, or 20120” and I explained why I suggested those values. I also pointed out that it would be important to also use numbers where the first 2 digits and the last 2 digits are the same, so we could cover that case as well.
She tried with 11211, with correct results: a = 11, m = 11. Then she tried with two cards with same number: 11211, 11211. The results were still correct. She tried all the simple options I had suggested, but still nothing was helping. She was again stuck and ready to give up.
I said I thought it was something related to the middle digit not being added correctly, so I said: “Let’s try something really simple, that covers both cases: one number has the 2 side digits equal, and one doesn’t. Let’s see how those add up.”
We chose 2 cards: 10220, 10210. Instead of a = 22, m=30, we got a = 24, m = 30.
Dara said: “We know the first round goes OK, so after the first card we have a = 12, m = 20. It’s as if our program doesn’t see that in the second case they’re equal…. But it works OK if we have just 1 card with the first 2 and last 2 digits equal, so the comparison is OK…”
We went back to look at the code.
The comparison was OK, but then… we both realized at the same time where the problem was: instead of comparing the digits first and making the decision based on just that, we were adding the digits to the totals of a and m first, then comparing them. So in our case, by the time we got to the second card, we had a = 12, m = 20, to which we added the first and last digits respectively, so we had a = 22, m = 30 and since a < m, a got the middle digit added.
We changed the code to correct this, and then the example produced correct results. We tried a few more cards, and everything was working OK.
Dara was eager to upload her program to the website and, as she was doing that, I tried to explain that she should not feel disappointed if she didn’t get maximum points yet, because there were still a few things that we had to do before the program was perfect – in my mind, I was already thinking about the restrictions mentioned in the exercise description, about edge cases, invalid input and what not, and trying to figure out the best way to explain all these to her.
It turned out I was the one to be disappointed: our program did get maximum points when uploaded to the website, even though it was far from perfect.
Dara was extremely happy, but I still had to mention that she wouldn’t get maximum points from me for that program. She seemed puzzled, so I asked her: What if sum.in is empty?” She said “Oh! Well.. then it doesn’t work…” and I continued “What about what happens if instead of the numbers you have letters? Or what if the number of the first row, which is supposed to be the number of cards, is bigger than the number of numbers you have on the second row? Or what if some of the numbers on the cards don’t have 5 digits?…”
I could have continued, but she stopped me by asking: “How do you come up with all of these?!”
I went on to explain that this is just part of what I do for work, that this is what I mean when I say I do software testing, to which she replied “Really?? That’s awesome!”.
I know!!! Right?
(Afterwards, when my mom came to see if we were done, Dara told her: “Now I know EXACTLY what Ru does for work!” My mom commented: “Great! You’re probably the first one in the family that really knows!”)