Cómo sumar 1 al valor de cada matriz en C


El reto

Dada una matriz de enteros de cualquier longitud, devuelva una matriz que tenga 1 agregado al valor representado por la matriz.

  • la matriz no puede estar vacía
  • solo se permiten números enteros no negativos de un solo dígito

Retorno nil (o el equivalente de su idioma) para entradas no válidas.

Ejemplos:

Matrices válidas

(4, 3, 2, 5) volvería (4, 3, 2, 6)
(1, 2, 3, 9) volvería (1, 2, 4, 0)
(9, 9, 9, 9) volvería (1, 0, 0, 0, 0)
(0, 1, 3, 7) volvería (0, 1, 3, 8)

Matrices no válidas

(1, -9) no es válido porque -9 es no es un entero no negativo

(1, 2, 33) no es válido porque 33 es no es un entero de un solo dígito

La solución en C

Opción 1:

#embody <stdlib.h>
#embody<string.h>
#embody<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;
}

Opcion 2:

#embody <stdlib.h>
#embody <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;
}

Opción 3:

#embody <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; 
}

Casos de prueba para validar nuestra solución

#embody <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: Exchange examples and use TDD improvement by writing your personal 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();
}

Related Articles

Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Same Category

spot_img

Stay in touch!

Follow our Instagram