### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: [Help] Writing a specific digit obtained from an integer.  (Read 3302 times)

#### Zvoni

• Sr. Member
• Posts: 345
##### Re: [Help] Writing a specific digit obtained from an integer.
« Reply #15 on: March 15, 2019, 01:58:13 pm »
JW,

i know that, we know that!
The modulo way is the fastest option. No Discussion!

The thing is: The Assignment never specified "...in as fast a way as possible...."

What's making me flying off the handle is, never seeing the most obvious way (and that is de facto the String-Operation, because we do it every day in real life), and then ask "Is there a better way?"
The OP is obviously a beginner, and from the way it's phrased he doesn't have an ounce of experience with programming (e.g in another Language C, VB, take your pick), because then he would have known, that there is a "string"-way, and a "math"-way, and he'd just ask "I know how to do it in C++, but how do i do it in Pascal?".

For me, Thaddy's advice is like going for your driving license, and the instructor tells you how Lewis Hamilton is determining breaking points and the apex of a corner for him to know when to hit the gaspedal again.

You should learn to walk first, before running.
« Last Edit: March 15, 2019, 02:01:19 pm by Zvoni »
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircrafts

#### marcov

• Global Moderator
• Hero Member
• Posts: 8107
##### Re: [Help] Writing a specific digit obtained from an integer.
« Reply #16 on: March 15, 2019, 04:02:28 pm »
Hands up, who in such a case uses the Modulo-way, and who uses the "String-Operations"-way....
If i do online-banking, and the bank asks me for the last four digits of my credit-card-number, i don't do math. I do String-Operations, "writeln(Right(CreditCardNumber.ToString, 4));"

(don't take all this to serious, but I do recommend the OP to consider both ways)

That step to modeling as string operations is just as much as an IT specific indoctrination as the math approach is.

Both are about isolating digits based on position. One uses the fact that a position more to the left means 10 times the value, and one simply counts positions.

In the past this was considered Basic diseases, i.e. too much exposure to Basic and/or scripting languages.

Quote
This is homework-assignment, and he doesn't even know what he'd be doing in real-life to solve the problem.

Homework is about learning. And, if you think about it, the string way involves as many divisions as there are digits, and the math way exactly one.

#### Zvoni

• Sr. Member
• Posts: 345
##### Re: [Help] Writing a specific digit obtained from an integer.
« Reply #17 on: March 15, 2019, 05:00:57 pm »
Hands up, who in such a case uses the Modulo-way, and who uses the "String-Operations"-way....
If i do online-banking, and the bank asks me for the last four digits of my credit-card-number, i don't do math. I do String-Operations, "writeln(Right(CreditCardNumber.ToString, 4));"

(don't take all this to serious, but I do recommend the OP to consider both ways)

That step to modeling as string operations is just as much as an IT specific indoctrination as the math approach is.

Both are about isolating digits based on position. One uses the fact that a position more to the left means 10 times the value, and one simply counts positions.

In the past this was considered Basic diseases, i.e. too much exposure to Basic and/or scripting languages.

Quote
This is homework-assignment, and he doesn't even know what he'd be doing in real-life to solve the problem.

Homework is about learning. And, if you think about it, the string way involves as many divisions as there are digits, and the math way exactly one.

All true, and i agree with you but there are some things in life i have my own view on, and it starts with recognizing a situation you have in real-life and applying that to the specific Problem.
Let's agree to disagree
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircrafts

#### ASBzone

• Sr. Member
• Posts: 327
##### Re: [Help] Writing a specific digit obtained from an integer.
« Reply #18 on: March 15, 2019, 06:17:13 pm »
...but you're forgetting the main purpose of Computer-Programming:
Replicating Real-Life-Processes to be calculated by a Computer.

By whose definition is this the main purpose of computer programming?

Is it not instead, the solving of processes too tedious or difficult for humans?

I program and use programs to achieve results that are either too difficult to achieve alone, too time-consuming, would otherwise be too error prone, etc.

I don't program or use programs to achieve results "just as a human would do it."     And I know of no-one with such a goal.

So, I'm not sure what sample size you used for that alleged truism.

My 2€-cents.

Ah, so an opinion.   Got it.
« Last Edit: March 15, 2019, 06:20:36 pm by ASBzone »
-ASB: https://www.BrainWaveCC.com

Lazarus v2.0.7 r62681 / FPC v3.2.0-beta-r44391 (via FpcUpDeluxe) -- Windows 64-bit install w/32-bit cross-compile
Primary System: Windows 10 Pro x64, Version 1909 (Build 18363.720)
Other Systems: Windows 10 Pro x64, Version 1909 or greater

#### jamie

• Hero Member
• Posts: 2513
##### Re: [Help] Writing a specific digit obtained from an integer.
« Reply #19 on: March 15, 2019, 09:48:01 pm »
or maybe the teacher is looking for a long hand version of the MOD function

MyLetter := Char(\$30+(InputNumber-(10 *(inputNumber DIV 10)));

Something along those lines...
Number 1 at blue screen app creations!

#### Bart

• Hero Member
• Posts: 3728
##### Re: [Help] Writing a specific digit obtained from an integer.
« Reply #20 on: March 15, 2019, 10:56:15 pm »
The "last one standing" approach:

Code: Pascal  [Select]
1. function LastDigit(L: Integer): Integer;
2. var
3.   S: String;
4. begin
5.   S := IntToSTr(L);
6.   while (Length(S) > 1) do Delete(S,1,1);
7.   Result := StrToInt(S);
8. end;

Bart

#### Zvoni

• Sr. Member
• Posts: 345
##### Re: [Help] Writing a specific digit obtained from an integer.
« Reply #21 on: March 16, 2019, 05:18:53 am »
Bart,

nah, i like this one better
Code: Pascal  [Select]
1. Program Project1;
2. Uses
3. Sysutils, strutils;
4. Var
5.   MyNumber:Integer;
6. Begin
7.   MyNumber:=123456;
8.   Writeln((StrEnd(PChar(MyNumber.ToString))-SizeOf(Char))^);  //Writes "6"
9. End.
10.
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircrafts

#### Kays

• Full Member
• Posts: 182
• Whasup!?
##### Re: [Help] Writing a specific digit obtained from an integer.
« Reply #22 on: March 16, 2019, 02:20:32 pm »
writeln(i mod 10);
Code: Pascal  [Select]
1. writeLn(abs(i) mod 10));
otherwise a negative sign may propagate, but I assume everyone already knew this (although nobody's mentioned it). [Also note, the ISO mode's mod operation works differently, especially if the first operand's negative.]
« Last Edit: March 16, 2019, 03:02:35 pm by Kays »
Yours Sincerely
Kai Burghardt

#### Bart

• Hero Member
• Posts: 3728
##### Re: [Help] Writing a specific digit obtained from an integer.
« Reply #23 on: March 16, 2019, 05:26:09 pm »
Code: Pascal  [Select]
1.   ...
2.   writeln(ReverseString(MyNumber.ToString)[1]);
3.   ...

You definitely need The Slowest Pascal ReverseString competition then.

Bart