how to increase the size of an array?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2004
Posts: 41
Reputation: varunrathi is an unknown quantity at this point 
Solved Threads: 1
varunrathi's Avatar
varunrathi varunrathi is offline Offline
Light Poster

how to increase the size of an array?

 
0
  #1
Dec 22nd, 2004
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.
"Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,056
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 127
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: how to increase the size of an array?

 
0
  #2
Dec 22nd, 2004
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
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 2
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: how to increase the size of an array?

 
0
  #3
Dec 22nd, 2004
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?
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 1,620
Reputation: kc0arf is a jewel in the rough kc0arf is a jewel in the rough kc0arf is a jewel in the rough 
Solved Threads: 51
Team Colleague
kc0arf kc0arf is offline Offline
Posting Virtuoso

Re: how to increase the size of an array?

 
0
  #4
Dec 22nd, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,113
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 944
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: how to increase the size of an array?

 
0
  #5
Dec 22nd, 2004
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<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;
[/php]
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: how to increase the size of an array?

 
0
  #6
Jan 1st, 2005
  1. int *array;
  2.  
  3. array = (int*)new int[size]; // re-size
  4. 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!
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,844
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 752
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: how to increase the size of an array?

 
0
  #7
Jan 2nd, 2005
>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:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int *resize_array(int *a, size_t new_size)
  5. {
  6. int *save;
  7.  
  8. save = realloc(a, new_size);
  9. if (save == NULL) {
  10. fprintf(stderr, "Memory exhausted\n");
  11. exit(EXIT_FAILURE);
  12. }
  13.  
  14. return save;
  15. }
  16.  
  17. int main(void)
  18. {
  19. int *a;
  20. int i = 0;
  21. int size;
  22.  
  23. /* Allocate a new array */
  24. size = 5;
  25. a = malloc(size * sizeof *a);
  26. for (i = 0; i < size; i++)
  27. a[i] = i;
  28. for (i = 0; i < size; i++)
  29. printf("%-2d", a[i]);
  30. printf("\n");
  31.  
  32. /* Double the size of the array */
  33. size *= 2;
  34. a = resize_array(a, size * sizeof *a);
  35. for (i = 0; i < size; i++)
  36. a[i] = i;
  37. for (i = 0; i < size; i++)
  38. printf("%-2d", a[i]);
  39. printf("\n");
  40.  
  41. /* Destroy the array */
  42. free(a);
  43.  
  44. return 0;
  45. }
Alternatively, you could build a linked list of arrays such that when the array for a node is full, you add a new node:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct node {
  5. int array[5];
  6. struct node *next;
  7. };
  8.  
  9. int main(void)
  10. {
  11. struct node *list = NULL;
  12. struct node *curr = NULL;
  13. int size = 0;
  14. int i;
  15.  
  16. for (i = 0; i < 23; i++) {
  17. if (size % 5 == 0) {
  18. if (list == NULL) {
  19. list = malloc(sizeof *list);
  20. if (list == NULL)
  21. break;
  22. list->next = NULL;
  23. curr = list;
  24. }
  25. else {
  26. curr->next = malloc(sizeof *curr);
  27. if (curr->next == NULL)
  28. break;
  29. curr = curr->next;
  30. curr->next = NULL;
  31. }
  32. }
  33. curr->array[size++ % 5] = i;
  34. }
  35.  
  36. for (curr = list; curr != NULL; curr = curr->next) {
  37. if (curr->next == NULL) {
  38. for (i = 0; i < size % 5; i++)
  39. printf("%-3d", curr->array[i]);
  40. }
  41. else {
  42. for (i = 0; i < 5; i++)
  43. printf("%-3d", curr->array[i]);
  44. }
  45. }
  46. printf("\n");
  47.  
  48. return 0;
  49. }
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 68
Reputation: harshchandra is an unknown quantity at this point 
Solved Threads: 1
harshchandra harshchandra is offline Offline
Junior Poster in Training

Re: how to increase the size of an array?

 
0
  #8
Jan 7th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,844
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 752
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: how to increase the size of an array?

 
0
  #9
Jan 8th, 2005
Originally Posted by harshchandra
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?
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC