i am working on a program to find the factorial of a number of any length. i have used an integer array to store the digits of the result. the problem is that i am unable to store more than 32000 digits in the array. if i give a larger value then it gives an error message "array size too large". i`ve tried using an character array .in case of the character array it doesn`t give any error but on running the computer hangs.
is there any way by which i can store more no. of digits.

Perhaps you can try storing the result in a linked list instead of in an array. Linked lists can be of variable size. Be sure to also take into consideration the amount of RAM of the computer running your program!

For help, visit the DaniWeb code snippet library (www.daniweb.com/code/) and do a search for "linked list" and something should come up :)

to varunrathi,

I believe u need to be a little more specific with ur problem. We also need to know a few more things...

1. U r using static arrays right? Why dont u try dynamic memory allocation with pointers? Even then the amount of memory is always limited by ur system. It is rather strange and probably a wrong approach to use an array of that big size.

2. What is the largest number ur program can find factorial of? factorial of 5000 requires 16326 digits, how many digits does the factorial of ur largest number require?

3. How far do u want to go, u want to find factorial of "large numbers" or a number of "any length"? I know ur title says "any length" but then again how far would u wanna go? upto 40000, 1 million, how far?

Hello,

I too would go with a dynamic (pointer) linked list to achieve your desire of the "infinite calculator". The only other way around it would be to declare that your program can process x amount of digits, and if the array fills up, send an error message to the user.

That is what my calculator does, at least.

From the pure curious viewpoint, you might also want to put in how long the computer took computing this information, and how many digits were involved.

Christian

Look at:
http://www.daniweb.com/code/snippet105.html

There is a little entry in there ...

// how many elements can a vector hold?
  // a huge number, actually until memory runs out!
  // well, let's test it to a million elements ...
  vector<int> kV;
  cout << "loading an integer vector with numbers 0 to 999999 ...\n";
  for(k = 0; k < 1000000; k++)
    kV.push_back(k);
  cout << "content of element 123456 = " << kV[123456] << endl;  
  cout << "content of element 999999 = " << kV[999999] << endl;
int *array;

array = (int*)new int[size]; // re-size
delete [] array; // when you finish with it

This is another easy way to make arrays of any size. Easier but not as powerful as a linked list - but STL (what vegaseat just used) could be even easier still - but I dont know enough about it! If you want to increase an array AND SAVE THE CONTENTS then dont use the above code as it (obviously) deletes the contents and changes the pointer to a new array!

>is there any way by which i can store more no. of digits.
Are you using C or C++? If C++ then an array should be avoided in favor of a standard container that grows dynamically, such as std::vector. If C then you need to simulate your own dynamic array with pointers and memory allocation:

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

int *resize_array(int *a, size_t new_size)
{
  int *save;

  save = realloc(a, new_size);
  if (save == NULL) {
    fprintf(stderr, "Memory exhausted\n");
    exit(EXIT_FAILURE);
  }

  return save;
}

int main(void)
{
  int *a;
  int i = 0;
  int size;

  /* Allocate a new array */
  size = 5;
  a = malloc(size * sizeof *a);
  for (i = 0; i < size; i++)
    a[i] = i;
  for (i = 0; i < size; i++)
    printf("%-2d", a[i]);
  printf("\n");

  /* Double the size of the array */
  size *= 2;
  a = resize_array(a, size * sizeof *a);
  for (i = 0; i < size; i++)
    a[i] = i;
  for (i = 0; i < size; i++)
    printf("%-2d", a[i]);
  printf("\n");

  /* Destroy the array */
  free(a);

  return 0;
}

Alternatively, you could build a linked list of arrays such that when the array for a node is full, you add a new node:

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

struct node {
  int array[5];
  struct node *next;
};

int main(void)
{
  struct node *list = NULL;
  struct node *curr = NULL;
  int size = 0;
  int i;

  for (i = 0; i < 23; i++) {
    if (size % 5 == 0) {
      if (list == NULL) {
        list = malloc(sizeof *list);
        if (list == NULL)
          break;
        list->next = NULL;
        curr = list;
      }
      else {
        curr->next = malloc(sizeof *curr);
        if (curr->next == NULL)
          break;
        curr = curr->next;
        curr->next = NULL;
      }
    }
    curr->array[size++ % 5] = i;
  }

  for (curr = list; curr != NULL; curr = curr->next) {
    if (curr->next == NULL) {
      for (i = 0; i < size % 5; i++)
        printf("%-3d", curr->array[i]);
    }
    else {
      for (i = 0; i < 5; i++)
        printf("%-3d", curr->array[i]);
    }
  }
  printf("\n");

  return 0;
}

There is no choice to increase the size of the array .....instead u can take use of calloc or malloc function to allocate the memory space dynamically .... or in these case the best would be to use a Linked list for storing the numbers.....by doing this u can take input as long long type of ineger ....something like 1245432342342423 which is impossible to store in any data types available in C .... for n implementatin of singly linked list u can try this link http://www.daniweb.com/code/snippet91.html

There is no choice to increase the size of the array .....instead u can take use of calloc or malloc function to allocate the memory space dynamically .... or in these case the best would be to use a Linked list for storing the numbers.....by doing this u can take input as long long type of ineger ....something like 1245432342342423 which is impossible to store in any data types available in C .... for n implementatin of singly linked list u can try this link http://www.daniweb.com/code/snippet91.html

Cool, you reiterated what everyone already said. So, what value were you expecting to add with this post? Or are you just trying to boost your post count?

Integer size is generally 4 bytes however char is only 1 byte your error is caused by thisissue in my opinion

Integer size is generally 4 bytes however char is only 1 byte your error is caused by thisissue in my opinion

You realize this thread is 8 years old.

This article has been dead for over six months. Start a new discussion instead.