Shortcut for declaring many variables in Swift - swift

Is there a more efficient way to write this code considering 12 variables all have the same value.
var b1 = 0, b2 = 0 , b3 = 0, b4 = 0, b5=0,b6=0,b7=0,b8=0,b9=0,b10=0,b11=0,b12=0

If you are okay with using an array instead, you might want to use this shortcut:
var b = [Int](repeating: 0, count: 12)
From struct Array documentation:
Creates a new array containing the specified number of a single, repeated value.

Related

Swift Index and it's implementation

I'm new to programming. I was trying to understand how indices work in swift. This is the following code from swift documents.
converted into function.
func ind(){
var c = [10, 20, 30, 40, 50] //index = [0:5]
var i = c.startIndex //i = [0]
while i != c.endIndex { // i!= [5] , while i not equal to [5]
c[i] /= 5
i = c.index(after: i)
}
print(c) // [2,4,6,8,10]
}
the code line i = c.index(after: i) doesn't seems to make sense to me. "after" means the character of string after the string.index, but because we initialized the 'i' to be zero(0) the output should stay [4 and onwards]. secondly, if i replace the i let's say with integer 2. the loop keeps repeating itself. why? Thank you in advance for your time
after means the next element on your list in this context.
An index is more general and is not limited to String.
In addition, an index can have different types depending on the structure that you are manipulating.
For instance:
var c1 = [10, 20, 30, 40, 50] //
var i1 = c1.startIndex
// Print: Int
print(type(of: i1))
var c2 = "Hello" //
var i2 = c2.startIndex
// Print: Index
print(type(of: i2))
You can even create your own index for a specific type, as long as you provide a way to compute the next index.
Thus, in your code for this particular example, the index is of type Int.
If you change the value of i to be the constant 2, you can never equal the value of the end index (which is 5 here).
Then, your loop will never end.

CAPL: Create a lookup table, that assigns values to a changing variable

I want to assign a value to a variable in CANoe by using a lookup table.
If sysvar:test changes to the values 1, 2 or 3, then I want that sysvar::show changes to the values 4, 78 and 33 or other values I assign. How can I do this?
Thanks in advance!
This would be a good place to start
variables
{
int mapValues[int64];
}
on prestart
{
mapValues[1] = 4;
mapValues[2] = 78;
mapValues[3] = 33;
}
on sysvar test
{
#sysvar::show = mapValues[#sysvar::test];
}

Make zeros to certain rows in 2D array [duplicate]

It's actually a very simple question, but after an hour I can not solve my problem.
I need to create a 2d array of Int.
var arr = [[Int]]()
or
var arr : [[Int]] = []
tried to change value :
arr[x][y] = 1
fatal error: Index out of range
Should I use APPEND or I need specify the size of the array?
I'm confused..
It's not simple really. The line:
var arr : [[Int]] = []
Creates a variable of type Array of Array of Int and initially the array is empty. You need to populate this like any other other array in Swift.
Let's step back to a single array:
var row : [Int] = []
You now have an empty array. You can't just do:
row[6] = 10
You first have to add 7 values to the array before you can access the value at index 6 (the 7th value).
With your array of arrays, you need to fill in the outer array with a whole set of inner arrays. And each of those inner arrays need to be filled out with the proper number of values.
Here is one simple way to initialize your array of arrays assuming you want a pre-filled matrix with every value set to 0.
var matrix : [[Int]] = Array(repeating: Array(repeating: 0, count: 10), count: 10)
The outer count represents the number of rows and the inner count represents the number of columns. Adjust each as needed.
Now you can access any cell in the matrix:
matrix[x][y] = 1 // where x and y are from 0 to rows-1/columns-1
Not only you need to initialize both the array and subarrays before being able to assign any values, but also each array length must be greater than the index position you are trying to set.
This is because Swift does neither initialize the subarrays for you, neither increments the array length when assigning to an index.
For instance, the following code will fail:
var a = [Int]()
a[0] = 1
// fatal error: Index out of range
Instead, you can initialize an array with the number of elements you want to hold, filling it with a default value, zero for example:
var a = Array(repeating: 0, count: 100)
a[0] = 1
// a == [1, 0, 0, 0...]
To create an matrix of 100 by 100 initialized to 0 values:
var a = Array(repeating: Array(repeating: 0, count: 100), count: 100)
a[0][0] = 1
If you don't want to specify an initial size for your matrix, you can do it this way:
var a = [[Int]]()
a.append([])
a[0].append(1)

Default dictionary value in Swift

I know I can have a default dictionary value in swift, but I am struggling to do this for a tuple
i.e.
var freq = [Int:(Int,Int,Int)]()
I want to make a default value
freq[num.element, default: (0, num.offset, num.offset) ] = (7, 0, 0 )
This produces the frequency table with 7,0,0 for every value when the key does not exist.
Can I use the default for a more complicated dictionary?
For an example if we have an array of numbers [1,2,2,3,3,3]
we can count the number of elements using a frequency table
var freq = [Int:Int]()
for num in nums {
freq[num, default: 0] += 1
}
We want to store the initial position of each number, and the final position of each number in the frequency table so use
var freq = [Int:(Int,Int,Int)]()
for num in nums.enumerated() {
if freq[num.element] == nil {
freq[num.element] = (1,num.offset, num.offset)
} else {
freq[num.element] = (freq[num.element]!.0 + 1, freq[num.element]!.1, num.offset)
}
}
For this code I want to use a default value.
I am looking for a way of using a default value on a frequency list containing more than just a single value. It is not relevant that I am using a tuple rather than an array of values, for an example I want to find out how to use default with a tuple using arrays.
I tried to use the example above to make a default value, and on testing it does not work. I have looked at previous questions, a Google Search and looked at Apple's documentation.
Question: How to use default for a dictionary var freq = Int:(Int,Int,Int)
Is this what you looking for? Just get the current value with default value and store it in a variable first makes life easier.
var freq = [Int:(Int,Int,Int)]()
var nums = [1,2,2,3,3,3]
for num in nums.enumerated()
{
let currentTuple = freq[num.element, default: (0,num.offset,num.offset)]
freq[num.element] = (currentTuple.0 + 1, currentTuple.1,num.offset)
}
print(freq) //output: [1: (1, 0, 0), 2: (2, 1, 2), 3: (3, 3, 5)]

Remove data from struct bigger than a certain value

I have a struct, that's a <1x1 struct>, and I'm trying to edit a field in the struct based on the values. The field is called GeoDist_Actual and the struct is called GeoDist_str. The field GeoDist_Actual is a <262792x1 double>, and this is the code I was trying to use in order to get rid of the values that are greater than 1.609344e+05.
i =1;
for i=i:size(GeoDist_str.GeoDist_Actual)
if GeoDist_str.GeoDist_Actual(i,1 > 1.609344e+05
GeoDist_str.GeoDist_Acutal(i,1) = [];
end
end
How would I append or alter this code in order to make it function like I'm aiming? I considered setting all the values to 0, but I'm going to have to go backwards from this in order to get back GPS values, doing a reverse-Vincenty(spherical) calculation, and I'd like to just completely get rid of the values that don't comply with the if condition.
If I can narrow down the question at all, let me know, and thank you for your help in advance!
Edit: I've noticed that when I changed out the section
GeoDist_str.GeoDist_Actual(i,1) = [];
for
GeoDist_str.GeoDist_Actual(i,1) = 0;
It didn't actually solve anything, instead it didn't access the field "GeoDist_Actual" within the struct "GeoDist_str", it just created a mirror field with values of 0.
Consider this example:
% a 10-by-1 vector
x = [1;2;3;4;5;6;7;8;9;10];
% remove entries where the value is less than five
x(x<5) = [];
This is called logical indexing, no need for loops.
Consider the following simple example:
A.a = 1:5;
A =
a: [1 2 3 4 5]
now delete all elements bigger 3;
A.a = A.a( ~(A.a > 3) );
A =
a: [1 2 3]
or alternatively:
A.a( A.a > 3 ) = []
For your case it's a little more bulky:
GeoDist_str.GeoDist_Actual = ...
GeoDist_str.GeoDist_Actual( ...
~(GeoDist_str.GeoDist_Actual > 1.609344e+05) )