Questions tagged [array]

An array is a systematic arrangement of similar objects, usually in rows and columns.

Filter by
Sorted by
Tagged with
1
vote
3answers
165 views

Does the gcc optimize out local arrays the same way it would optimize out a local variable?

If I write this code in a function: void func(void) { int x = 0; int y = 3724832+x; } It would (probably) optimize the variable y out because it isn't being used. Even if I'm wrong about ...
0
votes
1answer
67 views

PHP: simple class for storing read-only data structure, array alternative

I need to share some associative data between different parts of my application with two requirements: immutability (so read-only) safety against mispelling or unavailable index inside the data ...
2
votes
1answer
33 views

At what size array in Swift would you consider switching to CoreData?

I've developed a reading tracking app in Swift and for simplicity's sake I used arrays for my model. It stores some data about the books a user reads and a small image of the cover. I save them to ...
1
vote
0answers
33 views

is it better to have tracking fields that are maintained separately for arrays? [duplicate]

I wasn't sure exactly how to word this question, but basically, I have a struct stNeuralLayers (for a neural network I'm playing around with) with fields that are matrices (such as a double[,] ...
0
votes
2answers
157 views

Should execution continue if array is empty?

Just a general programming best practices question. If I have a function like that takes in an array of items and the function then calls a series of other functions which filter and act on the items ...
2
votes
1answer
91 views

How should I define hardcoded strings with some variable parts? Reuse more characters? Or keep the whole sentence?

for example, sometimes I need to define a hardcoded string with some variable parts, I often have trouble to choose the style: style 1 : reuse every characters when possible showMessage(num){ let ...
2
votes
2answers
346 views

Should I use Array or Set if both can be used to finish my task?

for example, Suppose I have a 2d array: let filterArr=[ [1,1,0,1,1], [0,1,1,1,0], [1,1,0,1,0] ]; I want to find and store the index of column that all are 1, i.e.:position 1 and 3, and the ...
0
votes
1answer
147 views

For arrays, should I use the first position to store the selected element, instead of variable like “selectedIndex”, if possible?

For example, I have a toggle button, which would show the current selected element, and would show next element when it is clicked, and it can loop back to first element. I have 2 ways to store the ...
2
votes
1answer
96 views

Should special case be inside or outside the for loop here?

For example, suppose I have 2 arrays: let arr1=[5,2,1]; let arr2=["abcde","ab","a"]; my work is simple : to check if length of strings in arr2 are larger than corresponding element with same index ...
2
votes
2answers
120 views

Should I move tasks which is just for a specific element only out of for loop?

For example, I have a for loop, which element 0 has additional function to run compared with other elements, my question is, should the additional function be: 1.place inside for loop for(int i=0;i&...
2
votes
3answers
338 views

How do I put objects of different types derived from a common base in the same array? [closed]

I have two classes called Player and Enemy both inheriting from a class called GameObject, how do I store them both in an array of type GameObject while still keeping their own unique information? I ...
3
votes
1answer
105 views

Array rotation algorithm

I'm developing an application which sends quotes to clients via email. Since quotes are obtained from various resources (per each client) they end up in a single array which is then used as a source ...
-1
votes
2answers
591 views

How does a dynamic array access work?

In C if i declare int x[4] Which tells me its an integer array with each array element size fixed of size 4. So after initialization when I access x[3] : address of array +element size which is 2 *...
2
votes
2answers
87 views

Linked-list iteration patterns

If you have a linked-list, where the items are not necessarily close to each other in memory, wondering if it is (in general) better/worse/no difference to do the following. Say you want to iterate ...
3
votes
1answer
118 views

What is best practice for handling mapping arbitrary depth in arrays?

I have a set of data: id | name | parentid ------------------------ 1 | parent | 0 2 | child | 1 3 | child | 1 4 | parent | 0 5 | child | 4 6 | subchild | 5 7 | child | 4 ...
1
vote
2answers
2k views

How exactly indexing works in arrays?

I only know that index is faster but don't know why is it faster. Suppose I have an array int[] a = {2,3,6,7}. Then I will try to find the element at a[3] and the speed of this will be O(1). Why? How ...
9
votes
2answers
911 views

How did the custom of using square brackets for array elements develop?

Many programming language use the syntax a[i] to refer to the i'th element of an array, sequence, or vector a - specifically, C and Pascal (from the late 1960s and early 1970s) do this. On the other ...
1
vote
2answers
177 views

Are there any use cases for List when Deques and Arrays are available?

I've been thinking about this over the past few weeks, and I've come up with no good arguments. My perspective is from Java, but if anyone has any language-specific cases outside of this language, I'...
3
votes
4answers
98 views

Store csv data as rows or columns in view of the needed processing?

Assume I have some data a csv-Files like ObjectName, PropertyA, PropertyB, PropertyC "Name1", 3, 1, 4 "Name2", 1, 5, 9 "Name3", 2, 6, 5 ... and a typical question I want to answer would be For ...
4
votes
3answers
622 views

Is “Array[1]” the first element or second element in the array?

Following the reading of the question Why are zero-based arrays the norm?, I wonder about the terms to use for referring to specific array elements, in the perspective of linguistic reading of ...
0
votes
1answer
92 views

What is the name or description of my technique for filtering items in array? [closed]

I've tried posting this question on StackOverflow but they told me to try it here since it's not so specific. I'd like to know the name of the algorithm or technique of following implementation. I ...
1
vote
0answers
117 views

speed up search in string array [closed]

Suppose we have sorted array of strings e.g. std::vector<std::string> or some other elements with string as key - for example std::vector<std::pair<std::string,data_type>. Normally to ...
1
vote
0answers
58 views

How should I store a frequently accessed array with an unknown number of elements in mongodb?

I have a simple "Index" object defined as follows: { "position": 4, "value": 345 } And I'm trying to figure out the best way to store this object in a mongo database, assuming that: The "...
-1
votes
1answer
568 views

Why is it possible to access an array out of bounds with negative indexes much further than with positive indexes?

I have written two small programs in which I declare a very small array. Then I try to access values out of bounds. The interesting thing I noticed that when I try to decrement the index I can ...
1
vote
2answers
160 views

Is it unnecessary to have a method that merely loops through an array and calls another method with each item?

For example, if there's multiple lines to be drawn, I could do function drawLines(lines) { lines.forEach(function (line) { drawLine(line); }); } function drawLine(line) { //... } ...
2
votes
3answers
2k views

Return array pointers vs. populating an array inserted as a parameter?

Which is better? I noticed the latter is used in a lot of C code. People will typically malloc an array, and then pass that as a parameter to a function, which will then populate it. Whereas in Java, ...
-4
votes
1answer
1k views

How to create a method that takes array list as argument?

To create a method that takes an array as an argument, you would do this: public static void printAges(int[] age) { // prints ages to screen for (int x = 0; x < age.length; x++) { ...
2
votes
1answer
150 views

Handling out of bounds requests in embedded C library

We're currently building a library in C that configures itself from tables in EEPROM. This leaves the possibility however that some caller requests a field that is out of bounds, and the question ...
1
vote
1answer
645 views

Merging algorithm for overlapping intervals

I have been searching for an efficient algorithm to merge overlapping intervals on a dynamic array of intervals. For example, (start time, end time) wise, [(1, 2), (4, 8), (3, 10)] becomes [(1, 2)...
0
votes
1answer
632 views

Lower bound for finding a value in sorted array

Suppose we have a sorted array of unique elements, A[n]. I am trying to find the lower bound for finding a specific element x in the array. I suspect the lower bound to be log_2(n+1). I tried to use ...
2
votes
5answers
765 views

Fastest algorithm of dividing array into positive and negative numbers

Given array of integers I am trying to design the fastest algorithm that swaps the elements such that at the end : all negative elements are on the left and then the positive elements, for example, ...
3
votes
2answers
5k views

Check for empty PHP array

An empty array in PHP is considered falsey. That means the following will not print anything as the array is empty. <?php $myArray = array() if ( $myArray ) { print "My Array is NOT empty"; } ...
-1
votes
1answer
587 views

Should objects be singular since arrays are plural?

Might be a silly question, but what is more logical: object.actions.bark() or object.action.bark()? Simply about plurality. I know arrays are usually pluralized. But if I were to be consistent; ...
1
vote
2answers
258 views

How do I best store a value with multiple keys?

I need to store two 32-bit numbers in a way that is quickly accessible and as storage space efficient as possible. My keys are a combination of the following two values: port: 0 - 29 vlan: 1 - 4095 ...
1
vote
0answers
488 views

Best way to handle large amount of objects

I'm building an javascript application where I need to register devices to an specific room. So I thought, I'm making my room_name as main key (room name is unique). And I'm going to add the devices ...
2
votes
2answers
254 views

Arrays vs Dynamic Arrays

In Steven Skiena's The algorithm design manual Edition 2, he talks about dynamic arrays on page 67, which results in them having same Big-Oh as normal arrays. I don't understand how. This is what book ...
0
votes
4answers
1k views

How does C know the bounds of a multi-dimensional array?

Coming from Python, if C does not have array bounds, how does it know where a[1] starts? int a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; b = a[1][1];
20
votes
3answers
5k views

Professional way to produce a large problem without filling up huge arrays: C++, free memory from part of an array

I'm developing a physics simulation, and as I'm rather new to programming, I keep running into problems when producing large programs (memory issues mainly). I know about dynamic memory allocation and ...
15
votes
4answers
2k views

size_t or int for dimensions, index, etc

In C++, size_t (or, more correctly T::size_type which is "usually" size_t; i.e., a unsigned type) is used as the return value for size(), the argument to operator[], etc. (see std::vector, et. al.) ...
1
vote
3answers
1k views

Building a simple AI for Noughts & Crosses game

I'm developing a simple noughts & crosses game where the player plays against the computer. Obviously, I use the Randomize function of the language, however, the final stage of refinement is to ...
-1
votes
4answers
2k views

how to speed the random access of 2D array? [closed]

arr[2^30][100000] for (int i = 0; i < 2^28; i++) { value = a*b+c;//only a example. In all, value is an integer. arr[value][arr[value].length++] = i; } My code like above. Random access a ...
0
votes
1answer
717 views

find longest subsequence with sum less than or equal to k

I have a vector<int> num, I want to get the size of longest subarray with the sum less than or equal to k. My approach: O(n^2). Repeat for every element in the array. Can we do better?' ...
-1
votes
1answer
31 views

Creating block of data from a large input data

I am working on an algorithm whose specs says that it accepts 32 bit input (long type). My actual data is 14 bytes e.g 11:12:12:04:DD I have created sub arrays like subarray1[4]={'1','1',':',1} // ...
1
vote
3answers
416 views

Find if any pair exists in an unsorted array?

I have come across a programming question in which I have to determine : Does there exists at least one pair in a given unsorted array such that |i - j| <= K and |A[i] - A[j]| <= x ? ...
-1
votes
1answer
66 views

How to fit k-length arrays to number of bigger arrays?

I have n number of one-dimensional arrays similar to: [0,0,0,0,0,1,1,1,0,0,1,1,...] 0's and 1's indicating occupation. And k number of smaller arrays similar to: [2,2,2], [2,2], [2], [2,2,2,2] ...
-2
votes
1answer
579 views

How to ask a user how many inputs they want

I want to know if it possible to use arrays for a more viable alternative of getting a non-predetermined number of inputs as an array and with an indefinite number of inputs. (python) original code --...
1
vote
2answers
2k views

Example of problem caused by casting Object[] to E[]

I've heard here and there that arrays contain runtime data about their type, making you unable to instantiate an E[] (E being a generic type parameter, for example in a class Foo<E>), and that ...
0
votes
1answer
220 views

Is there anyway I can store the static std::vectors of pointers of all my derived classes in my base class?

I currently have a program that has multiple derived classes. All instances of a derived class are stored inside of that class's static vector of pointers. I also have a static vector of pointers ...
6
votes
2answers
2k views

Is it good practice to use array.pop() assignment in a while loop condition?

Just saw a code snippet that used an array pop assignment in the while condition; was wondering if this is acceptable/good practice? var arr = [0,1,2,3,4,5]; var current; while (current = arr.pop()) {...
1
vote
2answers
652 views

Having trouble with AP computer science sample test problem

I am currently taking AP Computer Science at my high school and while looking at some sample AP test problems I came across one that really confused me. Sample Problem The answer to the problem is ...