I'm asking for a stream of numbers and want to store each number in an array. The number of #'s is variable, so I want to check how many #'s a user has entered so I can allocate the necessary amount of memory for my array.
Thanks
I'm asking for a stream of numbers and want to store each number in an array. The number of #'s is variable, so I want to check how many #'s a user has entered so I can allocate the necessary amount of memory for my array.
Thanks
You can either allocate a lot of memory and shoot for the best, or create a queue if you need to be more exact.
You can't put the cart before the horse. You have to plan in advance for some number of responses, and then act accordingly when that number is exceeded. That number could be 1 or 50 million - you don't know. My point being is that whatever you choose, when you get one more response than you planned for, you'll have to regroup.
The lazy person's choice might be to plan for 100 responses. If that or fewer are received, you are done. If more than 100 are received, then you'll need to pull out the heavy artillery and get some storage from the heap and create a new array that is larger than your initial pick (by some degree larger - you're choice).
Another way might be to get a hunk of heap from the get-go, and instead of defining it as an array, just treat it as an array, and when it's not big enough any more, realloc() that hunk to some amount larger than the first, and keep going with adding more entries.
Mainframe assembler programmer by trade. C coder when I can.
Mmm.. makes sense, but how do I know I've read all the entries?
for instance:
numbers: 1 2 3 4 5 6 7 8
code:
a loop?...
scanf("%d", store in array);
...
How do I know when to stop?
Thanks
The read routine you use will set the EOF (end of file) indicator, and you will test it and act accordingly.
Mainframe assembler programmer by trade. C coder when I can.
The queue idea that I mentioned would be something more like:
Example:
Code:#include <stdlib.h> struct item { int value; struct item *next; }; int push(struct item **q, int value) { struct item *node = malloc(sizeof(*node)), *tmp; if(!node) return EXIT_FAILURE; node->value = value; node->next = 0; if(*q) { for(tmp = *q; tmp->next; tmp = tmp->next) ; tmp->next = node; } else *q = node; return EXIT_SUCCESS; } int pop(struct item **q, int *r) { struct item *tmp; if(!r || !q) return EXIT_FAILURE; if(!*q) return EXIT_FAILURE; tmp = *q; *q = (*q)->next; *r = tmp->value; free(tmp); return EXIT_SUCCESS; } int main(int argc, char **argv) { struct item *q = 0; int i; for(i = 1; i < argc; i++) push(&q, atoi(argv[i]); while(pop(&q, &i) == EXIT_SUCCESS) printf("%d ", i); return EXIT_SUCCESS; }
Last edited by master5001; 10-28-2008 at 01:12 PM.
Great, thanks
I dunno who that was directed at, but I will give it to you, Todd. You were a little more distinct than I was on this one.
Let's give it to Salem. I think Salem needs it.
Mainframe assembler programmer by trade. C coder when I can.
True. Salem takes more crap than any of us. Todd, did you read the post within my link. I am so going to reference it everytime someone gets overly frustrated. At least I got that particular student's attitude turned around.
I wish somedays I could just "give up" on a problem I'm working on at work!!
Mainframe assembler programmer by trade. C coder when I can.