Thread: How to find the middle element of a bulk array?

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    1

    Question How to find the middle element of a bulk array?

    Hi,

    this is an interesting question a professor asked me in a viva exam.

    How can I get the middle element of a bulk array without using a division operator?

    It means if my array is of size 100, i will get the 50th element without using the following code snippets .....
    Code:
    #include<stdio.h>
    
    main()
    {
     int anArr[100]={5};
     printf("%d\n",anArr[(sizeof(anArr)/2)/2]); // using division operator
     return 0;
    }
    please suggest me how to get the middle element without using a division operator.
    If someone know more then a solution please do let us know ....

    Thanks in advance .............
    Last edited by void_mehboob; 04-18-2009 at 03:15 AM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    What an amazingly bad question.

    First of all, there is no middle element in an array with 100 elements. There needs to be an odd number of elements for there to be a middle element. The example divides by 2 twice. Why?
    Now, for the worst part, there are loads of ways to do this. Not dividing but casting to a double, multiplying by a half, converting it to an int (don't do this, this would be the worst solution). Or by shifting the number to do the same as divide by two (I'm not going to give you the answer, so find it out by yourself). Or by creating pointers to the first and last element and increasing/decreasing them until they point to the same element.

    Whatever you want...

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    half1 is set equal to the total
    and half2 = 0

    Now, while half1 > half2, subtract one from half1, and add one to half2.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Move towards the middle from each end, and stop when meeting up or crossing over, is one simple way.
    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"

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Quote Originally Posted by EVOEx View Post
    The example divides by 2 twice. Why?...
    Because the example obfuscates the need to scale the result of sizeof() down by the element size.

    (sizeof(anArr)/2)/2 should be better expressed as

    sizeof(anArr) / sizeof(int) / 2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  2. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  3. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  4. Arrays, how to find if some number is the element of an array.
    By InvariantLoop in forum C++ Programming
    Replies: 14
    Last Post: 03-18-2006, 02:43 AM
  5. finding the element number in an array
    By tommy69 in forum C Programming
    Replies: 7
    Last Post: 04-02-2004, 04:26 AM