
-----------------------------------
CtrlAltDelicious
Tue Jun 06, 2017 8:38 am

Find Digit(s) in an Integer
-----------------------------------
What is it you are trying to achieve?
I'm trying to find the digit in each numbers place in a nine digit integer (entered by the user). In this case, the 9 digit number is called a SIN number.

Describe what you have tried to solve this problem
I've determined how to find the length of the number successfully, but can't figure out how to find each individual digit.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
var name : string
var address : string
var Sin : string

% User Enters Information
put "Please Enter your full name: " ..
get name
put ""
put "Please Enter your Address: " ..
get address
put ""
put "Please Enter your 9 digit SIN #: " ..
get Sin

% Error Message if SIN is not 9 digits
if length (Sin) < 9 then
    put ""
    put "Error! This SIN number should be 9 digits long!"
elsif length (Sin) > 9 then
    put ""
    put "Error! Your SIN number should be 9 digits long!"
end if

Please specify what version of Turing you are using
Turing 4.1.1

Thanks!  :D

-----------------------------------
ProxyKaliber
Tue Jun 06, 2017 8:55 am

Re: Find Digit(s) in an Integer
-----------------------------------
One thing you could do is declare all three strings in one line so instead of:


var name : string 
var address : string 
var Sin : string 


you get:


var name, address, sin : string


if you'd like them to enter their full name with spaces you can put a ": *" after the "get name".

so instead of:


 % User Enters Information 
 put "Please Enter your full name: " .. 


so your code had:


get name 


you could do this:


get name : *


I personally would have put a loop so that they could renter their sin so the program won't end and they would have to restart from the beginning but I am under the assumption you have not learned loops as of right now. To find out which number is in each spot in the string you can use:


put sin(x) % let "x" represent the spot of the character you are looking to output.


you can also link your conditions together in one line with and/or. In this case you would use or because using and would mean both conditions would have to be true to execute the action. 

so if you have this:


 put "" 
 put "Please Enter your Address: " .. 
 get address 
 put "" 
 put "Please Enter your 9 digit SIN #: " .. 
 get Sin 

% Error Message if SIN is not 9 digits 
if length (Sin) < 9 then 
put "" 
put "Error! This SIN number should be 9 digits long!" 
elsif length (Sin) > 9 then 
put "" 
put "Error! Your SIN number should be 9 digits long!" 
end if 
put "" 
put "Please Enter your Address: " .. 
get address : *
put "" 


I would have done: 


loop
put "Please Enter your 9 digit SIN #: " .. 
get sin : * 
sinLength := length(sin)
% Error Message if SIN is not 9 digits 
if (sinLength < 9 or sinLength > 9) then 
put "" 
put "Error! This SIN number should be 9 digits long! Please re enter your sin number." 
else
put "" 
put "Sin number length valid." 
end if 
exit when (sinLength = 9)
end loop


but lets say the sin they entered was: 135790246


var sin : string

sin := "135790246"

% sin(x)=y

% x being the location in the string and y being the character at that location


put sin(1)
put sin(2)
put sin(3)
put sin(4)
put sin(5)
put sin(6)
put sin(7)
put sin(8)
put sin(9)


% sin(1)=1
% sin(2)=3
% sin(3)=5
% sin(4)=7
% sin(5)=9
% sin(6)=0
% sin(7)=2
% sin(8)=4
% sin(9)=6


I'm not to sure if this works with the int variable but I know it works for the string variable which you have given in your example of code.
I hope this helps clear things up.

-----------------------------------
Insectoid
Tue Jun 06, 2017 9:37 am

RE:Find Digit(s) in an Integer
-----------------------------------
You can use some very simple math to isolate digits in a number. Try to figure it out with a calculator before turning it into code.

-----------------------------------
ProxyKaliber
Wed Jun 07, 2017 9:40 am

Re: Find Digit(s) in an Integer
-----------------------------------

You can use some very simple math to isolate digits in a number. Try to figure it out with a calculator before turning it into code.   


Also know as an algorithm if I'm not mistaken...

-----------------------------------
CtrlAltDelicious
Fri Jun 16, 2017 7:50 am

Re: Find Digit(s) in an Integer
-----------------------------------
Thanks a lot, Kaliber!

And another thing: what if I wanted to check if the user entered something like 999999999, using the method above, could I see if all the digits are the same?

-----------------------------------
Insectoid
Fri Jun 16, 2017 3:06 pm

RE:Find Digit(s) in an Integer
-----------------------------------
Why don't you try it and see for yourself?

-----------------------------------
CtrlAltDelicious
Fri Jun 16, 2017 7:21 pm

Re: Find Digit(s) in an Integer
-----------------------------------
Good idea, I'll try it out  :wink:

-----------------------------------
CtrlAltDelicious
Tue Jun 20, 2017 8:12 am

Re: Find Digit(s) in an Integer
-----------------------------------
What if I wanted to now use these numbers as a number (integer)? I heard of something called a strint - could that be used here?

-----------------------------------
Insectoid
Tue Jun 20, 2017 1:10 pm

RE:Find Digit(s) in an Integer
-----------------------------------
You don't need my permission to try things. Just do it and see what happens. Regardless, you can do this without ever using strings in the first place. You can Input an integer (or real), separate the digits and use them as numbers, with no more than a bit of division and modulus. You don't need a single string.

-----------------------------------
Dragon20942
Fri Sep 22, 2017 9:45 pm

RE:Find Digit(s) in an Integer
-----------------------------------
Right. Part of intent that I see in this exercise is determining which type(s) to use when representing data, and how differences could change your strategies.

What Proxy mentioned with calling out individual characters in a string using an index is a good way to go about it as a string.
There are definitely also ways check for digits in an integer, as Insectoid mentioned.


And another thing: what if I wanted to check if the user entered something like 999999999, using the method above, could I see if all the digits are the same?


A useful way I like to approach problems sometimes is:
How would I do this as a human? And don't just answer this with "I just look at it, and if it's all the same, I know." Figure out HOW you figured out if they're all the same digit. Well, if you spot any differences between any 2 digits, then you know it's NOT all the same digit. But then this would mean comparing every digit to every other one (and it takes a lot of work, especially as the numbers get bigger), or maybe you could keep a tally of the first digit, and if all 8 digits after are the same as that one, then it's all the same digits. But do you NEED to go through all 8 digits? No, because the MOMENT you find a digit that's not the same as the first, then it's not all the same digit. Tackle the problem from a bunch of different angles, and figure out what works. Then, figure out a way to use that solution in your code.

There isn't a singular right way to approach coding, but at this stage, what is most important is that you research and try different strategies and understand how and why they worked/failed. It gives you a much better grasp about how to approach problems like this in the future, which are usually just small, small parts of other larger problems.

Edit: Necro post is necro. Sorry, I didn't notice this!
