Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Multiply Array position Swift

I need to fix this code in Swift language. I don’t know how can I to multiply array positions mut1 and mut2 values. The result is 8.

var mut1 = [7,-4,5]
var mut2 = [3,2,-1]

var multiply = mut1[0] * mut2[0] + mut1[1] * mut2[1] + mut1[2] * mut2[2]

print(multiply)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If you want a Swift-like solution, you could try this:

var mut1 = [7,-4,5]
var mut2 = [3,2,-1]

var multiply = zip(mut1, mut2).map { $0 * $1 }.reduce(0, +)

print(multiply) // 8
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading