3

I have an assignment that requires me to first set up integer arrays to store arbitrarily large numbers. By using each element of an int array, I can hold one digit per element because int variable types are 32 bits and can only reach up to about 2 billion before failing. I know that there are other libraries using BigInt but I wanted to make my own arrays instead.

I tested my code out, but it doesn't seem to be accepting values past 9 digits until it starts to fail.

int * toArray(int, int);
int main()
{
    int n, *res, counter, i;
    printf("Input: ");
    scanf("%d", &n);
    while(n>0){
         n/=10;
         counter++;
    }
    res = toArray(n, counter);
    for(i=1;i<=counter;i++){
         printf("%d", *(res)+i);
    }
    printf("\n");
}   

int * toArray(int n, int counter)
{
    int i;
    int *numberArray = malloc(sizeof(int) * counter);
    for(i=0; i< counter; i++)
    {   
         numberArray[i] = n % 10;
    }
    return numberArray;
}

I want to be able to accept close to twenty digits at the very least. I know this can be done with int arrays, although char arrays (or strings) are also a possibility. But I wanted to use int arrays instead. Any help in understanding why it fails around 9 digits and suggestions for going past that would be very much appreciated. Thank you.

3
  • array indices always start at 0. Why do you use i=1, i<=counter for printing? Commented Nov 4, 2012 at 18:13
  • side note: you never free your integer array created in toArray. You should fix that. Commented Nov 4, 2012 at 18:16
  • When you count the digits, you do n /= 10; while n > 0, so when you call res = toArray(n, counter);, your number n is 0. You need a copy for the original value if you do it thus. And in toArray, you never modify n, so all digits get the same value n % 10. Commented Nov 4, 2012 at 18:54

2 Answers 2

4

The problem is that you are reading an int from keyboard

scanf("%d", &n);

so therefore no matter how many digits you enter, you still will only get 9 digits.

In order to be able to enter an arbitrary number you would have to read it as a string instead, then convert it to your int array.

EDIT:

this way (for instance) would allow for 20 digits

  char ch;
  int digits[20];
  int i = 0;
  do
  {
    ch = fgetc(stdin);
    if ( isdigit(ch) )
    {
      digits[i++] = ch - 48; // convert ASCII to int
    }
  }
  while (isdigit(ch) && i < sizeof(digits)/sizeof(digits[0]));
Sign up to request clarification or add additional context in comments.

4 Comments

I'm kind of confused by the fundamentals in this problem. Since char is smaller than int, shouldn't a string end up displaying even fewer digits? It felt like since I'd have to convert to int arrays, it would be more convenient to bypass strings in the first place.
Ahh I see what you mean by my very first scanf function. That makes perfect sense. But wouldn't there still be a way to just use int arrays? If there is a way to input individual digits per cell inside the array, that would get past the issue of scanf("%d", &n); right? Although I do want to input one number at once as opposed to digit ENTER digit ENTER etc. If I used a string with gets(), would that be able to solve for the whole input issue? Sorry if this is all over the place.
you could use another function to have more control of the input, e.g. fgetc and read one character at the time then converting it to an int.
Thank you, I ended up solving it yesterday
0
#include<stdio.h>
#include<stdlib.h>
int *toArray(long long int n, int counter); int main() {
long long int n=0,copy_of_n=0;
int counter=0, i=0;
int *res=NULL;
printf("Input: ");
scanf("%lld", &n);//Refer note in the answer
copy_of_n=n;//see the while loop why use this.
while(n>0){
     n/=10;//Good, but you are not storing it. copy_of_n does that.
     counter++;//You didn't initialize this
}

res=toArray(copy_of_n, counter);
for(i=counter-1;i>=0;i--){//This ensures you get the numbers in a proper order and since index starts from 0 in the toArray function
     printf("%d", res[i]);
}
printf("\n");
free(res);//You forgot this
return 0; }   

int *toArray(long long int n, int counter) {
  int i=0;
  int *numberArray = malloc(sizeof(int) * counter);
  memset(numberArray,0x00,counter*sizeof(int));//Good to do this
  //Check for memory allocation
  for(i=0; i< counter; i++)
  {   
     numberArray[i] = n % 10;
     n=n/10LL;//You have to remove the digits too
  }
  return numberArray; }

NOTE: Read in a while loop the integers in small parts because long long has limits of how many digits it can store. Another approach would be to store in a string and send split the string into exactly the max. no. of digits that can be stored in long long in the n variable and send it to the function, part by part.

long long size is implementation dependent, hence ensure you check the max. no. of digits it can accomodate.(It will ofcourse bypass your 9-digit limit as asked in question)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.