How would you react?
This week I've been put to the test and had to solve a small code situation where I saw that my C skills were nowhere good at all.
Not that I don't know how to code but rather because my mind seems grounded to the delphi roots and refuses to use anything else (perhaps php being the exception)
Enough jiberish, let's cut to the chase and propose the challenge:
"Code a program where the user selects a positive integer number and your program outputs all the possible combinations of three numbers that summed up result in the number selected by the user. No number can be repeated after used once in any given combination"
For example, the user selects 12, and the program should output:
12 = 1 + 2 + 9
12 = 3 + 4 + 5
This sounded like a fun task (except that it needed to be done in C but I do need the practise).
I spent the first two days of the week thinking about the possible approaches to solves this challenge programatically in mind or ocasionally jolting down something onto paper.
Before you ask - no, I didn't had a compiler. Never had coded anything before on Linux and now I was in need for a good IDE where I could code and just press the compile button to see the results. Eventually found myself enjoying code::blocks as it was as simple as an IDE can get.
To my dispair, I thought on many possible solutions but being stuborn as I am, none was simple and direct enough to please my taste for a clean solution.
I wanted to make the smallest possible code that would output what was asked and all my solutions seemed bloated and self-confusing.
Last night, I was working for 36 hours without breaks and decided to dedicate these boring work hours to solve this seamingly tough dilemma - who knows if I stare to code long enough some answer will come up? (does this ever work?)
And so I went to code.
Took four hours to try out the first ideas - used arrays, ini files, pointer and multiple loops and none worked.
Last, tried the route that I've should have taken from the start: Manually simulate the behavior of this program by testing some number and checking how it should iterate through the number sequences.
I tried 70 as the test number and used excel spreadsheet (or better yet, the openoffice version) and numbered a column from 1 to 70.
Next, I started to create sequences and removing the numbers from column as they were placed on the combination and behold at my results:
70 = 1 + 2 + 67 --> OK
70 = 3 + 4 + 63 --> OK
70 = 5 + 6 + 59 --> OK
70 = 7 + 8 + 55 --> OK
70 = 9 + 10 + 51 --> OK
70 = 11 + 12 + 47 --> OK
70 = 13 + 14 + 43 --> OK
70 = 15 + 16 + 39 --> OK
70 = 17 + 18 + 35 --> OK
70 = 19 + 20 + 31 --> OK
70 = 21 + 22 + 27 --> OK
70 = 23 + 24 + 23 --> WRONG!
It turns out that looking on the possible combinations for 70 it is so plain easy to understand exactly how a code function can replicate the same results, doesn't it?
My code ended up like this:
printf("Type a number bigger than 5 to showcase the possible sums with three numbers\n-> ");
if (max < a="1," b="2," c="max-3;" a="a+2," b="a+1," c="max-(a+b))" d="%d" max=""><=11)printf("\n\n Only one combination is possible for %d\n",max); else printf("\n\n %d combinations possible for %d\n",a/2,max);}
So the relevant code here is
for (a=1, b=2, c=max-3; b
printf("\n%d = %d + %d + %d",max,a,b,c);
It's truly amazing what a FOR statement can do in C language and how it can produce all the required combinations for our goal.
In conclusion, from this challenge I can say that I've learnt at least two things.
- C is growing outdated but features like these on FOR make it a tool worth digging some more attention
- Next time I have a code task like this one, I'll certainly remember beginning with the manual simulation of the expected results.
What about you?
Do you feel confident to produce an even smaller or more compact solution for this challenge in C?
You're invited to post your code suggestions!