In discussing working with Two-Dimensional Arrays, please explain the following with the most simple, begiinger level, easiest to understand explaination (no coding example nessecary):
With a two-dimensional array, for which operations would you use nested “for” loops and for which operations would you use a single “for” loop?
When performing an operation on a given row, which index is fixed and which index is used as the looping variable?
When performing an operation on a given column, which index is fixed and which index is used as the looping variable?
Expert Answer
With a two-dimensional array, for which operations would you use nested “for” loops and for which operations would you use a single “for” loop?
The elements of a two-dimensional array are arranged in rows and columns and are with two pairs of square brackets like, string[][] where the first square bracket represents the number of rows and the second square bracket represents the number of columns.
While performing operations on the two-dimensional array if we need to traverse through both the rows and columns of the two-dimensional array we use nested “for loops”.
For example, if we want to print the two-dimensional array or if we need to search for a particular element in the array we need to traverse both the elements in the rows and the elements in the columns in such cases we use nested “for loops”.
While performing operations on the two-dimensional array if we need to traverse through either only the rows or columns of the two-dimensional array we use single “for loop”.
For example, if we want to print a particular row or column of a two-dimensional array we use single “for loop”.
When performing an operation on a given row, which index is fixed and which index is used as the looping variable?
As we know a two-dimensional array is represents with two square brackets, where the first square bracket represents the index of the row and the second square bracket represents the index of the column. When performing an operation on a given row the first index representing the row is fixed and the second index representing the column must be used as a looping variable.
When performing an operation on a given column, which index is fixed and which index is used as the looping variable?
As we know a two-dimensional array is represents with two square brackets, where the first square bracket represents the index of the row and the second square bracket represents the index of the column. When performing an operation on a given column the second index representing the column is fixed and the first index representing the rows must be used as a looping variable.