Easy way of saving every nth element of array?

This is a discussion on Easy way of saving every nth element of array? within the C Programming forums, part of the General Programming Boards category; Hi all, What is the easiest way of doing the "subject"? Thank you...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    5

    Easy way of saving every nth element of array?

    Hi all,

    What is the easiest way of doing the "subject"?

    Thank you

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    5
    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

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by drunkuilled View Post
    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.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    5
    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

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    the easiest way is with a loop.
    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;
            }
    }
    avoiding for loops is kind of dumb, but you can use while loops instead.
    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++;
        }
    }

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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]);
    }
    Seems much easier. Same change to the while-loop applies.

    --
    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.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    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]);
    }

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Meldreth View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 19
    Last Post: 07-20-2007, 02:46 AM
  2. Delete Samllest Array Element
    By ptysell in forum C Programming
    Replies: 5
    Last Post: 11-22-2004, 07:27 PM
  3. Sorting a 2-dimensional array
    By kmoyle73 in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2004, 02:54 PM
  4. Deleting an element from an array
    By Matt13 in forum C Programming
    Replies: 7
    Last Post: 11-19-2003, 04:42 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21