The source code to create a two-dimensional array using the append() function is given below. The given program is compiled and executed successfully.
// Swift program to create a two-dimensional array
// using append() function
import Swift
var twoDArr=[[Int]]()
var row1 = [Int]()
row1.append(1)
row1.append(3)
row1.append(5)
twoDArr.append(row1)
var row2 = [Int]()
row2.append(2)
row2.append(4)
row2.append(6)
twoDArr.append(row2)
print("Two Dimensional array: ",twoDArr)
Output:
Two Dimensional array: [[1, 3, 5], [2, 4, 6]]
...Program finished with exit code 0
Press ENTER to exit console.
Explanation:
In the above program, we imported a package Swift to use the print() function using the below statement,
import Swift;
Here, we created an empty 2D array twoDArr. Then we created two rows and added both rows into the created 2D array using the append() function and printed the result on the console screen.
Program/Source Code:
The source code to create a two-dimensional array using the append() function is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we imported a package Swift to use the print() function using the below statement,
Here, we created an empty 2D array twoDArr. Then we created two rows and added both rows into the created 2D array using the append() function and printed the result on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer