The problem
Given an enter of an array of digits, return the array with every digit incremented by its place within the array: the primary digit will probably be incremented by 1, the second digit by 2, and so on. Be certain to begin counting your positions from 1 ( and never 0 ).
Your end result can solely include single digit numbers, so if including a digit with its place offers you a multiple-digit quantity, solely the final digit of the quantity needs to be returned.
Notes:
- return an empty array in case your array is empty
- arrays will solely include numbers so don’t fear about checking that
Examples:
[1, 2, 3] --> [2, 4, 6] # [1+1, 2+2, 3+3]
[4, 6, 9, 1, 3] --> [5, 8, 2, 5, 8] # [4+1, 6+2, 9+3, 1+4, 3+5]
# 9+3 = 12 --> 2
The answer in C
Possibility 1:
#embody <stddef.h>
unsigned *incrementer(unsigned *dest, const unsigned *src, size_t n) {
if (n == 0) return NULL;
for (size_t i = 0; i < n; i++) {
dest[i] = (src[i] + i + 1) % 10;
}
return dest;
}
Possibility 2:
#embody <stddef.h>
unsigned *incrementer(unsigned *dest, const unsigned *src, size_t n) {
if(!n) return NULL;
for(size_t i = 0; i < n; i++) dest[i] = (src[i] + (i + 1)) % 10;
return dest;
}
Possibility 3:
#embody <stddef.h>
#embody <stdio.h>
#embody <string.h>
#embody <stdlib.h>
unsigned *incrementer(unsigned *dest, const unsigned *src, size_t n)
{
if (n <= 0) return NULL;
char str[100];
for (unsigned i = 0; i < n; ++i) {
sprintf(str, "%d", src[i] + (i+1));
if (strlen(str) > 1) dest[i] = str[strlen(str)-1] - '0';
else dest[i] = atoi(str);
strcpy(str, "");
}
return dest;
}
Take a look at instances to validate our answer
#embody <criterion/criterion.h>
#embody <stddef.h>
#embody <limits.h>
#embody <string.h>
#embody <stdio.h>
extern unsigned *incrementer(unsigned *dest, const unsigned *src, size_t n);
#outline ARRLIM 0xF
#outline FMT_UINT (CHAR_BIT * sizeof(unsigned) / 3)
#outline FMT_ALIGN 0x2
#outline FMT_DESCR 0x40
#outline FMT_ARR ((FMT_UINT + FMT_ALIGN) * ARRLIM)
#outline OUTPFLEN (FMT_DESCR + FMT_UINT * 3)
typedef enum {
ASSERT_PASS,
ASSERT_FAIL
} assertop;
assertop assert_mem_eq(const void *precise, const void *anticipated, size_t n, size_t measurement)
!anticipated)
return precise != anticipated ? ASSERT_FAIL : ASSERT_PASS;
for (i = 0ul; n--; i += measurement)
if (memcmp((char *)precise+i, (char *)anticipated+i, measurement))
return ASSERT_FAIL;
return ASSERT_PASS;
char *assert_fmt(char *outpf, const unsigned *precise, const unsigned *anticipated, const unsigned *arr, size_t n)
{
const char *descr[] = { "*Precise*:", "nExpected:", "n Array:" };
const unsigned *params[] = { precise, anticipated, arr };
//const unsigned *r;
size_t pos, m, i;
for (pos = m = 0ul; m < 3ul; ++m) {
pos += sprintf(outpf+pos, "%s ", *(descr+m));
if (!(arr = *(params+m))) {
pos += sprintf(outpf+pos, "NULL");
proceed;
}
pos += sprintf(outpf+pos, "[ ");
for (i = 0ul; i < n; ++i)
pos += sprintf(outpf+pos, "%u, ", *(arr+i));
if (i)
pos -= FMT_ALIGN;
pos += sprintf(outpf+pos, " ]");
}
pos += sprintf(outpf+pos, "n N: %lun", n);
return outpf;
}
void assert_data(const unsigned *arr, size_t n, const unsigned *anticipated)
{
char outpf[OUTPFLEN];
unsigned dest[ARRLIM];
unsigned *precise = incrementer(dest, arr, n);
if (!precise) {
if (anticipated)
cr_assert_fail("%s", assert_fmt(outpf, precise, anticipated, arr, n));
else
cr_assert(1);
}
else if (assert_mem_eq(precise, anticipated, n, sizeof(unsigned)) == ASSERT_FAIL)
cr_assert_fail("%s", assert_fmt(outpf, precise, anticipated, arr, n));
else if (assert_mem_eq(dest, anticipated, n, sizeof(unsigned)) == ASSERT_FAIL)
cr_assert_fail("Error: shouldn't allocate reminiscence!npercents", assert_fmt(outpf, precise, anticipated, arr, n));
else
cr_assert(1);
}
Take a look at(Sample_Test, should_return_the_incremented_array)
{
assert_data((const unsigned[]){ 1,2,3 }, 3ul, (const unsigned[]){ 2,4,6 });
assert_data((const unsigned[]){ 4,6,7,1,3 }, 5ul, (const unsigned[]){ 5,8,0,5,8 });
assert_data((const unsigned[]){ 3,6,9,8,9 }, 5ul, (const unsigned[]){ 4,8,2,2,4 });
assert_data((const unsigned[]){ 1,2,3,4,5,6,7,8,9,9,9,9,9,8 }, 14ul, (const unsigned[]){ 2,4,6,8,0,2,4,6,8,9,0,1,2,2 });
assert_data((const unsigned[]){ 0 }, 0ul, NULL);
}