Reset Matrix Help

Hi there!

can someone help me with this one:

func resetMatrix() {
    matrix = Array(repeating: Array(repeating: 0, count: numberOfColumns), count: numberOfRows)
    
    let coordinates = [
        (selectedRow1, selectedColumn1),
        (selectedRow2, selectedColumn2),
        (selectedRow3, selectedColumn3)
    ]
    
    for (row, column) in coordinates {
        matrix[row - 1][column - 1] += 1
    }
}

i can't seem to make it work.

i want it to work like this: If the chosen coordinate in Question 1 is (2,2), it will place the value 1 in the matrix coordinate (2,2). If the chosen coordinate in Question 2 is also (2,2), it will increment the value in the matrix coordinate (2,2) by 1, making it 2. Similarly, if the chosen coordinate in Question 3 is (2,2), it will increment the value in the matrix coordinate (2,2) by 1 again, making it 3.

Thank you!

for anyone who might have the same question/problem. here's the sample that works as I wanted.

import SwiftUI

struct ContentView: View { let numberOfRows = 5 let numberOfColumns = 5 @State var matrix: [[Int]] = Array(repeating: Array(repeating: 0, count: 5), count: 5) @State var selectedRow1 = 0 @State var selectedColumn1 = 0 @State var selectedRow2 = 0 @State var selectedColumn2 = 0 @State var selectedRow3 = 0

var body: some View {
    VStack {
        Image(systemName: "globe")
            .imageScale(.large)
            .foregroundColor(.accentColor)
        Text("Hello, world!")
        
        Form {
            Section(header: Text("Question 1")) {
                Picker("Select a row:", selection: $selectedRow1) {
                    ForEach(1...numberOfRows, id: \.self) { row in
                        Text("\(row)")
                    }
                }
                .pickerStyle(SegmentedPickerStyle())
                
                Picker("Select a column:", selection: $selectedColumn1) {
                    ForEach(1...numberOfColumns, id: \.self) { column in
                        Text("\(column)")
                    }
                }
                .pickerStyle(SegmentedPickerStyle())
            }
            
            Section(header: Text("Question 2")) {
                Picker("Select a row:", selection: $selectedRow2) {
                    ForEach(1...numberOfRows, id: \.self) { row in
                        Text("\(row)")
                    }
                }
                .pickerStyle(SegmentedPickerStyle())
                
                Picker("Select a column:", selection: $selectedColumn2) {
                    ForEach(1...numberOfColumns, id: \.self) { column in
                        Text("\(column)")
                    }
                }
                .pickerStyle(SegmentedPickerStyle())
            }
            
            Section(header: Text("Question 3")) {
                Picker("Select a row:", selection: $selectedRow3) {
                    ForEach(1...numberOfRows, id: \.self) { row in
                        Text("\(row)")
                    }
                }
                .pickerStyle(SegmentedPickerStyle())
                
                Picker("Select a column:", selection: $selectedColumn3) {
                    ForEach(1...numberOfColumns, id: \.self) { column in
                        Text("\(column)")
                    }
                }
                .pickerStyle(SegmentedPickerStyle())
            }
        }
        
        Button(action: {
            resetMatrix()
            
            matrix[selectedRow1 - 1][selectedColumn1 - 1] += 1
            matrix[selectedRow2 - 1][selectedColumn2 - 1] += 1
            matrix[selectedRow3 - 1][selectedColumn3 - 1] += 1
            let selectedValue1 = matrix[selectedRow1 - 1][selectedColumn1 - 1]
            let selectedValue2 = matrix[selectedRow2 - 1][selectedColumn2 - 1]
            let selectedValue3 = matrix[selectedRow3 - 1][selectedColumn3 - 1]
            print("Selected Coordinate (Question 1): (\(selectedRow1), \(selectedColumn1))")
            print("Value at Coordinate (Question 1): \(selectedValue1)")
            print("Selected Coordinate (Question 2): (\(selectedRow2), \(selectedColumn2))")
            print("Value at Coordinate (Question 2): \(selectedValue2)")
            print("Selected Coordinate (Question 3): (\(selectedRow3), \(selectedColumn3))")
            print("Value at Coordinate (Question 3): \(selectedValue3)")
        }) {
            Text("Show Selected Coordinates")
                .padding()
                .background(Color.blue)
                .foregroundColor(.white)
                .cornerRadius(10)
        }
        .padding()
        
        VStack {
            ForEach(0..<numberOfRows, id: \.self) { row in
                HStack {
                    ForEach(0..<numberOfColumns, id: \.self) { column in
                        let value = matrix[row][column]
                        Text("\(value)")
                            .frame(width: 50, height: 50)
                            .border(Color.gray)
                            .id("\(row)-\(column)")
                    }
                }
            }
        }
    }
    .padding()
}

func resetMatrix() {
    matrix = Array(repeating: Array(repeating: 0, count: numberOfColumns), count: numberOfRows)
}

}

struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }

Reset Matrix Help
 
 
Q