Hot answers tagged

94

Keep in mind that Any doesn't operate on a List; it operates on an IEnumerable, which represents a concrete type that may or may not have a Count property. It's true that it's not necessarily the best thing to use on a List, but it definitely comes in handy at the end of a LINQ query. And even more useful than the standalone version is the override that ...


89

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 ...


77

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) (...


73

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 non-...


59

It's anyways bad practice to initialie a char array 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 ...


58

What you describe, "smoothing by fives", is a finite impulse response (FIR) digital filter. Such filters are implemented with circular buffers. You keep only the last N values, you keep an index into the buffer that tells you where the oldest value is, you overwrite the current oldest value with the newest one at each step, and you step the index, ...


55

There is a run time difference Count() can be O(n) where Any() is O(1).


49

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 ...


46

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 ...


45

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 ...


40

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.


32

The way I see this, it depends on what you intend to do with the data afterwards. Based on a few simple checks you can determine which of the two data structures is better for you: Does this data have any logic associated with it? For example, is $price stored as an integer number of cents, so a product with a price of $9.99 would have price = 999 and not ...


27

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 :)


26

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? ...


26

sizeof(a)/sizeof(t) explicitly codes the type into the expression. You now have the type specified in multiple locations, with no compiler support for ensuring that you're using the same type. So, if you change the base type of the array, but not in the (completely separate) count expression, bingo: your code will compile just fine, but your element count ...


21

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 evaluation ...


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

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


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

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 ...


14

This answer truly depends on who is going to use your code, and what standards they want to see. size_t is an integer size with a purpose: The type size_t is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object. (C++11 specification 18.2.6) Thus, any time you wish to work with the size of ...


13

I'm not certain if the enumerator is reset if an enumeration is not completed, so if that's a worry, then who knows at what point the enumeration would start? Enumerating an array would always start at the beginning. These two sentences make me think that you have deep misunderstandings about how the enumerable pattern works. Can you explain why you think ...


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 doesn't ...


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 x[2][3][5][4] ...


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 another ...


13

The key to understanding why array access is O(1) is understanding that they have a known length. In a language like C, we can guarantee this by allocating a sequential block of memory. Since it's sequential in memory, we can use pointer arithmetic for direct addressing and thus direct access. An array object would follow a similar principle with its ...


13

Every problem can be solved by adding an additional level of indirection. So do that. You can't delete part of an array in C++. But you can create a new array holding just the data you want to keep, then delete the old one. So you can build a data structure that allows you to "remove" elements you don't want from the front. What it will actually do is ...


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