0

This is the first time that I use Stack Overflow to make a question. I created this code to divide the digits of a number in arrays:


#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int *getArray(int number);

int main(void)
{  
  int *x = getArray(1234);
  printf("%i", x[0]);
}
  
int *getArray(int number)
{
  int n = floor(log10(number)) + 1;
  int *receive = calloc(n, sizeof(int));
  
  for(int i=0; i<n; i++, number/=10)
  {
    receive[i] = number%10;
  }
  return receive;
}

It worked very well for integers but I tried to use it for large numbers and it didn’t work (I just changed some ‘int’s to ‘double’):


#include <stdio.h>
#include <math.h>
#include <stdlib.h>

double *getArray(double number);

int main(void)
{  
  double *x = getArray(100);
  printf("%f", x[0]);
}
  
double *getArray(double number)
{
  int n = floor(log10(number)) + 1;
  double *receive = calloc(n, sizeof(double));
  
  for(int i=0; i<n; i++, number/=10)
  {
    receive[i] = number%10;
  }
    
  return receive;
}

Does anyone know what I’m doing wrong? Thanks.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 2
    The operator `%` is not defined for `double`s. `double` is a floating point type, but you need an integer type. Larger integer types are `long` and `long long`. – Eugene Sh. Sep 16 '20 at 20:00
  • See also `man fmod` for a function that can be used to determine the modulo (remainder) for a floating point number. – David C. Rankin Sep 16 '20 at 20:05

0 Answers0