# Lazarus

## Free Pascal => Beginners => Topic started by: adpetrov on February 07, 2017, 07:18:56 pm

Title: [Help] Decimal into binary system
Post by: adpetrov on February 07, 2017, 07:18:56 pm

I need to write code for this task:

Write a program which converts an integer from decimal into binary system, and writes it in direct, one's complement and two's complement notations. Modify the program to perform summation.

Title: Re: [Help] Decimal into binary system
Post by: Bart on February 07, 2017, 07:21:17 pm
And what have you got so far?

Bart
Title: Re: [Help] Decimal into binary system
Post by: adpetrov on February 07, 2017, 07:38:28 pm
My friend need to do this. I'm not a programmer. I didn't know where to turn.
Title: Re: [Help] Decimal into binary system
Post by: Bart on February 08, 2017, 12:34:14 am
Yeah, right.
Best one so far.

Bart
Title: Re: [Help] Decimal into binary system
Post by: Lupp on February 08, 2017, 12:59:35 am
There is a function IntToBin in the unit StrUtils that can give you the exact bit pattern of the memory representation of a number of an integer type as a string. The rest is arithmetics and string manipulation as usual.
Title: Re: [Help] Decimal into binary system
Post by: Bart on February 08, 2017, 12:52:52 pm
There is a function IntToBin in the unit StrUtils that can give you ...

No, it can give his friend the exact bitpattern ....  >:D

Bart
Title: Re: [Help] Decimal into binary system
Post by: Handoko on February 08, 2017, 12:58:20 pm
https://en.wikipedia.org/wiki/Ones%27_complement
https://en.wikipedia.org/wiki/Two%27s_complement
Title: Re: [Help] Decimal into binary system
Post by: Lupp on February 08, 2017, 01:07:32 pm
And ...
Attached an example for anybody and their friends.

#27 means "The key read as ASCII control character number 27." That's the 'Esc' key.
Title: Re: [Help] Decimal into binary system
Post by: Handoko on February 08, 2017, 01:11:36 pm
Unit Crt can't run properly on my machine (don't know why). So I copy/pasted Lupp's code here for others to try and inspect.

Code: Pascal  [Select][+][-]
1. program Dec2Bin;
2. uses
3.   Crt, StrUtils;
4. type
5.   MyType = Longint; //e.g.
6. var
7.   MyNumber : MyType;
8.
9. var
10.   fNum: Double;
11.   BytesOf: array[1..8] of Byte absolute fNum;
12.   BinStr: String;
13.   j: Integer;
14. begin
15.   ClrScr;
16.   Writeln('First some integers (may be Longint).');
17.   repeat
19.     WriteLn('That''s in binary: ':30, IntToBin(MyNumber, SizeOf(MyType)*8));
20.     WriteLn('And the direct complement: ':30, IntToBin(NOT MyNumber, SizeOf(MyType)*8));
21.     WriteLn('Or whatever you want: ':30, IntToBin(- MyNumber, SizeOf(MyType)*8));
23.
24.   Writeln('And now numbers with a decimal part and probably in scientific notation.');
25.   WriteLn('No complements in this case.');
26.   ClrScr;
27.   repeat
29.     BinStr := '';
30.     for j := 8 downto 1 do BinStr := BinStr + IntToBin(BytesOf[j],8);
31.     WriteLn('That''s in binary standard 64-bit Double format: ');
32.     WriteLn(BinStr);
33.
35.
36. end.
Title: Re: [Help] Decimal into binary system
Post by: Bart on February 08, 2017, 01:56:42 pm
OK, now without any effort at all for TS we solved his friends homework assignment.

Personally I object to that.

Bart
Title: Re: [Help] Decimal into binary system
Post by: wp on February 08, 2017, 02:13:58 pm
Well I think application of a ready-made function certainly has not been in the intention of the teacher who gave this assignment. The OP will have a problem showing the posted solution anyway.
Title: Re: [Help] Decimal into binary system
Post by: Leledumbo on February 08, 2017, 02:18:50 pm
OK, now without any effort at all for TS we solved his friends homework assignment.

Personally I object to that.

Bart
Relax, the teacher will reject the use of such built-in function. Plus, IntToBin only gives answer in one's complement. He will still need hammer his head to get the two's complement (despite it's very trivial).
Title: Re: [Help] Decimal into binary system
Post by: Handoko on February 08, 2017, 02:32:23 pm
It is sad 'his friend' cannot visit here, otherwise we can have better communication and guide him/her better. Novices often learn nothing if receiving working code directly for assignment.
Title: Re: [Help] Decimal into binary system
Post by: Bart on February 08, 2017, 03:37:27 pm
Relax, the teacher will reject the use of such built-in function.

His friend only has to copy the implementation of said function (for 1 implmentation).
Of course this implementation may still be way above the skills he is expected to have, so his friends teacher might still reject it.

I still think however that we should help people who ask for this, but (especially for homework assignments) we should not give away code, but give hints and tips (and possibly minor corrections) on the code they can show us.

It's a matter of principle.

Bart
Title: Re: [Help] Decimal into binary system
Post by: Handoko on February 08, 2017, 03:42:18 pm
I still think however that we should help people who ask for this, but (especially for homework assignments) we should not give away code, but give hints and tips (and possibly minor corrections) on the code they can show us.

Very agree. I give hints, rarely codes.
Title: Re: [Help] Decimal into binary system
Post by: wp on February 08, 2017, 03:42:47 pm
I fully agree, Bart. And I don't like the idea of accepting donations which comes up from time to time in this context.
Title: Re: [Help] Decimal into binary system
Post by: Leledumbo on February 08, 2017, 07:38:51 pm
His friend only has to copy the implementation of said function (for 1 implmentation).
Of course this implementation may still be way above the skills he is expected to have, so his friends teacher might still reject it.
Yep, as expected, and that's only if the friend can find where the source code for that function is ;)
I still think however that we should help people who ask for this, but (especially for homework assignments) we should not give away code, but give hints and tips (and possibly minor corrections) on the code they can show us.
Indeed, that's what I always do, but only and only if, there's an existing visible and responsible effort (not necessarily code, could be idea or something else, but has to be done by OP him/herself and not a copy from somewhere, easily confirmed by asking the OP to explain it) so at least I can start thinking what kind of hints I should give. I think we should help those who are willing to learn, but not those who are trying to escape their responsibility (yes, there are people with this mindset, and I've met many of them).
Title: Re: [Help] Decimal into binary system
Post by: Lupp on February 08, 2017, 11:10:31 pm
Ok, I was the one to post the code. (I didn't give away anything.) I also am a retrired teacher. And I know that there arose new problems concerning homeworks and many other things from the technical innovations of the last few decades some of which we are using (and feeding) here. These are problems teachers (and the "educational system") have to consider and to solve. Nobody here should feel urged to do it for them. Let's demonstrate good style with every respect. That's the best we can do to act out our pedagogic impetus.

A few comments on the example now:
The two's complement is a nontrivial concept. Someone who doesn't feel competent at least on a level to be able to start with a task as posted, will also not be able to understand and to apply the concept except in a purely mechanical way.
Isn't it the teacher's role to prepare the basis and to answer questions?

The first question concerning the original post I had in mind was: What is a 'decimal number' in this context? Please tell me if you think this is wrong: "It must be a string of decimal digits". (Can we be sure there isn't a sign and/or a decimal point / a scaling factor in the E notation?) Most likely we have an "unsigned integer", first variant,  as defined under 1.6 in the reference guide.

If so:  Can anybody here help by hints to find a way to write a Pascal routine for the conversion? Can we do it ourselves from scratch extemporizing?
Why should we? We will use the built-in function StrToInt. (Funny Name!) And if we are very clever we will in addition check if the "decimal number" was actually decimal or probably ... (there are 4 variants).

Next: "How should the number look after it was converted into the "binary system". Should it be dyadic written in "0" and "1"? Why not Gray code which also is binary?

...

Can we be sure that the task was thoroughly prepared and clearly put by the teacher?
Can we be sure that just the student was too lazy to work on it?
What are the reasons making such a task an appropriate homewirk for a beginner's programming class?

Everything may be completeky differnt, of course, from what I supposed. No factual information so far!
Title: Re: [Help] Decimal into binary system
Post by: Handoko on February 09, 2017, 02:51:42 am
Calm down sir. You're not the one to blame.

At the very beginning the TS was asked to provide more information: "And what have you got so far?" By knowing how far his (or his friend) know about the problem or has done or at least already in his mind, we can better provide helps. But he answered with the thing that out of what I ever guess.

The code you provided isn't totally useless. At least he now can study how it works. If the student really wants to be a programmer, he will make a way to understand the code. But if he is a 'lazy' student, he will just submit the code to his teacher. So if he really is the lazy one, why should we care. Everyone is a winner. You have showed that your code works, we voiced our voice and the student got the code.
Title: Re: [Help] Decimal into binary system
Post by: Bart on February 09, 2017, 06:43:29 pm
Ok, I was the one to post the code. (I didn't give away anything.) I also am a retrired teacher.
...
The two's complement is a nontrivial concept. Someone who doesn't feel competent at least on a level to be able to start with a task as posted, will also not be able to understand and to apply the concept except in a purely mechanical way.
Isn't it the teacher's role to prepare the basis and to answer questions?

Of course the task at hand is not trivial.
The fact that one's complement and two's complement implementation is asked for implies that the student is supposed to understand what that is (I would have to google that).

The problem I am having with TS is that he did not show us that he (or his friend) made any effort at all.
I do not even require him to show me working code.
I want to see that he has thought about the problem and has at least tried to make an algorithm to solve the problem at hand.
This can be in "natural language" or pseudo code.

I would at least have expected (for "direct" method) that he would have come up with a solution that involves repeated "divide by 2 and see what the remainder is".
From that concept we could have helped him.

If so:  Can anybody here help by hints to find a way to write a Pascal routine for the conversion? Can we do it ourselves from scratch extemporizing?

For the most simple implementation: yes we can.
For one's complement: I think I can, for two's complement I would probably have to ask for help as well.
Difference is that for me this would not be homework and people around here know I'm legit, so they are probably inclined to give me sample code or point me to resources that help me make my own implementation.

Can we be sure that the task was thoroughly prepared and clearly put by the teacher?

I sincerely hope so.

Everything may be completeky differnt, of course, from what I supposed. No factual information so far!

Because TS did not respond at all.

He has moved on to another forum where he got a ready-made answer and submitted that (or he has failed because the deadline has expired).

Bart