Thanks. I programmed the floor function, that's solved.
Now at some point of the programming, it seems that I have no choice but to assign the value of a real variable to an integer variable. the operator x mod y works only when both x and y are integers, but the answer that my function floor() returns is a real number, and that's causing troubles for me.
Let me tell you what i'm trying to do. I'm writing a program that takes a real number as input and then converts it to any given base (2-9).
This is what I've done so far:
|
Program base_change;
uses wincrt;
Type no = array[1..50] of integer;
Var base:integer;
dig:integer;
x:real;
Var numb1:no;
numb2:no;
function floor (y:real): real;
Var m:real;
Begin
if y < 0 then
m := int(y) - 1
else
m := int(y);
floor := m;
End;
Procedure intconv(base:integer ; x:real);
Var n:integer;
number:real;
Begin
number := floor(x);
n:=1;
Repeat
Numb1[n] := (number mod base);
number:= (number div base);
n := n+1;
Until (number = 0);
for i := n-1 downto 1 do
write(Numb1[i]);
End;
Begin
Write('Choose the base: ');
readln(base);
Write('Enter a real number to be converted into base',base,':');
readln(x);
intconv(base, x);
End.
|
I guess my general idea is correct. When I'm given a real number, first I convert the integer part using the procedure intconv, then I convert the fraction part using another procedure that I'll right after it. But my main problem for now is: How could I save the value of x (a real type data) in number(an integer type data)?
Is there anyways to do that?
Thanks in advance.