For this question, you will implement the following C function: // Interleaves t
ID: 3792457 • Letter: F
Question
For this question, you will implement the following C function:
// Interleaves the digits of two numbers, working from *least significant*
// to *most significant*. When interleaving 2 digits, thedigit from parameter
// most, will always take the more significant place. When the two parameters
// don't have an equal number of digits, 0s are used as place holders. The
// result of this interleaving is a uint64_t, an unsigned 64 bit integer.
// Thus, the result won't start with an explicit 0, and that's why you don't
// see 0 in the most significant place of the interleaved integer below, e.g.,
// the result is not 01122334, but 1122334.
//
// interleave(123, 1); returns 102031
// interleave(123, 1234); returns 1122334
// interleave(0, 1234); returns 1020304
// interleave(12345, 1); returns 1020304051
// interleave(11111111, 22222222); returns 1212121212121212
// interleave(111111111, 0); returns 101010101010101010
// interleave(1464030511, 8474779565); returns 18446744073709551615
//
// Pre: most and least have been initialized. You may assume the
//intended result will be less than UINT64_MAX (18446744073709551615)
//
// Returns: the integer formed by interleaving each digit in most and least
//
// Restrictions:
// You may use any integer operations supported in C. You may also
// use any selection (if, if..else, etc) or iteration (for, while)
// constructs.
//
// You MAY NOT use an array or string, nor may you perform
// any I/O operations. Similarly you may not use math.h and string.h.
//
uint64_t interleave(uint64_t most, uint64_t least);
(C language, linux)
Given:
Interleave.h:
interleave.c: (this is what needs to be changed)
driver.c:
Explanation / Answer
Here is the code for you:
#include <stdio.h>
#include <inttypes.h>
//Interleaves the digits of two numbers, working from least significant to most significant.
//When interleaving 2 digits, the digit from parameter most, will always take the more
//significant place. When the two parameters don't have an equal number of digits, 0s are
//used as place holders. The result of this interleaving is a uint64_t, an unsigned 64 bit
//integer. Thus, the result won't start with an explicit 0, and that's why you don't see
//0 in the most significant place of the interleaved integer below, e.g., the result is
//not 01122334, but 1122334.
//
// interleave(123, 1); returns 102031
// interleave(123, 1234); returns 1122334
// interleave(0, 1234); returns 1020304
// interleave(12345, 1); returns 1020304051
// interleave(11111111, 22222222); returns 1212121212121212
// interleave(111111111, 0); returns 101010101010101010
// interleave(1464030511, 8474779565); returns 184467...
//
// Pre: most and least have been initialized. You may assume
// intended result will be less than UINT64_MAX
//
// Returns: the integer formed by interleaving each digit in most and least.
//
// Restrictions:
// You may use any integer operations supported in C. You may also use any selection
// (if, if..else, etc) or iteration (for, while) constructs.
//
// You MAY NOT use an array or string, nor may you perform any I/O operations.
// Similarly you may not use math.h and string.h
uint64_t interleave(uint64_t most, uint64_t least)
{
/*if(most == 0 && least == 0)
return 0;
return interleave(most/10, least/10) * 10 + most % 10 * 10 + least % 10 * 10;*/
uint64_t output = 0;
uint64_t reverseOut = 0;
while(most != 0 || least != 0)
{
output = output * 10 + least % 10;
least /= 10;
output = output * 10 + most % 10;
most /= 10;
}
while(output != 0)
{
int rem = output % 10;
reverseOut = reverseOut * 10 + rem;
output /= 10;
}
return reverseOut;
}
int main()
{
printf("%llu ", interleave(123, 1));
printf("%llu ", interleave(123, 1234));
printf("%llu ", interleave(0, 1234));
printf("%llu ", interleave(12345, 1));
printf("%llu ", interleave(11111111, 22222222));
printf("%llu ", interleave(111111111, 0));
printf("%llu ", interleave(1464030511, 8474779565));
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.