在2d数组NxN Swift上创建,随机填充和搜索元素

时间:2021-04-28 21:34:54

I create an array of numbers, trying to fill in their rule or random size NxN, then looking for the amount in each of the diagonals and deduce it. Help to understand the errors that I noted in the comments. What am I doing wrong? Thank you!

我创建了一个数字数组,试图填写他们的规则或随机大小NxN,然后查找每个对角线中的数量并推导出它。帮助理解我在评论中提到的错误。我究竟做错了什么?谢谢!

func diagonals (n:Int) -> String{
        var sumFirst: Int
        var sumSecond: Int

        var multArray = Array(count: n, repeatedValue: Array(count: n, repeatedValue: 0))
                print(multArray) //create 2d array NxN with Initialisation

        for var i in multArray {
            for var k in multArray {
                multArray[i][k] = i+k // fill array elements the sum of the indices (error line) or we can fill it random, but how?
                if i==k { //check element on the main diagonal
                     sumFirst += multArray[i][k]  // do sum (error line)
                }
                if (i+k) == n  { //check element on secondary diagonal (error line)
                    sumSecond += multArray[i][k] // do sum (error line)
                }
            }
        }

        return "Sum elemets on main diagonal =\(sumFirst) and second =\(sumSecond)"
    }

    diagonals(3)

1 个解决方案

#1


1  

Solved!

    func diagonals (n:Int) -> String{
    var sumFirst = 0
    var sumSecond = 0

    var multArray = Array(count: n, repeatedValue: Array(count: n, repeatedValue: 0))
            print(multArray) //create 2d array NxN with Initialisation

    for indexI in multArray.indices {
        for indexK in multArray.indices {
            multArray[indexI][indexK] = Int(arc4random_uniform(UInt32(n))) // fill array elements randomly
            if indexI==indexK { //check element on the main diagonal
                 sumFirst += multArray[indexI][indexK]  // do sum
            }
            if (indexI+indexK + 1) == n  { //check element on secondary diagonal
                sumSecond += multArray[indexI][indexK] // do sum
            }
        }
    }
print(multArray)
    return "Sum elemets on main diagonal =\(sumFirst) and second =\(sumSecond)"
}

diagonals(3)

#1


1  

Solved!

    func diagonals (n:Int) -> String{
    var sumFirst = 0
    var sumSecond = 0

    var multArray = Array(count: n, repeatedValue: Array(count: n, repeatedValue: 0))
            print(multArray) //create 2d array NxN with Initialisation

    for indexI in multArray.indices {
        for indexK in multArray.indices {
            multArray[indexI][indexK] = Int(arc4random_uniform(UInt32(n))) // fill array elements randomly
            if indexI==indexK { //check element on the main diagonal
                 sumFirst += multArray[indexI][indexK]  // do sum
            }
            if (indexI+indexK + 1) == n  { //check element on secondary diagonal
                sumSecond += multArray[indexI][indexK] // do sum
            }
        }
    }
print(multArray)
    return "Sum elemets on main diagonal =\(sumFirst) and second =\(sumSecond)"
}

diagonals(3)