Which array element is the first? [closed] - arrays

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
A few times during discussion about programming, I reached a misunderstanding, caused by different views on how consecutive zero-based array elements are referred to using ordinal numerals. There seem to be two views on that:
a[0] = "first";
a[1] = "second";
a[2] = "third;
vs:
a[0] = "zeroth";
a[1] = "first";
a[2] = "second";
I always preferred the first, knowing that "n-th" element is "element of index n-1". But I was surprised how many people found that counter-intuitive and used the latter version.
Is one of those conventions more correct than the other? Which should I use during discussion or documentation to avoid misunderstanding?

I think the English meaning of the word "first" is unambiguous, and refers to the initial element of a sequence. Having "first" refer to the successor of the initial element is just wrong.
In cases where there might be confusion, I would say "the third element, at index 2".

The element index is pretty much language-dependent (e.g. C: 0, Lua: 1), whereas the fifth element is the fifth element, it's just the index that may be different ;)
I guess that's way too diffuse an answer...

In some languages, such as Pascal, you can specify the range of indexes explicitly. i.e.
var stuff : array[-3..3] of integer;
stuff[-3] is still the first element in the array, not the negative third.

Anyone saying 'zeroth' must not really believe in zero-based indexing.

The first is the one which is first taken from the stack.

Related

Can't find how to extract part of an array using only the index path number. (swift) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have what I believe to be a very simple question but for some reason, I can't find an answer to it anywhere. Say I have an array:
var array = ["Apples","Dogs","Monkeys","Cats","Apples"]
With this array, I want to print the data "Monkeys"; how would I print the data "Monkeys" using only the known index number (3)?
I tried this code below, but it only returned the number 3, not the data at index number 3.
print(array.index(after: 2))
Thanks in advance, hope it's simple.
The index of an array is sort of like the address to the data. So when you ask to find the index of the array after number 2, you're just asking for the next valid address, not the data inside that address.
If you want to actually find the data after index number 2, you could do this:
print(array[2 + 1])
or
let index = 2
print(array[index + 1])
either way, using the brackets tells the code that you actually want to take a look inside that "address".
Lastly, it's important to note that arrays start counting at 0, not 1. So if you're looking to extract "Monkeys" from array, you want to look at array[2] not array[3].

Split an array in two parts such that the difference of their sums is minimum [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
Ok let's say that I have an array as 1 4 2 3 1 and I want to split it in 2 sub arrays such that the absolute difference of their sums is minimum.
That is to say as for the above example the 2 sub-arrays would be 4 2 and 3 1 1 which is |6 - 5| that is 1.
It seems to be a dynamic programming question but I'd like to solve it the conventional way.
I am not looking for exact answers but rather the ideology about how should I approach this problem.
Any hints would be appreciated. But just the hints as I'd like to solve it on my own thereafter.
We are not concerned about the order of the elements and their can be duplicate elements as well.
If you just want to find the minimum by any means then the brute force way would be to find all the ways to split the array in two and find the difference of sum of the numbers in each pair. Then store the minimum (and the two sub arrays) or a list of the minimums and replace this if the program finds a new minimum. Iterate over all the possible pairs and there you go your new subarray.
Depending on how you code this there are way to improve on this method. For example if you have the pair [1,2,3] and [4,5] you don't need to also do [4,5] and [1,2,3] as this would be the same pair of subarrays.
I don't want to write an actual way of doing this as you did specify you want to try it yourself

Is there any justification for addressing array like <number>[array]? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Recently I saw an answer on a question where they explained that addressing arrays in this way <number>[array] is valid C code.
How do square brackets work in C?
Example:
char x[] = {'A','B','C','D','E','F','G','H','I','J'};
printf("%d\n",5[X]);
//Will print 70 == 'F'
This kind of notation seems cumbersome and potentially confusing for everybody including the author.
Does this way of addressing arrays come with some justifiable advantage?
or
Can I continue with my life without worrying?
I have never encountered this in "real code" (i.e., outside of intentionally obfuscated things and puzzles with artificial limitations) so it would seem that it is quite universally agreed that this shouldn't be done.
However, I can come up with a contrived example where it might be considered by some (not necessarily me) a nicer syntax: if you have multiple pieces of data related to a single entity in a column, and you represent the rows as different arrays:
enum { ADA, BRIAN, CLAIRE };
const char *name[] = { "Ada", "Brian", "Claire" };
const unsigned age[] = { 30, 77, 41 };
printf("%s is %u years old\n", ADA[name], ADA[age]);
I will be the first to agree that this obfuscates the syntax by making it look like the people are the arrays instead of being the indexes, and I would prefer an array of struct in most cases. I think a case could be made for this being nicer-looking, though, or perhaps in some cases it would be a way to swap the rows and columns (arrays and indexes) with minimal edits elsewhere.
As far as I can tell, there are no technical pros or cons with either method. They are 100% equivalent. As the link you provided says, a[i] = *(p+i) = [addition is commutative] = *(i+p) = i[a].
For subjective pros and cons, well it's confusing. So the form index[array] is useful for code obfuscation, but other than that I cannot see any use of it at all.
One reason (but I'm really digging here) to use the standard way is that a[b+c] is not equivalent to b+c[a]. You would have to write (b+c)[a] instead to make it equivalent. This can be especially important in macros. Macros usually have parenthesis around every single argument in every single usage for this particular reason.
It's basically the same argument as to write if(2==x) instead of if(x==2). If you by accident write = instead of == you will get a compiler error with the first method.
Can I continue with my life without worrying?
Yes.
Yes, the pointer arithmetic is commutative because addition is commutative. References like a[n] are converted to *(a+n) but also n[a] is converted to *(n+a), which is identical. If you want to win ioccc competitions you must use this.

Removing values from an array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Is there any simple way to remove values from an array, and then move all the other values up into their positions, i.e. a bit like bit shifting?
For instance let's say I have an array
a[5] = {0x01,0x02,0x03,0x04,0x05};
and I would like to remove the first two values and shift the rest so that the array then becomes:
a[5]= {0x03,0x04,0x05,0x00,0x00};
Is there any easy way to do this without having to create a new array and use a for loop to transfer all the values after a[1]?
I'm not sure what language do you use, but general idea might be this
for(i=0; i<(5-2); i++){
a[i] = a[i+2];
}
a[3] = a[4] = 0;
So you don't need a copy of array, you can just shift values in existing one.

Interview Q- product of all elements in array without the current element [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I recently came across an interview question and was wondering what could be the solution. Any ideas to solve it are greatly appreciated.
Given an array A[N] containing N numbers. Create an array Output[N] where Output[i] is equal to the product of all the elements of A[N] except A[i].
For example Output[0] is the product of A1 to A[N-1] and Output1 is the product of A[0] and from A[2] to A[N-1].
Do this without using the division operator. Do it in O(n).
Tip: do two iterations over the array - on the first one put in each cell the product of all preceding elements and on the second one multiply this by the product of all succeding elements.

Resources