Thread: How to find the last value in an array

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    2

    How to find the last value in an array

    Hi experts,
    I am new to C, i need to insert an element in the middle of an array,for that i thought a logic that after finding the last value in an array i can shift the value and after that i can insert the new value for this i have to find the last value in an arry,how can i find the last value in an array.

    example

    int arr[30]



    in this i have to insert the element after the 4 location,for that i thought to find the last value in an array

    ex the last value in the arr[15].


    thanks and regards,
    kanimozhi.m

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    So the array is obviously not full, right? If the array is full the last element is obviously arr[29]. So I assume its not full.

    Since your using a primitive type ("int"), there is no "NULL" value. Therefore you have to decide on some "delimiter" to use. So if your array should only hold positive numbers, then the delimiter should be a negative number, say -1. So you would create an array and initialize all values to -1, then use the array.

    If you cant modify the array, then you need to keep a counter to say how many values are actually being used.

    If neither of these are possible, then it cannot be done. For example, if the array can store any value, then you cannot have a delimiter. So the only way is to use a counter. If you cant use a counter, then its not possible.

    So let us know which case your situation applies to.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    There is no "finding" the last item, just as there is no "finding" your hand. It's just there where it always is, at the end of your arm. You don't have to look at several points along your arm to find it, you can just look straight at it.
    An array of [30] items has item [29] as the last one. An array of [N] items has item [N-1] as the last one.

    If that's not what you're after then you have to use your own conventions such as using a certain variable to remember how many of the items have something of interest in them.
    Last edited by iMalc; 12-05-2009 at 09:45 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    Code:
    #include <iostream>
    
    int
    main (int argc, char * argv[])
    {
      int x[] = {1,2,3,4,5};
    
      std::cout << *((*(&x+1))-1) << std::endl;
    }
    last element printed, awesome.

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by edoceo View Post
    Code:
    #include <iostream>
    
    int
    main (int argc, char * argv[])
    {
      int x[] = {1,2,3,4,5};
    
      std::cout << *((*(&x+1))-1) << std::endl;
    }
    last element printed, awesome.

    have an extra set of parens there, no big deal though

    Code:
       std::cout << "6: " << *(*(&x+1)-1) << std::endl;

    I know this works, but real tricky code. Can you help explain how this works?

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I hope he's just messing with the OP's brain.

    But basically the idea is to move a pointer by sizeof(x) (where x is int[5]) and then decrement the pointer by sizeof(int).

    Not entirely sure if there's also some undefined behavior involved.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    Quote Originally Posted by slingerland3g View Post
    have an extra set of parens there, no big deal though

    Code:
       std::cout << "6: " << *(*(&x+1)-1) << std::endl;

    I know this works, but real tricky code. Can you help explain how this works?
    I tried explaining what was going on with the "extra set of parens". A int(*)[5] incremented by one, dereference this new pointer to look at the first element of this imaginary array, then move back one element. Simple as pie.
    Last edited by edoceo; 12-08-2009 at 02:09 PM.

  8. #8
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by anon View Post
    I hope he's just messing with the OP's brain.

    But basically the idea is to move a pointer by sizeof(x) (where x is int[5]) and then decrement the pointer by sizeof(int).

    Not entirely sure if there's also some undefined behavior involved.

    Honestly that messed with my brain, but I see the picture through the murkiness of pointer arithmetic.The jump to 20bytes got me. Ive testing with adding up to 3 more int's and that still gets the last element, still cool how that works.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you want to give people a chance to understand what it is doing, you might also try

    Code:
    #include <iostream>
    
    int main()
    {
      int x[] = {1,2,3,4,5};
    
      std::cout << x[sizeof(x)/sizeof(*x) - 1] << std::endl;
    }
    And if you want to make it even cleaner, and at the same time let the compiler ensure that you are in fact dealing with an array (and not an array that has decayed to a pointer and thus lost track of the array's size):

    Code:
    #include <iostream>
    
    template <class T, unsigned N>
    unsigned array_size(const T (&)[N])
    {
        return N;
    }
    
    int main()
    {
          int x[] = {1,2,3,4,5};
          std::cout << x[array_size(x) - 1] << std::endl;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    If you're using templates, you may as well just write a template that returns the N-1th element, and be done with it.

    But I suspect that none of this will help with the OP.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by anon View Post
    If you want to give people a chance to understand what it is doing, you might also try

    Code:
    #include <iostream>
    
    int main()
    {
      int x[] = {1,2,3,4,5};
    
      std::cout << x[sizeof(x)/sizeof(*x) - 1] << std::endl;
    }
    And if you want to make it even cleaner, and at the same time let the compiler ensure that you are in fact dealing with an array (and not an array that has decayed to a pointer and thus lost track of the array's size):

    Code:
    #include <iostream>
    
    template <class T, unsigned N>
    unsigned array_size(const T (&)[N])
    {
        return N;
    }
    
    int main()
    {
          int x[] = {1,2,3,4,5};
          std::cout << x[array_size(x) - 1] << std::endl;
    }


    I really had to read up on that as I was stuck on the array decay to pointer and the unique way sizeof() functions with that. This thread cleared that up for me.

    Newbie question about 'sizeof' and arrays

    Ok I'm done here...

  12. #12
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    sizeof - Wikipedia, the free encyclopedia

    "sizeof can only be applied to "completely" defined types. With arrays, this means that the dimensions of the array must be present in its declaration, and that the type of the elements must be completely defined."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to find the middle element of a bulk array?
    By void_mehboob in forum C Programming
    Replies: 4
    Last Post: 04-19-2009, 11:37 PM
  2. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  3. Easiest way to find the max value stored in an array
    By criticalerror in forum C++ Programming
    Replies: 14
    Last Post: 01-22-2004, 03:35 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM