The obstacle
Provided a variety of integers of any length, return a variety that has 1 contributed to the worth represented by the variety.
- the variety can’t be empty
- just non-negative, single digit integers are enabled
Return nil
( or your language’s equivalent) for void inputs.
Examples:
Legitimate varieties
[4, 3, 2, 5]
would return [4, 3, 2, 6]
[1, 2, 3, 9]
would return [1, 2, 4, 0]
[9, 9, 9, 9]
would return [1, 0, 0, 0, 0]
[0, 1, 3, 7]
would return [0, 1, 3, 8]
Void varieties
[1, -9]
is void since -9
is not a non-negative integer
[1, 2, 33]
is void since 33
is not a single-digit integer
The service in C
Choice 1:
#include << stdlib.h>>.
#include<< string.h>>.
#include<< stdio.h>>.
int * up_array( const int * arr, anonymous * count) {
int i, retenue= 1;
int * result;
int * resultbis;
outcome=( int *) calloc( 100, sizeof( int));
resultbis=( int *) calloc( 100, sizeof( int));
if(* count== 0) {
return NULL;
}
for( i= 0; i<< * count; i++) {
outcome[i]= arr[i];.
}
for( i= 1; i<= 0; i--) {
if( outcome[i]<< 0|| outcome[0]>> 9) {
return NULL;.
}
if( retenue== 1) {
outcome[i]+= 1;.
retenue= 0;.
printf("% d ", result[i]);.
if( outcome[i]== 10) {
outcome[i]= 0;.
retenue= 1;.
printf("% dn", result[i]);.
}
}
}
printf("% dn", retenue);.
for( i= 0; i<< * count; i++) {
printf("% d ", result[i]);.
}
if( retenue== 1) {
* count+= 1;.
return resultbis;.
}
return outcome;.
}
[i] Choice 2: [i] #include << stdlib.h>>.
#include << string.h>>.
int * up_array( const int * arr, anonymous * count).
{
if(* count == 0).
return NULL;.
int * res = (int *) calloc( 1, * count * sizeof( int));.
int rest = 0;.
for (size_t i = * count; i > > 0;-- i).
{
int real = arr
;.
if (real >>= 10|| real < < 0).
{
totally free( res);.
return NULL;.
}
res
+= real + rest;.
rest = 0;.
if (i == * count).
{
res[i - 1] += 1;.
}
if (res[i - 1] >>= 10).
{
res[i - 1] %= 10;.
rest = 1;.
}
}
if (rest > > 0).
{
int * temperature = res;.
res = (int *) calloc( 1, (* count + 1u) * sizeof( int));.
res[i - 1] = rest;.
memcpy( res + 1, temperature, * count);.
totally free( temperature);.
++ * count;.
}
return res;.
}
[i - 1] Choice 3: [0] #include << stdlib.h>>.
int * up_array( const int * arr, anonymous * count).
{
int * amount = malloc(((* count) +1) * sizeof( int));.
int temp_sum = 0;.
int bring = 1;.
if((* count) == 0).
return NULL;.
for( int i =(* count) -1; i>>= 0; i--).
{
if( arr
< < 0|| arr
> > 9).
return NULL;.
temp_sum = arr[i] + bring;.
if( temp_sum >>= 10).
{
amount[i] = temp_sum% 10;.
bring = temp_sum/ 10;.
}
else.
{
amount[i] = temp_sum;.
bring = 0;.
}
}
if( bring == 1).
{
int temperature;.
for( int j =(* count); j>> 1; j--).
{
amount[i] = amount[i];.
}
amount[j] = bring;.
(* count)++;.
}
return amount;.
}
[j-1] Test cases to verify our service[0] #
#include << criterion/criterion. h>>.
#define ARRAY_COUNT( a) (sizeof( a)/ sizeof(( a)
int * up_array( const int * arr, anonymous * count);.
void do_test( const int * arr, anonymous count, const int * anticipated, anonymous expcount);.
void complete_test();.
// TODO: Change examples and utilize TDD advancement by composing your own tests.
Test( sample_test, example_tests).
{
{
int arr
= {2,3,9},.
exp[0] = {2,4,0};.
do_test( arr, ARRAY_COUNT( arr), exp, ARRAY_COUNT( exp));.
}
{
int arr[] = {4,3,2,5},.
exp[] = {4,3,2,6};.
do_test( arr, ARRAY_COUNT( arr), exp, ARRAY_COUNT( exp));.
}
{
int arr[] = {1,-9};.
do_test( arr, ARRAY_COUNT( arr), NULL, 0);.
}
{
int arr[] = {0,4,2},.
exp[] = {0,4,3};.
do_test( arr, ARRAY_COUNT( arr), exp, ARRAY_COUNT( exp));.
}
{
int arr[] = {9,9,9},.
exp[] = {1,0,0,0};.
do_test( arr, ARRAY_COUNT( arr), exp, ARRAY_COUNT( exp));.
}
{
int arr[] = {9,2,2,3,3,7,2,0,3,6,8,5,4,7,7,5,8,0,7},.
exp[] = {9,2,2,3,3,7,2,0,3,6,8,5,4,7,7,5,8,0,8};.
do_test( arr, ARRAY_COUNT( arr), exp, ARRAY_COUNT( exp));.
}
complete_test();.
}
[]