Tag Info

Hot answers tagged

105

I don't think any of us can provide a stronger argument than Edsger W. Dijkstra's article "Why numbering should start at zero".


80

Authority argument Well... Apparently, most languages, including very recent ones, are zero-based. As those languages were written by quite skilled people, your friend must be wrong... Why one? why 1 would be a better starting index than zero? Why not 2, or 10? The answer itself is interesting because it shows a lot about the though process of the people ...


75

Fortunately, programs aren't limited by the physical constraints of the real world. Arrays aren't stored in physical space, so the number of dimensions of the array doesn't matter. They are flattened out into linear memory. For example, a single dimensional array with two elements might be laid out as: (0) (1) A 2x2 dimensional array might then be: (0,0) ...


53

This is a common conceptual difficulty when learning to use NumPy effectively. Normally, data processing in Python is best expressed in terms of iterators, to keep memory usage low, to maximize opportunities for parallelism with the I/O system, and to provide for reuse and combination of parts of algorithms. But NumPy turns all that inside out: the best ...


47

Half-open intervals compose well. If you're dealing in 0 <= i < lim and you want to extend by n elements, the new elements have indices in the range lim <= i < lim + n. Working with zero-based arrays makes arithmetic easier when splitting or concatenating arrays or when counting elements. One hopes the simpler arithmetic leads to fewer ...


47

You don't need to imagine in high spatial dimensions, just think of it as a fern leaf. The main stalk is your first array, with each branch being an item that it is storing. If we look at a branch this is your second dimension. It has a similar structure of smaller branches coming of it representing its data. These in turn have their own small branches ...


44

The dimensions are whatever you want to be, the 4th dimension doesn't necessarily have to be time. If you think of three dimensions as a cube, you can think of 4 dimensions as a row of cubes. 5 dimensions, a grid of cubes, and so on. You could also have a 3d collection of voxels, with a 4th dimension being color, or density, or some other property. When ...


40

Well, you can certainly implement a stack with an array. The difference is in access. In an array, you have a list of elements and you can access any of them at any time. (Think of a bunch of wooden blocks all laid out in a row.) But in a stack, there's no random-access operation; there are only Push, Peek and Pop, all of which deal exclusively with the ...


38

I'll expand my comment: ... if you're adding or removing elements, you want a list (or other flexible data structure). Arrays are only really good when you know exactly how many elements you need at the start. A Quick Breakdown Arrays are good when you have a fixed number of elements that is unlikely to change, and you wish to access it in a ...


37

Because they did a good job at separating the user model from the programming model, that's why. The same reason why most apps don't ask you, for example, Please enter a varchar(200) representing your name. Take a lesson from that.


29

Certain types of array manipulation get crazy complicated with 1-based arrays, but remain simpler with 0-based arrays. I did some numerical analysis programming at one point. I was working with algorithms to manipulate compressed, sparse matrices, written in both FORTRAN and C++. The FORTRAN algorithms had a lot of a[i + j + k - 2], while the C++ had ...


26

All arrays and data-structures are indexed based on zero, not just in .NET. Array indexes are numbered for the computer, text lines in a document are numbered for us. See screenshot :)


25

Imagine doing R&D on some new medical device, a series of sensors that you put along a patient's arms. You have seven volunteers lined up for testing. Each sensor reports low-frequency, mid-frequency, and high-frequency readings, which you take once every 100ms for about a minute. How to store all that data in memory for analysis and plotting? ...


20

There is a difference between counting and indexing. The index can start at any number (some languages support that), but for many reasons it is most often practical to have it start at zero. Counting also starts at zero, but as soon as one does count a set that is not empty, the first element is 1, and so on.


20

You wrote "assume a C-like language". Just how C-like should it be? First, it does appear that the author's logic is backwards: It has the effect of assigning the value of a[1] + 1 to a[2] whenever the evaluation of the left-hand component of the assignment precedes the evaluation of the right-hand one. In fact, it has that effect if the ...


20

An array is only a block of continous memory. Memory addressing is one-dimensional, you can either go forward or backward. So assuming you have an array with 5 elements, 5 memory blocks will be reserved. If you have a 2-dimensional array with 5 elements in each dimension, 25 memory blocks will be reserved.


18

...or I'd be asking it on MathSO... Well, as a matter of fact mathematicians would never (or at least not usually) associate a fourth dimension with anything like time. Nor would they associate the first three ones with anything space like: mathematicians simply define dimension as an abstract property of, typically, a vector space (often this will be ...


17

The reasons are not just historical: C and C++ are still around and widely used and pointer arithmetic is a very valid reason for having arrays start at index 0. For other languages lacking pointer arithmetic, whether the first element is at index 0 or 1 is more of a convention rather than anything else. The problem is that languages that use index 1 as ...


14

Yes, it's called a hash table or map - don't know if your language has them builtin but it's easy to code. This allows you to check if an entry is already there in the same time no matter how big the list (almost). If you need to preserve the order then you would use a sorted linked-list. Then it's easy to find (by searching) if the entry is new and a ...


14

you should never initialize a char[] with a string literal The author of that comment never really justifies it, and I find the statement puzzling. In C (and you've tagged this as C), that's pretty much the only way to initialize an array of char with a string value (initialization is different from assignment). You can write either char string[] = ...


14

Because the index n of an array points to the n+1th element in the array (using zero-based indexing). Some simple math allows you to calculate the exact position of the desired element in O(1). Further Reading Java Arrays


13

In a normal array to access elements i would simply do this: int matrix[2] = { 1, 15}; You're declaring an array with that code. You're also assigning '1' to the int at index 0 and '15' to the int at index 1. I have tried accessing elements the old way: int fly[2][2] = { 0}; but i noticed that this changes all the elements to 0 That code ...


13

The question is a silly question without qualification. Without specifying the language in the original question, there is no way of knowing the intended answer. If the question was "is the default implementation of a string an array in C" then the answer would be "yes". If the question was "is a string always an array" then the answer is no, as a string ...


13

In programming, arrays are quite easy to implement, but maybe not to understand. Generally, each level of arrays means to have the content n-fold. That means int x[4] are 4 blocks, each of them containing an int. int x[5][4] are 5 blocks, each of them containing an int[4]. int x[3][5][4] are 3 blocks, each of them containing an int[5][4]. int ...


13

Think of a one-dimensional array like a chest of drawers: Each drawer is an index of the array. You can put whatever you want in each drawer, and for many purposes, each drawer will only contain a single item (that's a one-dimensional array). This chest of drawers is magical though, so it's not limited by physical space. That means that you can put ...


12

Lists are much more versatile than arrays. With lists, you can recurse (e.g., for mapping or folding) by cdring down a list. This doesn't work for arrays; you'd have to pass in the array index too. For example, this is a simple implementation of map and fold that take one list only: (define (map1 f l) (if (null? l) l (cons (f (car l)) (map1 f (cdr ...


12

How many dimensions are needed? C++ Template programming may require some code duplication for each level of higher dimension. Address calculation is the easy part. A simple approach can be used for dimensions up to a dozen. For a 3-dimensional example: Let the size of the array be [m, n, p] For each dimension, we calculate a "weight vector" by ...


11

First of all, it would help to read dmr's Development of the C Language to get some insights into some of C's quirks, particularly when it comes to array semantics (basically, blame BCPL and B for most of it). As for the question "[w]hy not just enforce that index[array] is invalid, for clarity's sake," what would such a check buy you in exchange for the ...


11

So let's start with the simple approach to calculate the bare minimum and find additional means to reduce the size required. A 5x5 magic square contains the values from 1 - 25. Said another way, we have 25 potential numbers to store. There are 25 cells within the grid, and 5 rows. 1st approach The simplest approach is to use 1 Byte per cell. So that's 25 ...



Only top voted, non community-wiki answers of a minimum length are eligible