Q:

Swift program to create a jagged array

belongs to collection: Swift Array Programs

0

Here, we will create a jagged array. In the jagged array, each row contains a different number of arguments.

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 jagged array is given below. The given program is compiled and executed successfully.

// Swift program to create a jagged array

import Swift

let twoArr: [[Int]] = [[1,3,5,7], [2,4,6],[8,9]]

// print first row.
print("First Row:")
print(twoArr[0][0])
print(twoArr[0][1])
print(twoArr[0][2])
print(twoArr[0][3])

// print second row.
print("Second Row:")
print(twoArr[1][0])
print(twoArr[1][1])
print(twoArr[1][2])

// print third row.
print("third Row:")
print(twoArr[2][0])
print(twoArr[2][1])

Output:

First Row:
1
3
5
7
Second Row:
2
4
6
Third Row:
8
9

...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 a jagged array twoArr. In the Jagged array, each row contains a different number of elements and printed the jagged array 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 two-dimensional array us... >>
<< Swift program to create a two-dimensional array...