Possible off topic question about arrays

This is a discussion on Possible off topic question about arrays within the C Programming forums, part of the General Programming Boards category; Given an array of like Code: int a[5]= {"2", "4", "5", "8", "11"}; I don't see how to find the ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195

    Possible off topic question about arrays

    Given an array of like

    Code:
    int a[5]= {"2", "4", "5", "8", "11"};
    I don't see how to find the and remove the 5th element in the worst case, the time is 0(log n).

    Can someone either explain or point me in the right direction here?

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    I don't think you meant to have the quotes around the values.

    Also, you can't remove elements from C arrays, since they're fixed size. What exactly do you want to do?

    The common way to simulate the "removal" of an element is to shift everything right of it one place left, using memmove or a loop.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    The quotes where were a typo. The question stems from an old comphensive exam question given at UC-Berkeley.

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Well, to find the fifth element is O(1), because it's an array, so you can just go a[4] and there's the fifth element.

    And, as I explained above, you cannot remove an element from a C array, the concept doesn't exist, but you could move everything above down one. However in your example the fifth element is also the last, so that's a bit confusing too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about arrays.
    By Kelvie in forum C++ Programming
    Replies: 3
    Last Post: 09-17-2007, 05:32 AM
  2. A question concerning character arrays
    By ellipses in forum C Programming
    Replies: 3
    Last Post: 03-08-2005, 07:24 PM
  3. basic question to Arrays
    By doneirik in forum C++ Programming
    Replies: 1
    Last Post: 01-25-2005, 01:57 AM
  4. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM
  5. stupid c question re arrays
    By C. Unger in forum C Programming
    Replies: 1
    Last Post: 04-10-2002, 08:38 PM

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