Pascal - Coursework help

nffc10

Active Member
I won't go into too much detail about what i have to do. All i really need to know is how to extract part of a number.

For instance if i have the number 5, it is one digit and easy to register.
If i have, however, 129. There are 3 digits and i want to register each one as a separate number.
E.g. Number1 = 1, Number2 = 2, Number3 = 9.

Is there any way i can get the program to read the number like that?

If you need any more info just ask.;)

Thanks,
Liam
 
well I cant think of a simple way of doing it. If you know the max number of digits you could divide by all the 10 bases units then round down and store each one separate var eg
(this is in a kind of pseudocode, so you'll have to convert to pascal as i havent used it in over 8 years)

assume max length is 3 digit so its in the hundred
assume input_num is the source (in this case well take the number 543)


Hundreds = round((Input_num /100) - 0.5) // well 543/100 = 5.43, then less 0.5 = 4.93, which rounds to equal 5...its important to less 0.5 to ensure that the number always rounds down)

Tens = round (((input_num - (Hundreds*100))/10 ) -0.5) //so 543 less 500 (5*100) = 43, divided by 10 = 4.3, less 0.5 = 3.8...which rounds to 4

Singles = Input_num - ((hundred*100) + (tens * 10)) //will give you the remainder

So now you have the
Hundreds = 5
Tens = 4
Singles = 3
done :)



here is the syntax for the round function
http://www.mirrorservice.org/sites/www.gnu-pascal.de/gpc/Round.html
 
Quality, you have no idea how much this is going to help me. It'll probably save me literally around a couple of hundred lines of code.;)

I'm going to check it out now, but thanks again for taking the time to write all that down for me. :)
 
It works perfectly mate thanks alot, I'm absolutely over the moon with how well it works. :D

Thanks Again,

Liam.;)
 
Back
Top