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:
#embrace <stdlib.h>
#embrace<string.h>
#embrace<stdio.h>
int *up_array(const int *arr, unsigned *depend){
int i, retenue=1;
int *consequence;
int* resultbis;
consequence=(int*)calloc(100, sizeof(int));
resultbis=(int*)calloc(100, sizeof(int));
if(*depend==0){
return NULL;
}
for(i=0; i<*depend; i++){
consequence[i]=arr[i];
}
for(i=1; i<=*depend; i++){
resultbis[i]=0;
}
resultbis[0]=1;
for(i=*count-1; i>=0; i--){
if(consequence[i]<0||consequence[i]>9){
return NULL;
}
if(retenue==1){
consequence[i]+=1;
retenue=0;
printf("%d ", consequence[i]);
if(consequence[i]==10){
consequence[i]=0;
retenue=1;
printf("%dn", consequence[i]);
}
}
}
printf("%dn", retenue);
for(i=0; i<*depend; i++){
printf("%d ", consequence[i]);
}
if(retenue==1){
*depend+=1;
return resultbis;
}
return consequence;
}
Choice 2:
#embrace <stdlib.h>
#embrace <string.h>
int* up_array(const int* arr, unsigned* depend)
{
if(*depend == 0)
return NULL;
int* res = (int*)calloc(1, *depend * sizeof(int));
int relaxation = 0;
for (size_t i = *depend; 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 == *depend)
{
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, (*depend + 1u) * sizeof(int));
res[0] = relaxation;
memcpy(res + 1, temp, *depend);
free(temp);
++*depend;
}
return res;
}
Choice 3:
#embrace <stdlib.h>
int *up_array(const int *arr, unsigned *depend)
{
int * sum = malloc(((*depend)+1) * sizeof(int));
int temp_sum = 0;
int carry = 1;
if((*depend) == 0)
return NULL;
for(int i=(*depend)-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=(*depend);j>1; j--)
{
sum[j] = sum[j-1];
}
sum[0] = carry;
(*depend)++;
}
return sum;
}
Check circumstances to validate our answer
#embrace <criterion/criterion.h>
#outline ARRAY_COUNT(a) (sizeof(a) / sizeof((a)[0]))
int *up_array(const int *arr, unsigned *depend);
void do_test(const int *arr, unsigned depend, const int *anticipated, unsigned expcount);
void complete_test();
// TODO: Substitute examples and use TDD growth by writing your individual assessments.
Check(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();
}