![]() ![]() Basically anytime you have code that repeats it would be better to put it into a loop or a function where the code is reused. ![]() One of the earliest principles all programmers learn is Don't Repeat Yourself, usually shortened to DRY code. While number is longer, it might be better if it was credit_card_number. It isn't really clear in the program what i or t represents. A single character really doesn't tell anyone reading or modifying the code what the variable really is or does. Having single character variable names is generally frowned upon except for loop control values. Having variables i1 through i16 is a very good indication that i should be an array, this is also true of t. For a single character this would always give you a value between zero and nine. To get the actual numeric value of a character you would subtract '0' from the numeric character. It would also allow the program to detect if any non-numeric characters were entered. This would remove all the division in the program to get each character. In the C programming language a string is a null terminated array of type char or character. It might be better to treat the credit card number as a string. The code currently outputs the correct results below. calculate sum of variable digits if > 9 get the individual intergers of number I feel like my solution is very clunky although it does the job! #include The problem I have solved below is for week 1 (credit).Īny suggestions on how to improve this code to further my learning would be most appreciated. I am relatively new to C programming and I am currently working through the CS50 EDX course. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |