Any Pascal programmers?

Serp86

Senior member
Oct 12, 2002
671
1
0
Hi.

I am taking an optional study unit at university which is basically an introduction to programming in pascal. Now, this was compulsory, so dont bug me with "whats a biology and chemistry student doing pascal?" :p

Anyway, I nailed pretty much all the commands and did some past papers which were'nt too difficult. The problem is the assignment in which he asked us to make a program that converts from decimal to binary, octal or hexadecimal.

The main problem is how to make pascal convert a number into hexadecimal and vice versa, since there will be letters involved, too.

Can anyone give me any tips?
 

Serp86

Senior member
Oct 12, 2002
671
1
0
hey

Thanks for the reply :D but unfortunately I dont understand a word of it :(
Are there any C++ to pascal converters around?
 

Serp86

Senior member
Oct 12, 2002
671
1
0
Ok - I thought i'll start slow so I wrote this simple program to convert them from decimal to binary at least:

program Digits2bits;

VAR
A:integer;
B:integer;
C:integer;
Begin
writeln ('please enter a digit');
readln (A);
writeln ('the number in binary is:');
While A>1 DO
B := A mod 2;
writeln (B);
C := A-B;
C := C/2;
A := C;
End;
;readln

END.

HOWEVER - I get an error - type mismatch when it gets to C := C/2 - According to the logic, when c is divided by 2, it should always be an integer. Damn this sucks
 

sciencewhiz

Diamond Member
Jun 30, 2000
5,885
8
81
It's been a long time since I've done pascal so I can't really help you with your type mismatch problem. However, rather then dividing by 2, you can shift right which does the same thing.

I'd suggest either shifting right and ANDing with 1 or shifting left and checking the sign.
 

Serp86

Senior member
Oct 12, 2002
671
1
0
Thanks for the reply.

Turns Out I needed to do a C div 2 instead of c/2 :eek:

Now, I was experimenting with procedures and functions so that I can structure a menu and use them to direct the program to the required function. The problem is that when using CASE, the program isn't directed to the functions. It just gives me an unknown identifier

E.g.
CASE choice of
1:FUNCTION1
2:FUNCTION2
3:FUNCTION3
END;

it doesnt work :(

edit:

Thank god i'm not a programer hehe
 

Spydermag68

Platinum Member
Apr 5, 2002
2,616
99
91
Pascal web page

Try this web page. It has interger division and modulus.

Thebasi loop is

while (num > 0)
{
mod_num := num mod 16;
if (mod_num >= 10)
{
add a "a - f" character to output string;
}
else
{
add number 0 - 9 to output string
}
num := num div 16;

}
 

JustAnAverageGuy

Diamond Member
Aug 1, 2003
9,057
0
76
I'll try and add some documentation to the code then. Perhaps you understand the logic at least :)

//CREATE FUNCTION DECTOBIN THAT RECEIVERS AN INTEGER
String DecToBin(int num)
{
//DECLARE VARIABLES
String binnum = "";
//DETERMINES HOW MANY POSITIONS TO DISPLAY
int bob = 128; //Max number = bob*2

//DISPLAY ERROR IF LARGE NUMBER ENTERED, 8 BIT SUPPORT ONLY
if (num >= 256)
{
cout << "Sorry, numbers larger than 256 not fully supported in this version";
}

//START A LOOP
do
{
//CHECKS TO SEE IF BOB IS LESS THAN OR EQUAL TO NUMBER
if (num >= bob)
{
//SUBTRACTS BOB FROM NUMBER
num=num-bob;
//ADDS 1 TO BINARY RESULT
binnum = binnum + "1";
}
else
//ADDS 0 TO BINARY RESULT IF TOO SMALL
binnum = binnum + "0";
//DIVIDES BY 2
bob = bob/2;
//CONTINUES LOOP UNTIL BOB IS EQUAL TO ONE
}while(bob!=1);
//RETURNS RESULT AS STRING "01010101"
return(binnum);
}

I don't know if PASCAL has anything similar, but the way I did it was just keep tacking on either a 0 or a 1 to a string. It may not be the best way, but it does work.

The number 200, for example, would run like this.

Is 200 larger than 128?
Yes
Subtract 128 from 200 (72)
Divide 128 by 2 (64)
Binary Result = "1"
Is 72 larger than 64
Yes
Subtract 64 from 72 (8)
Divide 64 by 2 (32)
Binary Result = "11"
Is 8 larger than 32
No
Divide 32 by 2 (16)
Binary Result = "110"
Is 8 larger than 16
No
Divide 16 by 2 (8)
Binary Result = "1100"
Is 8 larger than 8
Yes
Subtract 8 from 8 (0)
Divide 8 by 2 (4)
Binary Result = "11001"
Is 0 larger than 4
No
Divide 4 by 2 (2)
Binary Result = "110010"
Is 0 larger than 2
No
Divide 2 by 2 (1)
Binary Result = "1100100"
Is 0 larger than 1
No
Binary Result = "11001000"
Number = 1, STOP LOOP
 

Serp86

Senior member
Oct 12, 2002
671
1
0
Thanks for all the replies people :D youre the best hehe

Anyway, menu is done - and right now i'm doing the conversion methods for different types of digits
Anything to hexadecimal still looks like being a pain, but I think i'll manage.

Super thanks to JustAnAverageGuy who has reached a new lever of JustABetterGuy :D