Hi all,
What is the easiest way of doing the "subject"?
Thank you
Hi all,
What is the easiest way of doing the "subject"?
Thank you
What do you mean? You want to copy every nth element of an array into another array? A for-loop that steps n, with "new_array[j++] = old_array[i]" would do the job, I suppose?
If that is not what you want, then describing exactly what you want to do is probably needed.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Hey, Mats,
I think about easy way of writing every nth element of array into the file using fwrite for example, possibly avoiding for loops
Cheers,
Serge
You can not do that without loops. Writing it to a file is, otherwise, even easier, as you just use the loop with the index variable stepping n elements at a time. Although a drawback is that you call fwrite with a lot of (relatively) small writes - it may be a better idea to actually use a temporary array of (array_size / n + 1) elements, and write that as a single write. If the array is VERY large, then doing a small batch in a temporary array would also work - but that means two loops and a bit of extra fiddling around.
--
Mats
Last edited by matsp; 02-04-2009 at 07:23 AM.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Thanks, Mats
That's exactly the problem - the array is huge and i have to save it every step in the external huge loop.
i'll do it with temporary array
Thanks again
the easiest way is with a loop.
avoiding for loops is kind of dumb, but you can use while loops instead.Code:#include <stdio.h> #include <stdlib.h> void save_nth(FILE *fp, int *a, int sz, int nth); int main() { int a[] = {1,2,3,4,5,6,7,8,9,0}; save_nth(stdout, a, 10, 3); return EXIT_SUCCESS; } void save_nth(FILE *fp, int *a, int sz, int nth) { int i, k = 1; for (i = 0; i < sz; i++) if (k++ == nth) { fprintf(fp, "%d\n", a[i]); k = 1; } }
Code:void save_nth(FILE *fp, int *a, int sz, int nth) { int i = 0, k = 1; while (i < sz) { if (k++ == nth) { fprintf(fp, "%d\n", a[i]); k = 1; } i++; } }
Seems much easier. Same change to the while-loop applies.Code:void save_nth(FILE *fp, int *a, int sz, int nth) { int i; for (i = 0; i < sz; i += nth) fprintf(fp, "%d\n", a[i]); }
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
if it's so much easier why is your output different from mine? to get the same result you have to start on the nth index instead of 0 and because nth is 1 based you can't forget to add 1 to the initialization but not the update.
Code:void save_nth(FILE *fp, int *a, int sz, int nth) { int i; for (i = nth + 1; i < sz; i += nth) fprintf(fp, "%d\n", a[i]); }
Obviously, the initial value for i is depending on where in the array is actually going to do. And the third element in the array is a[nth-1], not a[nth+1] - that's the fifth element.
But counting up two variables and having an if-statement is not the simplest way to do that. Admittedly, it doesn't make much difference in this case, as the fprintf() undoubtedly adds a huge amount to the loop. But say we were to SUM each nth element as an integer, and the array is LONG, then adding the extra 5-6 instructions inside the loop will probably make it take 5-10x longer in optimized code. Again, not important if you do it ONCE in an application - but if you need to do it many times (or for a HUGE array), then it may become important.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.