Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 turing - help w digits
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
comp_help




PostPosted: 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:

Turing:


var nm: int

put "Enter a number (integer):"
get nm

if nm div 100000 >= 1 and nm div 100000 < 2 then
put "\nEntered number has 6 digits"

elsif nm div 10000 >= 1 and nm div 10000 < 2 then
put "\nEntered number has 5 digits"

elsif nm div 1000 >= 1 and nm div 1000 < 2 then
put "\nEntered number has 4 digits"

elsif nm div 100 >= 1 and nm div 100 < 2 then
put "\nEntered number has 3 digits"

elsif nm div 10 >= 1 and nm div 10 < 2 then
put "\nEntered number has 2 digits"

elsif nm div 1 >= 0 and nm div 1 < 2 then
put "\nEntered number has 1 digits"

else
put "\nError: Number is greater than 6 digits."

end if



Please specify what version of Turing you are using
Turing 4.1.1
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Mon Apr 13, 2009 8:31 pm   Post subject: RE:turing - help w digits

code:

div 10000
div 1000
div 100
div 10
div 1

Notice a pattern?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
andrew.




PostPosted: 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.

Turing:

for n : 1..10 % The 10 is how many digits max you want
exit when nm div (10**n) >= 1 and nm div (10**n) < 2 % Remember to exit so that the program doesn't keep looping
if nm div 10**n >= 1 and nm div 10**n < 9 then
put "\nEntered number has  "+ intstr(n+1) + " digits."
end if
end for


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.
comp_help




PostPosted: 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. Confused
Tony




PostPosted: 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")?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
comp_help




PostPosted: 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?
Tony




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
andrew.




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
saltpro15




PostPosted: 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
Turing:

div
mod

click those
saltpro15




PostPosted: 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)
Tony




PostPosted: Tue Apr 14, 2009 4:06 pm   Post subject: Re: turing - help w digits

@saltpro

it's [tdoc]div[/tdoc]
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
saltpro15




PostPosted: Tue Apr 14, 2009 4:11 pm   Post subject: RE:turing - help w digits

thanks Tony

div


mod
comp_help




PostPosted: 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.
Tony




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
andrew.




PostPosted: 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?
It is similar to what andrew was saying.
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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 15 Posts ]
Jump to:   


Style:  
Search: