Question: A method for determining the first m digits 0 . b 1 b 2 b 3 . . . bm of the binary representation of

A method for determining the first m digits 0.b1b2b3...bm of the binary representation of a real number
x in (0,1) is the following algorithm:
c <- x
for j from 1 to m do
b[j]<- floor(2*c)
c <-2*c - b[j] where floor(x) is the greatest integer less than or equal to the real number x, and * represents
multiplication.
(a) Explain how this algorithm works.
(b) Write a C function that implements this algorithm. The prototype of your function should be
void get_binary(double x, int* b, int m);
where x is the real number in double precision floating point representation, b is the starting
address of an array of integers, and m is the number of digits we want to obtain. You will need
to include the header file math.h; also include the stdio.h header:
#include
#include
Test your function with the following main function:
int main(){
int b[30];
get_binary(0.1, b,30);
for(int i =0; i <30; i++){
printf("%d", b[i]);
}
return 0;
}
PLEASE NOTE: Even though the pseudo-code representation of the algorithm indexes the array of
digits starting at 1, C/C++ always starts indexing at 0. Make sure to write your code so that the
calculations are performed correctly.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!