Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,959 questions

51,901 answers

573 users

How to create a 2D array filled with zeros in Swift

2 Answers

0 votes
import Foundation

var arr = Array(repeating: Array(repeating: 0, count: 5), count: 5)
 
arr[2][0] = 9
 
for array in arr {
    for value in array {
        print(value, terminator: " ")
    }
    print(" ")
}

 
 
/*
run:
 
0 0 0 0 0  
0 0 0 0 0  
9 0 0 0 0  
0 0 0 0 0  
0 0 0 0 0 
 
*/

 



answered Sep 19, 2020 by avibootz
edited Jan 10, 2025 by avibootz
0 votes
import Foundation
 
let rows = 2
let columns = 3
 
// Create a 2D array filled with zeros
var array = Array(repeating: Array(repeating: 0, count: columns), count: rows)
 
print(array)
 
 
 
/*
run:
 
[[0, 0, 0], [0, 0, 0]]
 
*/

 



answered Jan 10, 2025 by avibootz
...