Computer Science Canada turing - help w digits |
Author: | comp_help [ Mon Apr 13, 2009 8:16 pm ] | ||
Post subject: | turing - help w digits | ||
What is it you are trying to achieve? I am trying to output the number of digits entered in a number and the sum of those digits. What is the problem you are having? I don't know how to find the sum of the digits. Describe what you have tried to solve this problem I have solved the number of digits entered part of the problem, but it only works up to 6 digits. Post any relevant code (You may choose to attach the file instead of posting the code if it is too long) Code is below:
Please specify what version of Turing you are using Turing 4.1.1 |
Author: | Tony [ Mon Apr 13, 2009 8:31 pm ] | ||
Post subject: | RE:turing - help w digits | ||
Notice a pattern? |
Author: | andrew. [ Mon Apr 13, 2009 8:34 pm ] | ||
Post subject: | RE:turing - help w digits | ||
See how use added a 0 for each if statement? Having 10^n is the same. You can use a for loop to go through different values of n.
Something like that should work. If you change the 10 to maxint, you will have an overflow, so you need some kind of exiting condition so that there is no overflow. |
Author: | comp_help [ Mon Apr 13, 2009 9:18 pm ] |
Post subject: | RE:turing - help w digits |
I would also like to know how to find the sum of the digits. ![]() |
Author: | Tony [ Mon Apr 13, 2009 10:09 pm ] |
Post subject: | RE:turing - help w digits |
For what you need to get the individual digits first. Worry bout summing them later. if the input number is 123, how would you find the 2nd digit (in this case "2")? |
Author: | comp_help [ Mon Apr 13, 2009 10:39 pm ] |
Post subject: | RE:turing - help w digits |
I am guessing, but I think you would divide 123 by 10 then floor it. After that you can subtract ten. You can divide according to the number of digits. Can you tell me if this is right? |
Author: | Tony [ Mon Apr 13, 2009 10:50 pm ] |
Post subject: | RE:turing - help w digits |
As long as you know how to get ten in "subtract ten", then yes, this would work. |
Author: | andrew. [ Tue Apr 14, 2009 3:03 pm ] |
Post subject: | RE:turing - help w digits |
The cheap way out is to convert the int to a string and then just use length for the number of digits and use the substrings to find the digit. e.g. strNum (1) would be the last digit. This is the cheap way though and it's probably not what your teacher wants. I thought that you should know that you can convert ints to strings so that you can it in another assignment later. |
Author: | saltpro15 [ Tue Apr 14, 2009 3:59 pm ] | ||
Post subject: | RE:turing - help w digits | ||
I suggest looking into div and mod maybe this will work
click those |
Author: | saltpro15 [ Tue Apr 14, 2009 4:00 pm ] |
Post subject: | RE:turing - help w digits |
hmm, didn't work, well they'll be in the help index (F10) |
Author: | Tony [ Tue Apr 14, 2009 4:06 pm ] |
Post subject: | Re: turing - help w digits |
@saltpro it's [tdoc]div[/tdoc] |
Author: | saltpro15 [ Tue Apr 14, 2009 4:11 pm ] |
Post subject: | RE:turing - help w digits |
thanks Tony div mod |
Author: | comp_help [ Wed Apr 15, 2009 11:36 pm ] |
Post subject: | RE:turing - help w digits |
Ok, so for this code, in finding the sum of the digits, should I convert the input to string then get the part of the string according to the number of digits (ex. 2..3) and then convert it back to integer, and then finally add them? It is similar to what andrew was saying. |
Author: | Tony [ Thu Apr 16, 2009 12:33 am ] |
Post subject: | RE:turing - help w digits |
Except that taking a shortcut via strings pretty much defeats any purpose to this exercise. You really need to be comfortable with numbers. You had the right idea for getting the individual digits. Now you just need to add them together in a sum variable. |
Author: | andrew. [ Thu Apr 16, 2009 4:23 pm ] |
Post subject: | Re: RE:turing - help w digits |
comp_help @ Wed Apr 15, 2009 11:36 pm wrote: Ok, so for this code, in finding the sum of the digits, should I convert the input to string then get the part of the string according to the number of digits (ex. 2..3) and then convert it back to integer, and then finally add them?
I don't think you should do that. You're on the right track. I just told you how to do that because you will probably want to use something like that in the final ISU or something.It is similar to what andrew was saying. |