Why does 12 - 6 = 7? (Arrays)

18 replies
--------------------------------------------------
#arrays
  • Profile picture of the author mojojuju
    You just need some sleep.
    Signature

    :)

    {{ DiscussionBoard.errors[5127071].message }}
  • Profile picture of the author laluman
    This depends on the language you are using some start the number with 0 so 6 would = the 7th number not 6!
    {{ DiscussionBoard.errors[5127276].message }}
  • Profile picture of the author eminc
    Here basic principles of a arithmetic progression will be applied. A little bit of mathematical explanation here

    For a arithmetic progression, you will calculate the Nth element using the formula

    An = a1 + (n-1)*d -------(1)
    , where a1 is the first element and d is the common difference between two elements.

    If the list were ascending, i.e. 1, 2, 3, 4... then the 6th element will be

    A6 = 1+ (6-1) * 1 [d = 1, n=6]
    A6 = 7

    But in the case you stated above, the list is descending and that makes the difference between two elements of a progression -1, and the first element is 12. Thus


    A6 = 12 + (6-1)(-1) [a1 = 12, d = -1, n = 6]
    A6 = 12 - 5
    A6 = 7

    Hence Proved

    Mohit
    {{ DiscussionBoard.errors[5127764].message }}
    • Profile picture of the author Terry Crim
      [DELETED]
      {{ DiscussionBoard.errors[5128317].message }}
      • Profile picture of the author eminc
        Originally Posted by Terry Crim View Post

        Your smart. Is that the math behind all arrays or just this example?
        Actually its a progression example, and not precisely related to an array. If you don't take a progression and assign a random value to array, this condition will not arise and thus formulas will not work However the condition specified by you will only arise if its a arithmetic progression.


        Mohit
        {{ DiscussionBoard.errors[5128832].message }}
  • Profile picture of the author jamesfwillett
    just like stated above you count from 0 up so once you hit 6 you are actually hitting 7 because when you count from 0 you get .
    0 , 1 , 2, 3,4,5,[6] .

    It starts at 0 because the index value tells the computer how far it needs to move away from the starting point in the array. In some programming languages, if you use an array (without giving an index number in brackets), you are really only referring to the memory address of the starting point of the array. When you reference a specific value in the array, you are telling the programming language to start at the memory address of the beginning of the array and then move up the memory address as needed.
    {{ DiscussionBoard.errors[8373925].message }}
    • Profile picture of the author Terry Crim
      [DELETED]
      {{ DiscussionBoard.errors[8384143].message }}
      • Profile picture of the author ninjawarrior
        I think that people with no understanding of IM or SEO overhearing a conversation between very knowledgeable marketers would feel that funny/sad sentiment as well. My point being, that'd be a normal reaction by non-geeks about geeky discussions among geeks (where I define "geek" here as "a person who has excessive enthusiasm for and some expertise about a specialized subject or activity").
        Originally Posted by Terry Crim View Post

        Was downtown earlier today and over heard a conversation between to self proclaimed nerds. Talking about blogs and wordpress vs some social media platform and the conversation went towards the stupidity of some technical thing wordpress does they don't like. You know how many people would actually understand what the hell they were talking about and THEY were the only ones that would EVER notice anything like that. I Just found it funny and sort of somewhat sad at the sametime. LOL
        {{ DiscussionBoard.errors[8401310].message }}
  • Profile picture of the author techwizard
    Array is based on index and it will start from 0 index and not 1 so if its 7th element then its index would be 6
    {{ DiscussionBoard.errors[8374651].message }}
  • Profile picture of the author mariahoustone
    just like stated above you count from 0 up so once you hit 6 you are actually hitting 7 because when you count from 0 you get .
    0 , 1 , 2, 3,4,5,[6] .
    ------------------------------
    Miami Beach Florida Multifamily
    {{ DiscussionBoard.errors[8399668].message }}
  • Profile picture of the author SteveJohnson
    Doesn't matter how you index your array - start with 0, start with 90 - if you have an even number of elements, there is no single 'middle' element. Let's take 12 elements, like your example - there are two elements that surround the middle: 6 & 7. Reversing the array values will always swap the values of those two keys.

    If you have an odd number of elements, like 13, reversing the array will never alter the center, or seventh, element. 13-6 = 7, and 1+6=7.
    Signature

    The 2nd Amendment, 1789 - The Original Homeland Security.

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[8409873].message }}
  • Originally Posted by Terry Crim View Post

    Math was never my favorite subject and I am probably
    the only one that finds this interesting not to mention
    I am probably only one that never noticed this.


    I have an array consisting of 12 records
    Sort order is reversed so 1 is 12th record.

    PHP Code:
    array Record  reverse count
    ----------------------------
    1:  |   12   |     12
    2
    :  |   11   |     11
    3
    :  |   10   |     10
    4
    :  |    9   |      9
    5
    :  |    8   |      8
    6
    :  |    7   |      7  <---  6th element contains 7th record  OR 7th record is 6th element in array.
    7:  |    6   |      6
    8
    :  |    5   |      5
    9
    :  |    4   |      4
    10
    : |    3   |      3
    11
    : |    2   |      2
    12
    : |    1   |      
    I just thought 6 was 6 and no matter how you divided, subtracted or added to get
    12 the halfway point between 1 and 12 was always 6.

    Had me thinking there for a while but the more you look at it the stupider this
    "puzzle" seems.. LOL

    Ok, back to your regular scheduled programming.


    - T
    six is not the mid point.

    The midpoint is 6.5

    1 thru 6 = 6
    7 thru 12 = 6
    {{ DiscussionBoard.errors[8436240].message }}
  • Profile picture of the author yukon
    Banned
    Usually programming doesn't start at 1, it starts at 0, so you have to factor in 0. In your case 12 is actually 13.
    {{ DiscussionBoard.errors[8436247].message }}
    • Profile picture of the author andrewjorgenson
      array Record reverse count
      ----------------------------
      1: | 12 | 12
      2
      : | 11 | 11
      3
      : | 10 | 10
      4
      : | 9 | 9
      5
      : | 8 | 8
      6
      : | 7 | 7 <--- 6th element contains 7th record OR 7th record is 6th element in array.
      7: | 6 | 6
      8
      : | 5 | 5
      9
      : | 4 | 4
      10
      : | 3 | 3
      11
      : | 2 | 2
      12
      : | 1 | 1

      Assuming the creator of this is using PHP, and the code for the array is like this:

      Apparently variables are removed from my post, so instead of using the dollar sign I'll use S before it.

      PHP Code:
      SmyArray = array(121110987654321)
      foreach(
      SmyArray as SmyItem )
      {
            echo 
      SmyItem;
            unset(
      SmyItem);

      Arrays start at 0, not 1.

      SmyItem[0] would be equal to the first item, SmyItem[2] would be equal to the third item.

      I'm assuming that was your question from the post, though I don't really know for sure.
      {{ DiscussionBoard.errors[8436539].message }}
  • Profile picture of the author jminkler
    Perhaps a more interesting question is - what are your trying to do? I program a lot, and you should rarely ever need to know the position of something (There are usually search functions for that) ...
    {{ DiscussionBoard.errors[8452207].message }}
  • Profile picture of the author SteveJohnson
    Originally Posted by Terry Crim View Post

    I love you guys.

    It was more of a thinking out loud thing.
    Responses not required but thanks.

    Now if I had asked which comes first chicken or the egg, now THAT would be good to know. Answers in php code only.

    Wonder how many will try to answer that one. LOL

    Love it.

    - T
    PHP Code:
    echo ( rand() % ) ? 'chicken' 'egg'
    Signature

    The 2nd Amendment, 1789 - The Original Homeland Security.

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[8531701].message }}
    • Profile picture of the author tonyla
      Arrays are contiguous memory cells.
      let a be the name of the array.
      int a[6]
      allocates 6 continous memory cells where a is the address of the first cell.

      Assume a is an array of integers , and integers are 4 bytes.
      if a or a[0] starts at memory cell 200 then a[3] is 200 + 4(3) = 212.
      so to get to the 4th element of the array we go to memory cell 212

      a[0] 200
      a[1] 204
      a[2] 208
      a[3] 212

      so starting the indexing at 0 makes for easy computation to directly access the other array elements
      {{ DiscussionBoard.errors[8602114].message }}
      • Profile picture of the author tonyla
        the chicken came first. Chickens evolved from cluck a twos . cluck a twos came from eggs of duck a doodle doos . But the question is where did duck a doodle doos come from. I know but i was sworn to secrecy very sorry but you would be freaked if i told you the evolution of duck a doodle doos.
        Hint the answer is in the way duck a doodle doos burp. Strange stuff.
        {{ DiscussionBoard.errors[8602126].message }}
        • Profile picture of the author telewarrior
          Hi,

          The actual problem is that the range is 11, not 12, which divided by 2 is 6.5 and not 6 or seven at all, which is reason enough to add the concept of zero in set of numerals. The issue is a round-up or round-down issue of using integers for a number that isn't evenly divisible by two (11).

          Consider that the usual index subscript values doesn't fix the problem if you count the starting element subscript:

          0 11
          1 10
          2 9
          3 8
          4 7
          5 6 <- sixth element forwards
          6 5 <- sixth element backwards
          7 4
          8 3
          9 2
          10 1
          11 0

          Here are the 13 elements that add and subtract correctly (12-6 = 6, 6-0=6, etc.):

          0 12
          1 11
          2 10
          3 9
          4 8
          5 7
          6 6 <- sixth element both ways
          7 5
          8 4
          9 3
          10 2
          11 1
          12 0

          This is a good example of how integer arithmetic quantises data (the range is a continuous function that is divided up by the use of integers in the example) and leads to inaccurate or misleading results. It just had to be wrong in one direction or the other regardless of where you start and is a classic programming issue for sure.

          Hope this helps,

          Brett
          {{ DiscussionBoard.errors[8623077].message }}

Trending Topics