multidimensional-array interview questions
Top multidimensional-array frequently asked interview questions
What are the differences between multidimensional arrays double[,]
and array-of-arrays double[][]
in C#?
If there is a difference, what is the best use for each one?
Source: (StackOverflow)
This question already has an answer here:
I have an array of objects:
var array = [(id, name, value),(id, name, value)]; //and so on
How do I get the array to be sorted in ascending order of the atribute name (array[i][1])
?
I've tried to do this: array[i][1].sort()
, but that doesn't work.
Please help me!
Edit: the array can contain more than two objects! It can contain hundreds.
Edit:
Why is this question marked as a duplicate, when it was asked 2 years before the "duplicated" question?
Source: (StackOverflow)
Possible Duplicate:
How do I Sort a Multidimensional Array in PHP
How can I sort this array by the value of the "order" key? Even though the values are currently sequential, they will not always be.
Array
(
[0] => Array
(
[hashtag] => a7e87329b5eab8578f4f1098a152d6f4
[title] => Flower
[order] => 3
)
[1] => Array
(
[hashtag] => b24ce0cd392a5b0b8dedc66c25213594
[title] => Free
[order] => 2
)
[2] => Array
(
[hashtag] => e7d31fc0602fb2ede144d18cdffd816b
[title] => Ready
[order] => 1
)
)
Source: (StackOverflow)
i was wondering what is the best way to generate an MD5 (or any other hash) of a multi-dimensional array?
I could easily write a loop which would traverse through each level of the array, concatenating each value into a string, and simply performing the MD5 on the string.
However, this seems cumbersome at best and i wondered if there was a funky function which would take a multi-dimensional array, and hash it?
Thanks for your time.
Source: (StackOverflow)
I'm beginning python and I'm trying to use a two-dimensional list, that I initially fill up with the same variable in every place. I came up with this:
def initialize_twodlist(foo):
twod_list = []
new = []
for i in range (0, 10):
for j in range (0, 10):
new.append(foo)
twod_list.append(new)
new = []
It gives the desired result, but feels like a workaround. Is there an easier/shorter/more elegant way to do this?
Source: (StackOverflow)
I use in_array()
to check whether a value exists in an array like below,
$a = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $a))
{
echo "Got Irix";
}
//print_r($a);
but what about an multidimensional array (below) - how can I check that value whether it exists in the multi-array?
$b = array(array("Mac", "NT"), array("Irix", "Linux"));
print_r($b);
or I shouldn't be using in_array()
when comes to the multidimensional array?
Source: (StackOverflow)
This question already has an answer here:
I have CSV data loaded into a multidimensional array. In this way each "row" is a record and each "column" contains the same type of data. I am using the function below to load my CSV file.
function f_parse_csv($file, $longest, $delimiter)
{
$mdarray = array();
$file = fopen($file, "r");
while ($line = fgetcsv($file, $longest, $delimiter))
{
array_push($mdarray, $line);
}
fclose($file);
return $mdarray;
}
I need to be able to specify a column to sort so that it rearranges the rows. One of the columns contains date information in the format of Y-m-d H:i:s
and I would like to be able to sort with the most recent date being the first row.
Source: (StackOverflow)
This question already has an answer here:
double[][] ServicePoint = new double[10][9]; // <-- give me error (1)
double[,] ServicePoint = new double[10,9]; // <-- ok (2)
What' their different? (1) gives me error, what's the reason?
And
double d = new double[9]
ServicePoint[0] = d;
using (2) will prompt me error, what's the reason?
Source: (StackOverflow)
- What is the most efficient way to check if an array is a flat array
of primitive values or if it is a multidimensional array?
- Is there any way to do this without actually looping through an
array and running
is_array()
on each of its elements?
Source: (StackOverflow)
I have a JavaScript array like:
[["$6"], ["$12"], ["$25"], ["$25"], ["$18"], ["$22"], ["$10"], ["$0"], ["$15"],["$3"], ["$75"], ["$5"], ["$100"], ["$7"], ["$3"], ["$75"], ["$5"]]
How would I go about making this just:
["$6", "$12", "$25", ...]
Source: (StackOverflow)
C++ inherited arrays from C where they are used virtually everywhere. C++ provides abstractions that are easier to use and less error-prone (std::vector<T>
since C++98 and std::array<T, n>
since C++11), so the need for arrays does not arise quite as often as it does in C. However, when you read legacy code or interact with a library written in C, you should have a firm grasp on how arrays work.
This FAQ is split into five parts:
- arrays on the type level and accessing elements
- array creation and initialization
- assignment and parameter passing
- multidimensional arrays and arrays of pointers
- common pitfalls when using arrays
If you feel something important is missing in this FAQ, write an answer and link it here as an additional part.
In the following text, "array" means "C array", not the class template std::array
. Basic knowledge of the C declarator syntax is assumed. Note that the manual usage of new
and delete
as demonstrated below is extremely dangerous in the face of exceptions, but that is the topic of another FAQ.
(Note: This is meant to be an entry to Stack Overflow's C++ FAQ. If you want to critique the idea of providing an FAQ in this form, then the posting on meta that started all this would be the place to do that. Answers to that question are monitored in the C++ chatroom, where the FAQ idea started out in the first place, so your answer is very likely to get read by those who came up with the idea.)
Source: (StackOverflow)
We have some interop code that involves matrices. I was trying to call the native DLL and for the most part it works very reliably.
I am relying on the default marshalling by .net, avoiding unmanaged pointers and rather using .net arrays for the most part, and maybe a byref
here and there. The .net article says, that multidimensional arrays are implicitly marshalled as column-major one-dimensional arrays, which would be fine.
The only thing that does not seem to work, is trying to marshal a multi-dimensional array, as the F# compiler complains that float[,]
is not allowed in an extern
declaration. Is there any way around this limitation?
I am aware of the PinnedArray
and PinnedArray2
types from the F# PowerPack, but I was looking for a solution which relies on managed pointers and - more importantly - I'd like to avoid having to include F# PowerPack as a dependency just for the PinnedArray
classes.
Source: (StackOverflow)
I wonder if there's a possibility to create a two dimensional array and to quickly access any horizontal or vertical sub array in it?
I believe we can access a horizontal sub array in the following case:
x = Array.new(10) { Array.new(20) }
x[6][3..8] = 'something'
But as far as I understand, we cannot access it like this:
x[3..8][6]
How can I avoid or hack this limit?
Source: (StackOverflow)
Can anyone give me a sample/example of JavaScript with a multidimensional array of inputs? Hope you could help because I'm still new to the JavaScript.
Like when you input 2 rows and 2 columns the output of it will be 2 rows of input and 2 columns of input.
Like this:
[input][input]
[input][input]
Source: (StackOverflow)
I want to repeatedly zero a large 2d array in C. This is what I do at the moment:
for(j = 0; j < n; j++)
{
for(i = 0; i < n; i++)
{
array[i][j] = 0;
}
}
I've tried using memset:
memset(array, 0, sizeof(array))
But this only works for 1D arrays. When I printf the contents of the 2D array, the first row is zeroes, but then I got a load of random large numbers and it crashes.
Source: (StackOverflow)