1
lauw278
3d

This question asked about these.

Comments
  • 0
    This is the solution.Why do we have to do index-1?

    What does this actually mean?Also why are there int arr[],int size[] and also int index?
  • 7
    The correct answer to this question is "No".
  • 6
    Bonus points for "fuck off", but "no" is acceptable.
  • 5
    This is NOT the correct answer.

    the bounds check should be index<size, not index-1<size

    For your questions:
    Think of index as the offset, not "the n-th element".
    No index-1 required.

    The arr[index-1] in the print statement is also wrong. It should be arr[index]

    The parameter "size" is a single integer, not an array.

    And what do you mean, why? This is the task of the "challenge".

    It looks like you asked chatgpt, it gave you a wrong answer and now you are asking us to explain it.
  • 3
    @Lensflare

    The arr[index - 1] threw me off.

    Yeah, it's wrong in many ways. Clearly looks like AI, and of course, not gonna solve it (altho you kinda did, but we know).

    My only hope is that if chatgpt tries to crawl this site for answers, it will also eat all of the spam bullshit XD
  • 2
    In your computer, there's a chip called memory. It stores 0s and 1s in groups of 8, called a byte.

    Something like an integer number is usually 8 bytes, so, 32 0s and 1s.

    It's easy to imagine 32 0s and 1s in your memory, but now imagine you need a *list* of integers.

    You have two options: either define a million of them in your code (int1, int2, int3....) which is a terrible option, OR you can ask your computer for a whole CHUNK of 0s and 1s and you can put all of your numbers in there.

    Here's why it starts at zero: you're writing computer code, not a shopping list.

    When you ask for a CHUNK of memory, it gives it to you, and the variable is a marker for where your memory STARTS.

    So, if you do this:

    int myNumbers[5];

    myNumbers is actually, itself, a number. It's the first byte (group of eight 0 and 1s) where your chunk of memory starts. This is called an address. (cont...)
  • 2
    (...cont)

    So, to get your FIRST integer from your chunk, you don't need to move anywhere from your address, thus, zero.

    If you wanted your SECOND integer, you start at the beginning of the chunk and move ONE over. Because if you didn't move at all, you would just get the first item from memory. If you moved TWO items over, you'd get the third item.

    In other words, the number you're giving to that array is not a COUNT, but an OFFSET from a STARTING POINT.

    I can't do bold in devRant so you're gonna have to settle for caps :P
  • 0
    @AlgoRythm your explanation is good, but it only applies to low level languages like C++.

    For understanding arrays and indices, imo it‘s enough to know that indexes are just offsets for collections like arrays or lists.

    The "n-th element minus one" hint is just misleading and counterproductive.

    If you get yourself into the mindset that you are just accessing elements from the beginning of a container and giving it an offset, you should never need index-1 in your code.
  • 0
    @Lensflare I just tailored it to OP who is writing C. Beyond that, though, high level languages technically don’t need to start at zero. It’s only in lower level languages where it’s literally a pointer offset.
  • 0
    @AlgoRythm yeah but as I said, you don’t need to go low level and think in terms of pointers and addresses in order for "starts at 0" to make sense.
    You just need to get rid of the wrong notion of "index is the n-th element minus one"
  • 0
    @Lensflare I think it’s important to explain why we use indexes instead of positions. Because we are programming computers and understanding that fact is very important.
  • 0
    @AlgoRythm I believed that once, too. Now I‘m not so sure anymore.
    Most programming languages are too high level for that and you don’t necessarily need knowledge of the low level stuff.
    The concept of contiguous, index based collections is more broad than just what a C-array is.
Add Comment