Tag Info

New answers tagged

0

Well, if you need to find something in your associative array, you need to iterate through each of the keys, and find equality. This means that in the worst case, you'll need to iterate through your whole collection to find the good key. Accessing an array with an index (myArray[0] for example) is instant; it doesn't require any searching, because the ...


3

Because you still need a way to search the associative array. The hash table is your search mechanism; it gives you O(1) performance for any given key search. What is your underlying search mechanism, if it's not a hash table? A Binary Search Tree? That's good for very large tables, but it's not O(1); it's O(log n). If you don't have a search ...


3

Yes, there is a reason: If you have a 1-based address, the starting (first) element of array anyway lies at zero address. It is a contradiction, that disappears if you have a 0-based address. And for the last you have one operation less when counting the address of the element. So, C in array addresses counting is more effective than much older 1-based ...


2

Seems as if your main difficulty is that you're using arrays instead of lists. Lists do not have the problem of requiring an initial size; you can grow them or shrink them as needed. I also have the impression that you think you have to store the entire content of the web site in a single data structure. You don't; a relational database like SQL Server ...


3

This anti-pattern would be named whatever you like. I'm not sure I've seen it named before, though I would heartily recommend against getting cozy with it. I've seen this in games occasionally when someone thinks it's a good idea to have a global list of "all of the units in the game". Even if that's a good idea, that shouldn't be the responsibility of the ...


0

You would usually want your zero (in fact variable) size array to know its size at run time. Then pack that in a struct and use flexible array members, like e.g.: struct my_st { unsigned len; double flexarray[]; // of size len }; Obviously the flexible array member has to be the last in its struct and you need to have something before. Often that ...


4

Let's look at how an array is typically laid out in memory: +----+ arr[0] : | | +----+ arr[1] : | | +----+ arr[2] : | | +----+ ... +----+ arr[n] : | | +----+ Note that there isn't a separate object named arr that stores the address of the first element; when an array appears in an ...


1

If you want a pointer to a memory address, declare one. An array actually points at a chunk of memory you have reserved. Arrays decay to pointers when passed to functions, but if the memory they are pointing at is on the heap, no problem. There is no reason to declare an array of size zero.


10

The issue I would wager is that C arrays are just pointers to the beginning of an allocated chunk of memory. Having a 0 size would mean that you have a pointer to... nothing? You can't have nothing, so there would have had to be some arbitrary thing chosen. You can't use null, because then your 0 length arrays would look like null pointers. And at that ...


0

You can specify base class with virtual function Update and derived classes overriding this function. Here is simple example: class Enemy { public: // this is abstract function, but you can also add implementation // as default behavior for derived classes virtual void Update() = 0; }; class Enemy1 : public Enemy { public: void Update() ...


0

(I'm assuming C++) std::vector holds its elements by value so if you store subclasses you are slicing the objects. You can use the PImpl idiom to avoid this, you have a concrete Enemy class that holds a (unique) pointer to a concrete enemy an abstract IEnemy that holds the custom code. class Enemy { Point location; std::unique_ptr<IEnemy> pimpl; ...


0

An n-dimensional array is a collection of data. This data can be accessed by a value. People often associate this value with coordinate in an n-dimensional space so this value would have n components say (x,y,z) for 3-d array. In other words, the number of dimensions in an array is the number of components in the value you use to access data in the array. ...


4

Most of the aspects of this question have already been considered, but I think it will help if you consider the nature of a dimension. Not all dimensions are spatial. A dimension is a context for measurement. Here are some examples: Frequency - colour or pitch Mass Valence Colour (up quark, down quark, strange quark, charmed quark etc) Spin direction ...


3

In physics, we assume each spatial dimension to be infinite, which makes finding space for new dimensions pretty difficult. When dealing with finite arrays, it's easy to find space. Imagine a sheet of paper with a grid printed on it; you can write some information in each cell of the grid. That's a 2D array: row and column. Put several of those sheets ...


4

Dimensionality comes from ... dimensions! A one-dimensional array is like one of those daily pill containers: It's a vector, with a single index, and you can select one specific element at a time by specifying where along the line it lies. A two-dimensional array is like a chessboard: It's a matrix, with two indices, and you can select one specific ...


1

Yes the examples given are all 1 dimensional as Kilan states, though it could also depend on exactly what you are using, e.g. computer languages which have different format to represent such constructs. Dimensionality itself can also be thought of as 'nesting', e.g. if there is one set of discrete elements that is 1 dimension, if each elements also has ...


2

In the Cartesian coordinate system, you have the x and y axes on a plane. You can represent any number on the plane as (x,y). In three-"space" (otherwise known as a cube), you can have the x, y, and z axes. You can represent any element of the cube as (x,y,z). In multivariate space, you can have the x, y, z and, w axes (where the w axis is "imaginary"). ...


5

1: [0] 2: [0,0] 3: [0,0,0] 4: [0,0,0,0] ... I think you've got it wrong. If you mean that these are one-/two-/three-/four-dimensional arrays, that's not the case: they're all one-dimensional, they just have different lengths. But length is not dimensionality. As whatsisname explained, the dimensionality of an entire array is the number of subscripts ...


3

The dimensionality is the number of subscripts you can use to select elements. Your example [0, [0,0,0]] is still a 2d array, albeit containing a 3d array as its second element. That doesn't make it a 4d or 5d array, just a nested data structure where the "dimensionality" concept breaks down and gets confusing real fast. Examples like [[0,0], [0,0]] and ...


12

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

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


22

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


40

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


16

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


42

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


19

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.


70

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



Top 50 recent answers are included