Lazarus

Programming => General => Topic started by: Pancho on March 14, 2019, 01:42:37 pm

Title: [Help] Writing a specific digit obtained from an integer.
Post by: Pancho on March 14, 2019, 01:42:37 pm
Hi, so, I started learning the Pascal language but stumbled upon a problem that asked me to write the last digit (https://i.imgur.com/Tt9nyIH.png (https://i.imgur.com/Tt9nyIH.png)) of a specific input (https://i.imgur.com/vs4Qu4s.png (https://i.imgur.com/vs4Qu4s.png)).
I would be really thankful if someone would help me.
Title: Re: [Help] Writing a specific digit obtained from an integer.
Post by: marcov on March 14, 2019, 01:47:16 pm
What do you have so far to test with? A few vague images on imgur is not really a problem description.
Title: Re: [Help] Writing a specific digit obtained from an integer.
Post by: Pancho on March 14, 2019, 02:05:34 pm
What do you have so far to test with? A few vague images on imgur is not really a problem description.
Write a program in PASCAL that shows on the screen the last digit of a whole number.
Title: Re: [Help] Writing a specific digit obtained from an integer.
Post by: bigeno on March 14, 2019, 02:17:22 pm
Code: Pascal  [Select]
  1.   s:=IntToStr(56009);
  2.   ShowMessage('string='+s[Length(s)]);  
Title: Re: [Help] Writing a specific digit obtained from an integer.
Post by: marcov on March 14, 2019, 02:23:05 pm
writeln(i mod 10);
Title: Re: [Help] Writing a specific digit obtained from an integer.
Post by: valdir.marcos on March 14, 2019, 03:23:39 pm
What do you have so far to test with? A few vague images on imgur is not really a problem description.
Write a program in PASCAL that shows on the screen the last digit of a whole number.
Show us the code you have written so far.
Please type your code inside de option "code" and select "Pascal".

When you post a message, use the "Attachments and other options" link so you can attach images to your comment, just like I did now.
Title: Re: [Help] Writing a specific digit obtained from an integer.
Post by: wadman on March 15, 2019, 07:12:44 am
RightStr(IntToStr(76576), 1);
Title: Re: [Help] Writing a specific digit obtained from an integer.
Post by: Zvoni on March 15, 2019, 10:01:05 am
Well, if we're going "All-In", let me too get in on the Fun

Code: Pascal  [Select]
  1. program Project1;
  2. Uses
  3. sysutils,strutils;
  4. Var
  5.   MyNumber:Integer;
  6. begin
  7.   MyNumber:=123456;
  8.   writeln(ReverseString(MyNumber.ToString)[1]);
  9.  
  10. end.                
  11.  
Title: Re: [Help] Writing a specific digit obtained from an integer.
Post by: Thaddy on March 15, 2019, 10:18:47 am
Why is everybody except Marco doing string operations on an integer?
Never knew we have modulo?
Never knew we have shift?

The question asked is rather basic, please advice the correct way....
Dead giveaway: do not convert to string.... until you have the desired result... >:(
Title: Re: [Help] Writing a specific digit obtained from an integer.
Post by: Zvoni on March 15, 2019, 12:06:03 pm
Because it has to do with algorithms and nothing with the chosen Language (in our case Pascal).
Yes, the Modulo is the fine and elegant way, but you're forgetting the main purpose of Computer-Programming:
Replicating Real-Life-Processes to be calculated by a Computer.

In this case, many forget, how they would do it in real life without (!!) a computer.
They would have a sheet of paper, where they've written down: 123456
Now you have a Number on a sheet of paper.
How would you do it to, to get the last digit?
Correct, you'd read from right to left, or reverse it and read from left to right.
That's an algorithm we use subconciously everyday, and we don't even know it
To use the Modulo-way implies mathematical knowledge, which doesn't exist nowadays anymore.
(i'm always baffled when my 12 year old niece uses her mobile phone-app "calculator" to calculate "4x7-3").

Doing it with String-operations is valid, because it represents real-life-processing in the human brain

My 2€-cents.
Title: Re: [Help] Writing a specific digit obtained from an integer.
Post by: marcov on March 15, 2019, 12:21:45 pm
That's an algorithm we use subconciously everyday, and we don't even know it

Strange reasoning. So only algorithms in every day practice are allowed in programming?

I wonder what would happen if you apply that to physics, how do you determine the number of atoms in a gram of matter? You count them one by one, because that is what you do in every day practice!  :D
Title: Re: [Help] Writing a specific digit obtained from an integer.
Post by: six1 on March 15, 2019, 12:38:40 pm
 yes marcov ...and additionally we can use the smartphone app to add it when it's getting more and more huge?  :D
Title: Re: [Help] Writing a specific digit obtained from an integer.
Post by: Zvoni on March 15, 2019, 12:47:05 pm
That's an algorithm we use subconciously everyday, and we don't even know it

Strange reasoning. So only algorithms in every day practice are allowed in programming?

I wonder what would happen if you apply that to physics, how do you determine the number of atoms in a gram of matter? You count them one by one, because that is what you do in every day practice!  :D

OK, maybe i described it in the wrong way.
Of course it's not only the algorithms in every day practice.
I was referring to the problem at hand! Quote: "In THIS case, many forget...."
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));"

And in every programming-problem, which has user-interaction (and printing something on a screen is User-Interaction), is the first step to ask: How would i do it without a computer?

This is homework-assignment, and he doesn't even know what he'd be doing in real-life to solve the problem.
Define the problem in real-life, translate it to computer-programming, and in such cases you end up with String-Operations in 99% of cases.

My Opinion
Title: Re: [Help] Writing a specific digit obtained from an integer.
Post by: six1 on March 15, 2019, 12:50:54 pm
in my opinion, it has only something to do with it, whether you work a lot with modulo and ultimately how well you "speak a language"
Title: Re: [Help] Writing a specific digit obtained from an integer.
Post by: jwdietrich on March 15, 2019, 01:45:59 pm
Beyond these (very interesting) philosophical contemplations some empirical data may be helpful:

Checking the two options to get the last digit with both methods in a loop of 100.000.000 iterations on a 64 bit machine with Intel Core i7 @ 3.4 GHz needs 18 seconds with the string method and 1 second with the modulo option.

Code: Pascal  [Select]
  1. procedure TForm1.StringButtonClick(Sender: TObject);
  2. var
  3.   i: longint;
  4.   lastDigit: char;
  5.   inputString: string;
  6.   startTime, stopTime: TDateTime;
  7. begin
  8.   LastDigitEdit.Text := '';
  9.   TimeEdit.Text := '';
  10.   startTime := now;
  11.   for i := 0 to IterationsSpinEdit.Value do
  12.   begin
  13.     inputString := IntToStr(TestNumberSpinEdit.Value);
  14.     lastDigit := inputString[length(inputString)];
  15.   end;
  16.   LastDigitEdit.Text := lastDigit;
  17.   stopTime := now;
  18.   TimeEdit.Text := TimeToStr(stopTime - startTime);
  19. end;
  20.  
  21. procedure TForm1.ModuloButtonClick(Sender: TObject);
  22. var
  23.   i: longint;
  24.   lastDigit: integer;
  25.   startTime, stopTime: TDateTime;
  26. begin
  27.   LastDigitEdit.Text := '';
  28.   TimeEdit.Text := '';
  29.   startTime := now;
  30.   for i := 0 to IterationsSpinEdit.Value do
  31.   begin
  32.     lastDigit := TestNumberSpinEdit.Value mod 10;
  33.   end;
  34.   LastDigitEdit.Text := IntToStr(lastDigit);
  35.   stopTime := now;
  36.   TimeEdit.Text := TimeToStr(stopTime - startTime);
  37. end;

See a small demo application in the attachment.

This may at least be pertinent in situations where a large number of digits is to be extracted.
Title: Re: [Help] Writing a specific digit obtained from an integer.
Post by: Zvoni 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 same for answers like Thaddy's:
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.
Title: Re: [Help] Writing a specific digit obtained from an integer.
Post by: marcov 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.
Title: Re: [Help] Writing a specific digit obtained from an integer.
Post by: Zvoni 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  :D
Title: Re: [Help] Writing a specific digit obtained from an integer.
Post by: ASBzone 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.
Title: Re: [Help] Writing a specific digit obtained from an integer.
Post by: jamie 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...
Title: Re: [Help] Writing a specific digit obtained from an integer.
Post by: Bart 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
Title: Re: [Help] Writing a specific digit obtained from an integer.
Post by: Zvoni on March 16, 2019, 05:18:53 am
Bart,

nah, i like this one better  >:( >:( :o :o :o :D :D :D :D :D 8-) 8-) 8-) 8-) 8-) 8-) :P :P :P :P :P
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.  
Title: Re: [Help] Writing a specific digit obtained from an integer.
Post by: Kays 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.]
Title: Re: [Help] Writing a specific digit obtained from an integer.
Post by: Bart 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 (https://www.flyingsheep.nl/reversestring.htm) then.

Bart