Please write a function and names it convert_to_base hat finds the representatio
ID: 3549957 • Letter: P
Question
Please write a function and names it
convert_to_base hat finds the representation of the value in the given base and
// stores it as a sequence of digits in the digits array.
//
// For example, if value = 5 and base = 2, the array is set
// to 0 0 ... 0 1 0 1.
//
// If the value is too big, then once we fill up the array,
// we stop and print an error message that gives the part we
// couldn't represent. Example: If value = 31, base = 2,
// and the array had length 3 then we set the array to 1 1 1
// and print out 24 as the value left over. (You'd have to
// set ARRAYLEN to 3 to replicate this example.)
//
void convert_to_base(int value, int base, int digits[], int len) {
// We'll fill in the digits from right to left;
// the next digit to fill in is at digits[position].
//
// If leftover = value - the number represented by the
// digits we've filled in so far, then remain =
// (value - leftover) / base ^ (len - position).
//
// posn_value = the value represented by a 1 at the
// current position (i.e., base ^ (len - position))
//
int position = len-1;
int posn_value = 1;
int remain = value;
// Pseudocode:
// while remain > 0 and position >= 0,
// Calculate remain/base, put remainder
// into digits, update position, and
// set remain to the quotient
// if remain is 0
// We're done? (Oops! what about the rest of the
// digits?)
// else
// Calculate the leftover value and
// complain about it
// HINT: It might help to introduce a new variable to
// keep track of the leftover value as the loop runs
}
Explanation / Answer
#include <stdio.h>
// stores it as a sequence of digits in the digits array.
//
// For example, if value = 5 and base = 2, the array is set
// to 0 0 ... 0 1 0 1.
//
// If the value is too big, then once we fill up the array,
// we stop and print an error message that gives the part we
// couldn't represent. Example: If value = 31, base = 2,
// and the array had length 3 then we set the array to 1 1 1
// and print out 24 as the value left over. (You'd have to
// set ARRAYLEN to 3 to replicate this example.)
//
void convert_to_base(int value, int base, int digits[], int len) {
// We'll fill in the digits from right to left;
// the next digit to fill in is at digits[position].
//
// If leftover = value - the number represented by the
// digits we've filled in so far, then remain =
// (value - leftover) / base ^ (len - position).
//
// posn_value = the value represented by a 1 at the
// current position (i.e., base ^ (len - position))
//
int position = len-1;
int posn_value = 1;
int remain = value;
int leftover = value;
while (remain > 0 && position >= 0)
{
digits[position] = remain % base;
remain = remain/base;
leftover -= digits[position]*posn_value;
posn_value *= base;
position--;
}
if (remain > 0)
{
printf("left=%d ", leftover);
}
else
{
while (position >= 0)
{
digits[position] = 0;
position--;
}
}
// Pseudocode:
// while remain > 0 and position >= 0,
// Calculate remain/base, put remainder
// into digits, update position, and
// set remain to the quotient
// if remain is 0
// We're done? (Oops! what about the rest of the
// digits?)
// else
// Calculate the leftover value and
// complain about it
// HINT: It might help to introduce a new variable to
// keep track of the leftover value as the loop runs
}
int main()
{
// test it
int digits1[8];
int digits2[3];
int i;
convert_to_base(5, 2, digits1, 8);
for (i = 0; i < 8; i++)
printf("%d", digits1[i]);
printf(" ");
convert_to_base(31, 2, digits2, 3);
for (i = 0; i < 3; i++)
printf("%d", digits2[i]);
printf(" ");
return 0;
}