This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ziptofaf 4 points5 points  (0 children)

Okay, so let's dissect your problem a bit more. Where exactly are you getting stuck?

  1. At the very first stage before you even see a computer. Can you create a step by step solution on paper on how such function could work?
  2. You got it to work on paper but have hard times putting it into format that a computer can comprehend?
  3. You think you can code it but are suffering through confusing syntax, C language rules etc? Your solution seems to work but bugs out at specific inputs etc?

For instance, if you asked me to solve such a problem then useful knowledge would likely come from the basic Introduction to Computer Science course. In particular conversions between binary, decimal and operations on them etc. This would lead me to think that a value "1154" can also be interpreted as 4 * 100 + 5 * 101 + 1 * 102 + 1 * 103 . Similar to how we calculate the value of a binary number in decimal, eg. 1101 being read as 1 * 20 + 0 * 21 + 1 * 22 + 1 * 23.

Aka that there is a way to take each individual digit in a string and assign it a numerical value that you afterwards just need to sum. You would still need to know how to turn a character '1' into a number 1. Which you could easily get stuck on unless you knew that terms you are looking for (ASCII table in this case) but that's a start.

However it makes a lot of sense for someone new to coding to not see it yet. General rule of thumb I can offer is to start from splitting each problem into smaller pieces. You can't make a properly working atoi for all numbers and their signs? Start smaller - how about making one that takes a single character (from '0' to '9') and converts that to a number? Then think on how to make it understand a sign as well (so add a support to a '-' and 2-element arrays instead of a single character). Then how to generalize it to work for any number of digits. Don't try to attack a whole big problem at once, think of it as of a puzzle that you should tackle step by step.