Search code examples
iosswiftmultidimensional-arrayswift-playground

swift- get exc_bad_instruction (code=exc_i386_invop, subcode=0x0) with array value replacement in while loop


I am making a note writing app. In this app I am turning the lines into a bezier curve. At the moment I am getting an error when I try to put new values into a matrix that was defined outside of the while loop. I have tried the expression by itself when put into a dummy variable and it works in the loop. When I use the format for replacing a value of the two dimensions variable it works outside the loop, however, when I do both those actions together in the loop it does not work.

//: Playground - noun: a place where people can play

import UIKit


var x2 = 6.0
var y2 = 5.0
var x1 = 1.0
var y1 = 1.0

var p1x = 2.0
var p1y = 3.0

var B = 6.5
var Ba = 2.2
var percentall = Ba/B

var yall = 5.0
var xall = 1.0
var A = 4.0
var C = 5.0
var Ct = 0.0
var At = 0.0
var Aall = Int(A) * 10
var Call = Int(C) * 10


var thelistx = [[Double]](count: Call, repeatedValue: [Double](count: Call, repeatedValue: 0.0 ))
var thelisty = Array(count: Aall, repeatedValue: Array(count: Call, repeatedValue: 0.0 ))


var theAt = 0
var theCt = 0


while At <= A {
    var Apercent = percentall * At
    var Aend = y1 + At
    var yslope = (A - At) * percentall + Aend
    var lefty = (yslope - Apercent) * percentall + Apercent
    var righty = ( y2 - yslope) * percentall + yslope
    while Ct <= C {
        var Cpercent = percentall * Ct
        var Cend = x2 - Ct
        var xslope = (C - Ct) * percentall + Cend
        var leftx = (xslope - x1) * percentall + x1
        var rightx = (x2 - xslope) * percentall + xslope

        thelistx [theAt] [theCt] = (rightx - leftx) * percentall + left

*********************Execution was interrupted, reason exc_bad_instruction (code=exc_i386_invop, subcode=0x0) ***************

        thelisty [theAt] [theCt] = (righty - lefty) * percentall + lefty

        Ct +=  0.1
        theCt += 1
    }
    At = At + 0.1
    theAt += 1
}

Solution

  • The console shows what your error is:

    fatal error: Index out of range

    You're going out of bounds with thelistx [theAt] [theCt]

    Changing while Ct < C to Ct < C - 1 keeps your calls in the bounds of the array.