How to Produce an Incrementer in C

The obstacle

Provided an input of a range of digits, return the selection with each digit incremented by its position in the selection: the very first digit will be incremented by 1, the 2nd digit by 2, and so on. Ensure to begin counting your positions from 1 ( and not 0 ).

Your outcome can just include single digit numbers, so if including a digit with its position offers you a multiple-digit number, just the last digit of the number must be returned.

Notes:

  • return an empty selection if your selection is empty
  • varieties will just include numbers so do not fret about examining 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 option in C

Choice 1:

 #include << stddef.h>>.

anonymous * incrementer( anonymous * dest, const anonymous * 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;.
}

Choice 2:

 #include << stddef.h>>.

anonymous * incrementer( anonymous * dest, const anonymous * 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;.
}

Choice 3:

 #include << stddef.h>>.
#include << stdio.h>>.
#include << string.h>>.
#include << stdlib.h>>.

anonymous * incrementer( anonymous * dest, const anonymous * src, size_t n).
{
if (n < 1) dest[100] = str[i] - '0';.
else dest[i] = atoi( str);.
strcpy( str, "");.
}
return dest;.
}
[strlen(str)-1] Test cases to verify our option[i] #

#include << criterion/criterion. h>>.
#include << stddef.h>>.
#include << limits.h>>.
#include << string.h>>.
#include << stdio.h>>.

extern anonymous * incrementer( anonymous * dest, const anonymous * src, size_t n);.

#define ARRLIM 0xF.
#define FMT_UINT (CHAR_BIT * sizeof( anonymous)/ 3).
#define FMT_ALIGN 0x2.
#define FMT_DESCR 0x40.
#define FMT_ARR (( FMT_UINT + FMT_ALIGN) * ARRLIM).
#define OUTPFLEN (FMT_DESCR + FMT_UINT * 3).

typedef enum {
ASSERT_PASS,.
ASSERT_FAIL.
} assertop;.

assertop assert_mem_eq( const space * real, const space * anticipated, size_t n, size_t size).
! anticipated).
return real!= anticipated? ASSERT_FAIL: ASSERT_PASS;.
for (i = 0ul; n--; i += size).
if (memcmp(( char *) real+ i, (char *) anticipated+ i, size)).
return ASSERT_FAIL;.
return ASSERT_PASS;.

char * assert_fmt( char * outpf, const anonymous * real, const anonymous * anticipated, const anonymous * arr, size_t n).
{
const char * descr

 = {real, anticipated, arr};.
// const anonymous * 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");. continue;. } pos += sprintf( outpf+ pos, "[]");. } pos += sprintf( outpf+ pos, "n N: %lun", n);. return outpf;. } void assert_data( const anonymous * arr, size_t n, const anonymous * anticipated). { char outpf[];. anonymous dest[ "); for (i = 0ul; i < n; ++i) pos += sprintf(outpf+pos, "%u, ", *(arr+i)); if (i) pos -= FMT_ALIGN; pos += sprintf(outpf+pos, " ];. anonymous * real = incrementer( dest, arr, n);. if (! real) { if (anticipated). cr_assert_fail("% s", assert_fmt( outpf, real, anticipated, arr, n));. else. cr_assert( 1 );. } else if (assert_mem_eq( real, anticipated, n, sizeof( anonymous)) == ASSERT_FAIL). cr_assert_fail("% s", assert_fmt( outpf, real, anticipated, arr, n));. else if (assert_mem_eq( dest, anticipated, n, sizeof( anonymous)) == ASSERT_FAIL). cr_assert_fail(" Mistake: must not designate memory!n% s", assert_fmt( outpf, real, anticipated, arr, n));. else. cr_assert( 1 );. } Test( Sample_Test, should_return_the_incremented_array). { assert_data(( const anonymous[OUTPFLEN]) {1,2,3}, 3ul, (const anonymous[ARRLIM]) {2,4,6} );. assert_data(( const anonymous[]) {4,6,7,1,3}, 5ul, (const anonymous[]) {5,8,0,5,8} );. assert_data(( const anonymous[]) {3,6,9,8,9}, 5ul, (const anonymous[]) {4,8,2,2,4} );. assert_data(( const anonymous[]) {1,2,3,4,5,6,7,8,9,9,9,9,9,8}, 14ul, (const anonymous[]) {2,4,6,8,0,2,4,6,8,9,0,1,2,2} );. assert_data(( const anonymous[]) {0}, 0ul, NULL);. } []

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: