I spent a few hours on credit.c yesterday, and finally got it to compile and pass check50 (yay!). I wanted to know if anyone could look over my code for optimization and tell me if there were things I did that were unnecessary (which I am expecting to be so) or things that I could've done easier.

I put my code in pastebin due to length and formatting.




1004000000000004 is recognised as VISA. I doubt it is.

4200000000000000 is recognised as MASTERCARD.

The code to me looks more or less random, tuned until it passes the tests, not created for matching the specification. The specification clearly states certain combinations of leading digits and length of the number are required. I don't see any mention of number length at all. Probably because you reset the number of digits to 0 on the way, so it's broken.

I did it differently: calculate the checksum, count the digits (without any checks at that point, just counting how often I have to divide by 10 to receive 0!), keep the first two digits in a separate variable. I then decide based on these three numbers (first checksum, then various combinations of the other two).

And yeah, you're doing things more complicated than necessary. For example, you wouldn't need any array if you processed the digits as you separate them (if you wanted, you could do it in a single loop). The expression (ccArray[ccArrayLength - 4] == 4 && ccArray[ccArrayLength - 4] + 4 == 8) was where I thought the code might be the result of a genetic algorithm, trying random combinations and variations to maximise the fitness function (smiling faces in check50)

  • Thanks Blauelf! I'll definitely work further on the code to fix those issues. I appreciate you looking over the code and I appreciate your input. – CampArawak Jun 26 '18 at 2:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .