# trouble translating pseudo code for decimal number to base b

1 view (last 30 days)
Tom Schneider on 10 Aug 2022
Commented: Tom Schneider on 10 Aug 2022
I was given the following pseudo code however because of matlabs arrays starting at 1 and other issues im running into i cant really translate it and its really confusing.
I managed to translate the "base(z,b)" properly which translates an int to a base b number.
I cant really get behind why some indices are -k. the output doesnt really make much sense either.
Pseudocode:
decimaltobase(z,b):
nums = base(z,b);
r[0] = z - floor(z);
k = 0;
period = 0;
while period == 0 && r[k] != 0:
k = k + 1;
r[k] = b * r[k - 1];
z[-k] = r[k] - z[-k];
if r[k] == r[p] for any p < k:
period = 1;
store p;
if rk == 0:
return nums;
else:
return nums "." r;
my code:
function y = decimaltobase(z,b)
nums = base(floor(z),b);
r(1) = z - floor(z);
k = 1;
period = 1;
while period == 1 && r(k) ~= 0
k = k + 1;
r(k) = b * r(k - 1);
z(k) = floor(r(k));
r(k) = r(k) - z(k);
pfound = false;
if pfound == false
p = 1;
end
while p < k
disp(p)
disp(k)
if r(k) == r(p)
period = 1;
pfound = true;
else
p = p + 1;
end
end
end
y = horzcat(nums,".",r);
end
##### 2 CommentsShowHide 1 older comment
Tom Schneider on 10 Aug 2022
ah yes, i didnt think abt the k-th last element thank you very much. unfortunately this is a project for uni so I, in fact, have to reinvent the wheel ðŸ˜‚