Saturday, March 4, 2023
HomeSoftware EngineeringThe way to Create an Incrementer in C

# The way to Create an Incrementer in C

## 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 might be incremented by 1, the second digit by 2, and so on. Make sure that toÂ begin counting your positions from 1Â ( and never 0 ).

Your outcome can solely include single digit numbers, so if including a digit with its place provides you a multiple-digit quantity, solely the final digit of the quantity ought 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
``````

Possibility 1:

``````#embrace <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:

``````#embrace <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:

``````#embrace <stddef.h>
#embrace <stdio.h>
#embrace <string.h>
#embrace <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;
}
``````

## Check circumstances to validate our resolution#

``````#embrace <criterion/criterion.h>
#embrace <stddef.h>
#embrace <limits.h>
#embrace <string.h>
#embrace <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)

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: mustn't allocate reminiscence!npercents", assert_fmt(outpf, precise, anticipated, arr, n));
else
cr_assert(1);
}

Check(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);
}
``````

RELATED ARTICLES