The problem
Given an array of integers of any size, return an array that has 1 added to the worth represented by the array.
- the array can’t be empty
- solely non-negative, single digit integers are allowed
Return nil
 (or your language’s equal) for invalid inputs.
Examples:
Legitimate arrays
[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]
Invalid arrays
[1, -9]
 is invalid as a result of -9
 is not a non-negative integer
[1, 2, 33]
 is invalid as a result of 33
 is not a single-digit integer
The answer in C
Choice 1:
#embody <stdlib.h>
#embody<string.h>
#embody<stdio.h>
int *up_array(const int *arr, unsigned *rely){
int i, retenue=1;
int *end result;
int* resultbis;
end result=(int*)calloc(100, sizeof(int));
resultbis=(int*)calloc(100, sizeof(int));
if(*rely==0){
return NULL;
}
for(i=0; i<*rely; i++){
end result[i]=arr[i];
}
for(i=1; i<=*rely; i++){
resultbis[i]=0;
}
resultbis[0]=1;
for(i=*count-1; i>=0; i--){
if(end result[i]<0||end result[i]>9){
return NULL;
}
if(retenue==1){
end result[i]+=1;
retenue=0;
printf("%d ", end result[i]);
if(end result[i]==10){
end result[i]=0;
retenue=1;
printf("%dn", end result[i]);
}
}
}
printf("%dn", retenue);
for(i=0; i<*rely; i++){
printf("%d ", end result[i]);
}
if(retenue==1){
*rely+=1;
return resultbis;
}
return end result;
}
Choice 2:
#embody <stdlib.h>
#embody <string.h>
int* up_array(const int* arr, unsigned* rely)
{
if(*rely == 0)
return NULL;
int* res = (int*)calloc(1, *rely * sizeof(int));
int relaxation = 0;
for (size_t i = *rely; i > 0; --i)
{
int precise = arr[i - 1];
if (precise >= 10 || precise < 0)
{
free(res);
return NULL;
}
res[i - 1] += precise + relaxation;
relaxation = 0;
if (i == *rely)
{
res[i - 1] += 1;
}
if (res[i - 1] >= 10)
{
res[i - 1] %= 10;
relaxation = 1;
}
}
if (relaxation > 0)
{
int* temp = res;
res = (int*)calloc(1, (*rely + 1u) * sizeof(int));
res[0] = relaxation;
memcpy(res + 1, temp, *rely);
free(temp);
++*rely;
}
return res;
}
Choice 3:
#embody <stdlib.h>
int *up_array(const int *arr, unsigned *rely)
{
int * sum = malloc(((*rely)+1) * sizeof(int));
int temp_sum = 0;
int carry = 1;
if((*rely) == 0)
return NULL;
for(int i=(*rely)-1;i>=0;i--)
{
if(arr[i] < 0 || arr[i] > 9)
return NULL;
temp_sum = arr[i] + carry;
if( temp_sum >= 10)
{
sum[i] = temp_sumpercent10;
carry = temp_sum/10;
}
else
{
sum[i] = temp_sum;
carry = 0;
}
}
if(carry == 1)
{
int temp;
for(int j=(*rely);j>1; j--)
{
sum[j] = sum[j-1];
}
sum[0] = carry;
(*rely)++;
}
return sum;
}
Take a look at circumstances to validate our resolution
#embody <criterion/criterion.h>
#outline ARRAY_COUNT(a) (sizeof(a) / sizeof((a)[0]))
int *up_array(const int *arr, unsigned *rely);
void do_test(const int *arr, unsigned rely, const int *anticipated, unsigned expcount);
void complete_test();
// TODO: Change examples and use TDD improvement by writing your individual assessments.
Take a look at(sample_test, example_tests)
{
{
int arr[] = {2,3,9},
exp[] = {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();
}