954,173 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to increase the size of an array?

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.

varunrathi
Light Poster
41 posts since Aug 2004
Reputation Points: 10
Solved Threads: 1
 

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 :)

cscgal
The Queen of DaniWeb
Administrator
19,421 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 

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?

Asif_NSU
Posting Whiz
353 posts since Apr 2004
Reputation Points: 113
Solved Threads: 3
 

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

kc0arf
Posting Virtuoso
Team Colleague
1,937 posts since Mar 2004
Reputation Points: 121
Solved Threads: 57
 

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

There is a little entry in there ...
[php]
// 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 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;
[/php]

vegaseat
DaniWeb's Hypocrite
Moderator
5,976 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,416
 
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!

1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 

>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;
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

harshchandra
Junior Poster in Training
68 posts since Nov 2004
Reputation Points: 7
Solved Threads: 1
 
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?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

resat.1907
Newbie Poster
1 post since Feb 2012
Reputation Points: 10
Solved Threads: 0
 
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.

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

Post: Markdown Syntax: Formatting Help
You