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
19,421 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
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
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
5,976 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,416
>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
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
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
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