Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help with C code, Only C! Integer Decimal to Base-q ConversionComplete the functionvoid num2q(int num, char num_q[], int q))in homework2, whichconverts an integer number to

Help with C code, Only C!

Integer Decimal to Base-q ConversionComplete the functionvoid num2q(int num, char num_q[], int q))in homework2, whichconverts an integer number to a base-q number string.Use letter for digits that are bigger than 9, such that A for 10, B for 11 etc.To make things simple, we only use capital letter here.The integer to be tested here will be smaller than the maximum integer.The q to be tested will be bigger than 2 and less than 20 and you do not have to consider theexceptions that if q is too big that there are not enough letters for representing digits.3 Integer Base-q Conversion to DecimalDo the reverse of above, ,complete the functionint q2num(char num_q[], int q)in homework2,which converts a base-q number string to a integer number.

4 Floats Decimal to Binary ConversionFloats have two parts, i.e. integer part and fraction part, and for each of these two parts they maybe represented in decimal format or in binary format, like below:8.125decimal format100.001binary formatThere are rules for converting them interchangeably and in this homework you are required toimplement what has been described below.4.1 Decimal Integer to BinaryTo convert integer to binary, start with the integer in question and divide it by 2 keeping noticeof the quotient and the remainder. Continue dividing the quotient by 2 until you get a quotient ofzero. Then just write out the remainders in the reverse order.Here is an example of such conversion using the integer 12.First, lets divide the number by two specifying quotient and remainder:Dividend(Dd) = Divisor(Dr) * Quotient(Q) + Reminder(R)Dd Dr QR12 = 2 * 6 +06 = 2 * 3 + 03 = 2 * 1 + 11 = 2 * 0 + 1Now, we simply need to write out the remainder in the reverse order1100. So, 12 in decimalsystem is represented as1100in binary.4.2 Decimal Fraction to BinaryTo convert fraction to binary, start with the fraction in question and multiply it by 2 keepingnotice of the resulting integer and fractional part. Continue multiplying by 2 until you get aresulting fractional part equal to zero. Then just write out the integer parts from the results ofeach multiplication.Here is an example of such conversion using the fraction 0.375.Multiplicant(Mt) * Multiplier(Mr)=Product(Fraction(F) + Integer(I))Mt MrFI0.375 * 2 = 0.75 + 00.75 * 2 = 0.5 + 10.5 * 2 = 0+ 1Now, lets just write out the resulting integer part at each step0.011. So, 0.375 in decimalsystem is represented as0.011in binary.

4.3 Floats to binary stringSuppose we let a binary string to represent a float. And this binary string is delimited by thecharacter " . " into two parts integer part and fraction part, like discussed above.Here are the requirements of this kind of binary string here:1. For this string, it has to have " . " in it.2. For the fraction, it at least contains 1 bit and at most 8 bits3. For the 8 bits fraction, you dont need to use rounding.4. For the integer part, it at least contains 1 bit.Based on these requirements, complete the functionvoid f2b(float f, char f_b[])in home-work2 to convert a float number into a binary string.4.4 Notes1. I will testify the floats within the range[28,28]on the function ofvoid f2b(float f, charf_b[])5 Floats Binary to Decimal ConversionDo the reverse of above, complete the functionfloat b2f(char f_b[])in homework2, such that abinary string being converted to a float.

Source code:

#include #include #include

/* this function can print out an q_string s[]*/ void print(char s[], int q){

printf(" %2d-string: ", q); for (int i=0;s[i]; i++){ if (!((i)%4)&&i!=0) printf("|"); printf("%c", s[i]); } printf(" "); }

/* Read bits into a char array * from memory of a 32-bit data type */ void bin_char(unsigned n, char s[]) { unsigned i, j=0; for (i = 1 << 31; i > 0; i = i / 2) { s[j++] = (n & i)? '1' : '0'; } s[j] = 0; }

/* this function converts the integer num into a q_based string */ void num2q(int num, char num_q[], int q){ }

/* this function converts a q_based string into an integer */ int q2num(char num_q[], int q){ }

/* this function converts the float f into a binary string fb[]*/ void f2b(float f, char fb[]){}

/* this function converts the binary string fb[] into a float */ float b2f(char fb[]){ }

int main(void) {

char num_q[100]; int num = 2020; // this loop converts num into string num_q and print out for (int i=2; i<16; i++){ num2q(num, num_q, i); print(num_q, i); printf(" num: %d ", q2num(num_q,i)); }

char fb[32]; // this loop converts f into binary string fb[] for (int i=-10;i<4;i++){ float f = (float) pow(2,i); f2b(f,fb); printf("pow(2,%2d)f: %g ",i, f); print(fb,2); printf("pow(2,%2d)f: %g ",i, b2f(fb)); }

float f = 127 + 0.1; f2b(f,fb); printf(" f: %g ", f); print(fb,2); printf(" f: %g ", b2f(fb));

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

1. Identify six different types of history.

Answered: 1 week ago

Question

2. Define the grand narrative.

Answered: 1 week ago

Question

4. Describe the role of narratives in constructing history.

Answered: 1 week ago