Q:

Swift program to create a two-dimensional array using the append() function

belongs to collection: Swift Array Programs

0

Here, we will create an empty 2D array and then create rows and added using the append() function into a created 2D array.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

// 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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Swift program to create a three-dimensional array... >>
<< Swift program to create a jagged array...