Tag Info

Hot answers tagged

39

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


25

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


24

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


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


19

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.


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


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


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


11

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[] = ...


11

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


10

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


9

You won't like this answer because you've already heard it. Go code! Seriously. At some point, coding becomes an experiential thing. You have to "do it" in order to "get it." You're there. So go code. but I haven't had any reasonable goals and reading books feels like eternal loop that just leads nowhere Then fix your expectations and goals. ...


8

There is nothing wrong with taking those temporary references - in fact it may be preferable in some cases because you can name the reference. If that particular reference has a particular role that may not be immediately obvious from the array index then the name will add clarity. If the discussion centers around "performance", then the guideline is the ...


8

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


8

System.Array is effectively derived from System.Object. You don't see it here, because everything in .NET Framework derives by default from System.Object, including types like int (System.Int32). In other words: class MyClass : MyOtherClass { } indicates that MyClass is derived from MyOtherClass. class MyClass { } indicates that MyClass is derived from ...


8

I think you could construct some kind of binary tree where each left child node contains the maximum value in the left half of the range covered by its parent and the child right node the maximum value in the right half. 78 45 78 23 45 78 6 23 17 9 45 78 2 4 6 Then you only need to find ...


8

If by "best" you actually mean "fastest", then by far the fastest way (although not nearly the most efficient way) is to choose a multiplier that makes all of the weights integers, at least to whatever precision you care about, and then store that many copies of each in one large array. For example, if you assign "score multiplier" a weight of 80%, and ...


7

If your situation is that you've got a piece of data and you want to see what it looks like as RGB values, try using the PPM file format. It's trivially simple, and writing a tool to convert the byte array to PPM should be no more than a few lines of code. PPM file format description: http://en.wikipedia.org/wiki/Netpbm_format


7

The order of evaluation is usually undefined, and therefore can go either way depending on the compiler, although some pedantic languages might define it. That's why most of the time when you see an example like that, it's to warn you not to do it. If the compiler goes strictly by the order the index is needed, the book is right, because you have to read ...


7

Ruby’s model is provided more for convenience than correctness, and is inconsistent: array + array is array concatenation, allowing duplicates, but array - array is set difference, removing duplicates: [1, 1] - [1] is [], not [1]. - is not the inverse of +, because it’s not the case that a + b - c == a for all Array instances a, b, and c: take [1] + [1] - ...


6

If you keep your newlist sorted, then you can use binary search to determine whether a duplicate exists. This will only make a difference when your newlist starts to get large, so if it never gets longer than (say) 64 entries you may not see any improvement. It may also not work if it's expensive to insert into the middle of an array -- shifting all the ...


6

Arrays are reference types. That is when a property returns an array object it's returning a pointer/reference to it's internal array. The caller of the property can now modify that internal array (as they have a pointer to it), this is generally unwanted. To stop this you can return a copy of the internal array but now your doing a memory allocation and ...


6

Well of course they aren't the same, in the same way that a cat is not the same as a dog because they are both mammals and a crab is not a spider even because they are both from phylum arthropoda; though I've had teachers tell my children the latter, thus necessitating a daddy lecture on evolution, biological classification of species, and the importance of ...


6

Python actually supports insertion of arbitrary sub-lists, as a part of list slice assignment. You just assign to an empty slice before the element where you want to insert a new list: list1 = [1, 2, 3] list1[1:1] = [10, 20, 30] # insert 3 elements at index 1 print list1 # prints [1, 10, 20, 30, 2, 3] Many languages view lists as primarily sequential ...


6

Let's assume we have a 2d-array like (for simplicity sake, the process for higher dimensionality is the same) int array[3][3]; The array viewed in memory is one dimensional, but logically we can think of it like this array ---------------------------- | [0][0] | [0][1] | [0][2] | ---------------------------- | [1][0] | [1][1] | [1][2] | ...


5

The choice to use array[index] was probably made to follow mathematical convention and the precedent set for arrays by other languages like ALGOL, FORTRAN, and BASIC (the latter two use parentheses instead of brackets). That decision does make the operator an odd duck because it's binary operator but requires that you throw in an additional token after the ...


5

In a pure stack, the only allowable operations are Push, Pop, and Peek but in practical terms, that's not exactly true. Or rather, the Peek operation often allows you to look at any position on the stack, but the catch is that it's relative to the one end of the stack. So, as others have said, an array is random access and everything's referenced to the ...


5

The design decisions such as the one you mention should be driven by the requirements. Since it seems purely a modelling exercise, you shouldn't worry too much. As a general rule for less future headaches, favour inmutable classes, so I would get rid of that setter. I'd also use enums instead of char arrays. Finally try not repeating chunks of code. ...


5

This goes a bit beyond an intro Java class, but... Subclassing can send you down some pretty deep rabbit holes. The number of strings is only the beginning. Acoustic, electric, or acoustic-electric hybrid? If electric, solid or hollow body? How many pickups? Single or double coil (humbuckers)? Fixed or floating bridge? If acoustic, classical or steel ...



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