Other Programming Languages
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreOther Programming Languages

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old September 29th, 2006, 12:31 PM
mit111 mit111 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 4 mit111 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 35 sec
Reputation Power: 0
Pascal Help!

Hi guys,

Been doing Pascal at college an have been told to change a number into binary. I think I've got the idea of how to do it, it's just that the code seems to be full of errors which I just can find/understand.

I was using Freepascal, I couldn't copy it, so I took a screenshot.
URL

So, if anyone could tell me were I'm going wrong, that would be great!

Thanks,

Reply With Quote
  #2  
Old September 30th, 2006, 02:56 AM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Location: The People's Republic of Berkeley
Posts: 1,082 Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 10 h 16 m 50 sec
Reputation Power: 446
Quote:
Originally Posted by mit111
Hi guys,

Been doing Pascal at college an have been told to change a number into binary. I think I've got the idea of how to do it, it's just that the code seems to be full of errors which I just can find/understand.


The main problem I see is that you aren't actually saving the value you are reading into the variable input1; you dropped the trailing '1'. The reason you may not be getting a compiler error is because input is the name of the standard console, equivalent to stdin in C and Perl or cin in C++. Since you are giving ReadLn() an input stream but no variables, it doesn't actually read anything. To make things a bit less ambiguous, you might rename input1 to something like value or just simply n.

As a stylistic note, any time you have a series of variables with names like foo1, foo2, foo3, etc., you generally should simply replace them with an array:

Code:
answer: array [1..7] of Integer;


answer[1] := input1 mod 2;
input1 := input1 div 2;  { I'll explain this in a moment }
answer[2] := input1 mod 2;
{ ... }


Also, as shown here, the successive values need to be from the division of the current version of the number, not the modulo; the modulo of any number by 2 is always either 0 or 1, after all, with 0 values being even and 1 values being odd (this is the basis of the algorithm).

There's another problem with this, in that you are printing the bits in the wrong order. In the algorithm you are using, the first bit returned is the least significant, i.e., the rightmost bit. The second bit it returns is the second twos, the third bit the fours, and so on. You can fix this by reversing either the order you produce them in, or, more usefully, the order in which you print them.

Alternately, you can dispense with all but one of the temporary values entirely, and instead re-write the function so that it goes through the number recursively, getting the current bit, then calling itself, then printing the bit. This is the classic form of the algorithm, in fact (in pseudo-code):
Code:
procedure printBinary(x) is
   if x is less than zero
       print a negative sign
       printBinary(abs(x))
   else
       if x is equal to zero
           return from the function
       else
           bit :=  x mod 2
           printBinary(x div 2)
           write(bit)
end printBinary


Done this way, you can print binary values for values up to 2,147,483,648 accurately. (BTW, the maximum value a 7 bit number can hold is 127, not 134).

(Converting this algorithm into Pascal is left as an exercise. As a hint, I will tell you that you'll need to write it as a procedure - or perhaps a function, one which returns, say, a string - and that you'll want to read the value in first before calling it.)

Finally, regarding the difficulty of cutting and pasting code from FreePascal, you should recall that a Pascal program is just a text file; you could, after having closed it in FreePascal, opened the file in a text editor such as Notepad, and cut and paste it from there. Alternately, you can use DevPascal as your development system, which uses the Free Pascal compiler but gives a Windows-based editing environment to work in.
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

Last edited by Schol-R-LEA : September 30th, 2006 at 09:12 AM.

Reply With Quote
  #3  
Old September 30th, 2006, 03:37 AM
mit111 mit111 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 4 mit111 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 35 sec
Reputation Power: 0
Wow! Thanks for helping me understand it a little better.

Edit: I've made the file now and it compiles without any bugs, the only problem is, once I input my number the window closes, is there any way of stopping this so I can see if my code works?

Reply With Quote
  #4  
Old September 30th, 2006, 07:38 AM
LinuxPenguin's Avatar
LinuxPenguin LinuxPenguin is offline
fork while true;
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: May 2005
Location: England, UK
Posts: 5,535 LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)  Folding Points: 11590 Folding Title: Novice Folder
Time spent in forums: 1 Month 3 Weeks 1 Day 19 h 23 m 58 sec
Reputation Power: 1008
pascal Code:
Original - pascal Code
  1. procedure pause;
  2. begin
  3.   writeln('Press any key to continue...');
  4.   readkey;
  5. end;

Reply With Quote
  #5  
Old September 30th, 2006, 08:13 AM
lingon's Avatar
lingon lingon is offline
C++arl!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Location: Stockholm
Posts: 165 lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 5 h 59 m 35 sec
Reputation Power: 12
Quote:
Originally Posted by LinuxPenguin
pascal Code:
Original - pascal Code
  1. procedure pause;
  2. begin
  3.   writeln('Press any key to continue...');
  4.   readkey;
  5. end;


Jepp, or use the 'readln();' function, I belive the only difference is that readkey waits untill you enter any key and readln() ignores all keys except the ENTER key.

EDIT: Also, instead of taking screenshots of you compiler, open the source code in wordpad and copy it from there.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreOther Programming Languages > Pascal Help!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway