282,060 views
17 votes
17 votes
Write a function that takes a number of rows, a number of columns, and a string and returns a two-dimensional array of the dimensions specified where each element is the string given. Sample run: mat = fill_str(4, 2, "2D​") print(mat) Prints the following: [['2D', '2D'], ['2D', '2D'], ['2D', '2D'], ['2D', '2D']]

User Noran
by
2.9k points

1 Answer

25 votes
25 votes

Answer:

Step-by-step explanation:

The following code is written in Python. The function, as asked, takes in three parameters and creates a 2D array with the dimensions specified in the inputs and fills it with the element passed for the third parameter.

def createArray(rows, columns, element):

my_array = []

for x in range(rows):

sub_arr = []

for y in range(columns):

sub_arr.append(element)

my_array.append(sub_arr)

return my_array

Write a function that takes a number of rows, a number of columns, and a string and-example-1
User Intellimath
by
3.3k points