| | |
how to increase the size of an array?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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.
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."
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
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

Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
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?
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?
•
•
Join Date: Mar 2004
Posts: 1,620
Reputation:
Solved Threads: 51
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
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 ...
[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]
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!
C Syntax (Toggle Plain Text)
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!
http://sales.carina-e.com
no www
no nonsense
coming soon to a pc near you! :cool:
no www
no nonsense
coming soon to a pc near you! :cool:
>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:
Alternatively, you could build a linked list of arrays such that when the array for a node is full, you add a new node:
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:
C Syntax (Toggle Plain Text)
#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; }
C Syntax (Toggle Plain Text)
#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; }
New members chased away this month: 4
•
•
Join Date: Nov 2004
Posts: 68
Reputation:
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
•
•
•
•
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
New members chased away this month: 4
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: what is fflush?
- Next Thread: inserting an element into an array in c language
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory drawing dynamic executable fflush file floatingpointvalidation fork frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer lazy license linked linkedlist linux linuxsegmentationfault list logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat openwebfoundation pattern pause pdf pointer pointers posix power problem program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming spoonfeeding stack standard strchr string strings structures student suggestions system test testautomation unix urboc user voidmain() win32api windows.h






