how can i possible sum in diagonal 3 arrays?
example:
[6, 6, 5]
[10, 5, 10]
[3, 20, 1]
from left to right, and right to left
6 + 5 + 1 = 12
3 + 5 + 5 = 13
my code:
import UIKit
class exerViewController: UIViewController {
@IBOutlet weak var num1: UITextField!
@IBOutlet weak var num2: UITextField!
@IBOutlet weak var num3: UITextField!
@IBOutlet weak var num4: UITextField!
@IBOutlet weak var num5: UITextField!
@IBOutlet weak var num6: UITextField!
@IBOutlet weak var num7: UITextField!
@IBOutlet weak var num8: UITextField!
@IBOutlet weak var num9: UITextField!
@IBOutlet weak var result: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func leftTOright(_ sender: UIButton) {
if num1.text == "" || num5.text == "" || num9.text == ""{
displayAlert(title: “USER”, message: “FIELDS 1/5/9ARE EMPTY“)
}
else{
// 1 array
let a:Int? = Int(num1.text!)
let b:Int? = Int(num2.text!)
let c:Int? = Int(num3.text!)
// 2 array
let d:Int? = Int(num4.text!)
let e:Int? = Int(num5.text!)
let f:Int? = Int(num6.text!)
//3 array
let g:Int? = Int(num7.text!)
let h:Int? = Int(num8.text!)
let i:Int? = Int(num9.text!)
let oddNumbers = [a, e, i]
let numberSum = oddNumbers.reduce(0, { x, y in
x + y!
})
resultado.text = “result: left to right \(numberSum)"
}
}
@IBAction func rightTOleft(_ sender: UIButton) {
if num3.text == "" || num5.text == "" || num7.text == ""{
displayAlert(title: “USER”, message: “FIELDSS 3/5/7 ARE EMPTY“)
}
else{
// 1 array
let a:Int? = Int(num1.text!)
let b:Int? = Int(num2.text!)
let c:Int? = Int(num3.text!)
// 2 array
let d:Int? = Int(num4.text!)
let e:Int? = Int(num5.text!)
let f:Int? = Int(num6.text!)
//3 array
let g:Int? = Int(num7.text!)
let h:Int? = Int(num8.text!)
let i:Int? = Int(num9.text!)
let oddNumbers = [c, e, g]
let numberSum = oddNumbers.reduce(0, { x, y in
x + y!
})
resultado.text = “result right to left \(numberSum)"
}
}
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
self.view.endEditing(true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func displayAlert (title:String, message:String){
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
/*
@IBAction func dereAizquie(_ sender: Any) {
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}i have 9 numbers and i only use
numbers 1,5,9 to make the left to right diagonal sum
let a:Int? = Int(num1.text!)
let e:Int? = Int(num5.text!)
let i:Int? = Int(num9.text!)
let oddNumbers = [a, e, i]
let numberSum = oddNumbers.reduce(0, { x, y in
x + y!
})
resultado.text = “result: left to right \(numberSum)"as you can see i'm not displaying my array's im only taking the value that i need
like this
let oddNumbers = [c, e, g]
let numberSum = oddNumbers.reduce(0, { x, y in
x + y!
})
resultado.text = “result right to left \(numberSum)"is there a smart way to do it?, by taking the completly array and take the values that i need?