Swift Program to read and print two-dimensional array


Just like other programming languages Swift also supported two- dimensional array. A two dimensional array is known as an array of array that store same type of data in a tabular format. Or we can say 2-D array is an array which represent homogeneous data in rows and columns.

Syntax

var twoDArray:[[Int]] = [[2, 3],[3, 4]]

To read two-dimensional array from the user Swift provide an inbuilt function named as readLine(). The readLine() function read a string of characters from the input. If the EOF has already been reached when this function is called, then it will return nil.

func readLine()
Or
func readLine(strippingNewline: true)

Here strippingNewline parameter is of boolean type. If the value of strippingNewline parameter is set to true, means the newline characters and character combination are stripped from the result. Otherwise they are preserved. By default value of strippingNewline parameter is true.

To print a 2-D array we can use nested for-in loop, The nested for-in loop iterate through each elements present in the rows and columns of the given 2-D array.

for x in 0..<rows {
   for y in 0..<cols {
      print(newArray[x][y], terminator: " ")
   }
   print("")
}

Here outer for-in loop represent the rows and inner for loop represent columns of the 2-D array. And subscript ‘newArray[x][y]’ is used to access the array elements.

Algorithm

  • Step 1 − Read the total number of rows and columns that a 2-D array is going to have from the user.

  • Step 2 − Create and initialise a 2-D array with default values.

  • Step 3 − Now read the array elements from the user.

  • Step 4 − After entering all the array elements display the result 2-D array on the output screen.

Example

In the following swift example, we will first read the size of the rows and columns from the user using the readLine() function. Then we create and initialize a 2-D array with default values of 0. Now we read each element of the 2-D array from the user according to their location for (0, 1) element is 2, (0, 2) element is 4, and so on, here 0 represents the 0th row and 1 represents the 1st column. After entering all the array elements display the final 2-D array.

import Foundation
import Glibc

// Enter the size of rows and columns
print("Enter the size of row")
let rows = Int(readLine()!)!
print("Enter the size of column")
let cols = Int(readLine()!)!

// Initialise a 2-D array with default values
var newArray = [[Int]](repeating: [Int](repeating: 0, count: cols), count: rows)

// Enter array elements
for x in 0..<rows {
   for y in 0..<cols {
      print("Enter element (\(x),\(y)): ")
      if let input = readLine(), let ele = Int(input) {
         newArray[x][y] = ele
      } else {
         print("Input is not valid. Please enter an integer value.")
      }
   }
}

// Displaying resultant array
print("\nFinal \(rows)x\(cols) matrix is:")
for x in 0..<rows {
   for y in 0..<cols {
      print(newArray[x][y], terminator: " ")
   }
   print("")
}

Input

Enter the size of row
3
Enter the size of column
3
Enter element (0,0): 
3
Enter element (0,1): 
2
Enter element (0,2): 
4
Enter element (1,0): 
2
Enter element (1,1): 
6
Enter element (1,2): 
7
Enter element (2,0): 
4
Enter element (2,1): 
3
Enter element (2,2): 
7

Output

Final 3x3 matrix is:
3 2 4 
2 6 7 
4 3 7 

Conclusion

So this is how we can read and print two-dimensional array. Using the above method you can create an type of matrix like 2x2, 3x4, 4x3, etc. Also while working with readLine() function you have to use initialisers(like for integer data type use Int(), for double data type use Double()) to convert the result into desired data type because readLine() function always return result in String type.

Updated on: 24-Apr-2023

340 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements