Index out of range in multidimensional array in Swift [duplicate] - arrays

This question already has answers here:
Swift 3 2d array of Int
(2 answers)
Closed 5 years ago.
var tri = [[Int]]();
tri[0][0] = 321;
This code causes this error:
fatal error: Index out of range
What's wrong?

You're accessing the first element of the first subarray of the array. But your array doesn't contain any subarrays, so it crashes.

Related

Ruby: Compare if array contains other array [duplicate]

This question already has answers here:
How to determine if one array contains all elements of another array
(8 answers)
Closed 4 years ago.
how can i compare if an array of strings contains a smaller array of strings in Ruby?
e.g.
a=["1","2","3","4","5"]
b=["2","3"]
now i want to check if a contains b and get true/false
Thanks.
The most common approach would be to
(b - a).empty?
It has issues with unique elements, though. To detect whether a includes all elements from b, one should:
a_copy = a.dup
b.all? { |e| a_copy.delete e }
# or
b.all?(&a_copy.method(:delete))

finding the size of an unknown array in C [duplicate]

This question already has answers here:
How do I determine the size of my array in C?
(24 answers)
Closed 4 years ago.
We have been given a large array of unknown size with elements given , is there any function or something other through which we can find the size of that array in language C
int a[]={4,6,4,26,3,2,5,7,3,7,3,2,5,4,6,3,7,232,6,32,6,3,7,3,6,2,5,7,3,6,3,6,36,3,67,23,6};
The size in bytes you can get by
sizeof(a);
The number of elements in that array you can get by
sizeof(a)/sizeof(a[0]);

How to convert a swift string to an array? [duplicate]

This question already has answers here:
Swift 3.0 iterate over String.Index range
(10 answers)
Convert Swift string to array
(14 answers)
Closed 5 years ago.
Would it be possible to loop through each character in the string, and then place each character into an array?
I'm new to swift, and I'm trying to figure this out. Could someone write a code for this?
It's really simple:
let str = "My String"
let letters = str.characters.map({String($0)})
print(letters)

Move array in array of arrays [duplicate]

This question already has answers here:
How to change the position of an array element?
(8 answers)
Closed 6 years ago.
I have an array of arrays like this:
[['a','b','c'],['d','e','f'],['g','h','i'],['j','k','l']]
Now I want to move the last array ['j','k','l'] after array ['a','b','c'] and before array ['d','e','f']. How can I do this?
array.insert(1, array.delete_at(3))
This should do it.

How to randomize numbers with no repeat in C? [duplicate]

This question already has answers here:
Unique (non-repeating) random numbers in O(1)?
(22 answers)
Unique random number generation in an integer array [duplicate]
(9 answers)
Closed 8 years ago.
I want to randomize number in each element of array in a variabel. I currently use srand() function. But, with this function i could get a same number in two or more element of array.
the output of my program is
number[0]=6
number[1]=3
number[2]=8
number[3]=3
See, number[1] and number[3] has same value. How to prevent this thing happen?

Resources