Recent

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

Pancho

  • Newbie
  • Posts: 2
[Help] Writing a specific digit obtained from an integer.
« 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) of a specific input (https://i.imgur.com/vs4Qu4s.png).
I would be really thankful if someone would help me.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: [Help] Writing a specific digit obtained from an integer.
« Reply #1 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.

Pancho

  • Newbie
  • Posts: 2
Re: [Help] Writing a specific digit obtained from an integer.
« Reply #2 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.

bigeno

  • Sr. Member
  • ****
  • Posts: 266
Re: [Help] Writing a specific digit obtained from an integer.
« Reply #3 on: March 14, 2019, 02:17:22 pm »
Code: Pascal  [Select][+][-]
  1.   s:=IntToStr(56009);
  2.   ShowMessage('string='+s[Length(s)]);  

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: [Help] Writing a specific digit obtained from an integer.
« Reply #4 on: March 14, 2019, 02:23:05 pm »
writeln(i mod 10);

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: [Help] Writing a specific digit obtained from an integer.
« Reply #5 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.

wadman

  • New Member
  • *
  • Posts: 37
    • wadman's home
Re: [Help] Writing a specific digit obtained from an integer.
« Reply #6 on: March 15, 2019, 07:12:44 am »
RightStr(IntToStr(76576), 1);

Zvoni

  • Hero Member
  • *****
  • Posts: 2316
Re: [Help] Writing a specific digit obtained from an integer.
« Reply #7 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.  
« Last Edit: March 15, 2019, 10:05:04 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: [Help] Writing a specific digit obtained from an integer.
« Reply #8 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... >:(
Specialize a type, not a var.

Zvoni

  • Hero Member
  • *****
  • Posts: 2316
Re: [Help] Writing a specific digit obtained from an integer.
« Reply #9 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.
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: [Help] Writing a specific digit obtained from an integer.
« Reply #10 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

six1

  • Full Member
  • ***
  • Posts: 117
Re: [Help] Writing a specific digit obtained from an integer.
« Reply #11 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

Zvoni

  • Hero Member
  • *****
  • Posts: 2316
Re: [Help] Writing a specific digit obtained from an integer.
« Reply #12 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
« Last Edit: March 15, 2019, 12:50:33 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

six1

  • Full Member
  • ***
  • Posts: 117
Re: [Help] Writing a specific digit obtained from an integer.
« Reply #13 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"

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: [Help] Writing a specific digit obtained from an integer.
« Reply #14 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.
« Last Edit: March 15, 2019, 07:19:51 pm by jwdietrich »
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

 

TinyPortal © 2005-2018