- for(int i = 0; i < cols; i++) for(int j = 0; j < rows; j++) rowSum[i] += arri;
- for(int i = 0; i < rows; i++) for(int j = 0; j < cols; j++) rowSum[i] += arri;
- Both A and C
Explanation:
The correct answer is option "c". This method uses a nested loop to run through the 2D array arr's elements row by row. Every row (indexed by i) is traversed by the outer loop, while the inner loop traverses every column (indexed by j). The rowSum[i] function effectively accumulates the sum of elements for each row by adding the value of each element arri to rowSum[i]. It ensures that the sum for every row is computed accurately.
- What is the correct method to find the minimum element in a 2D array arr?
- for(int i = 0; i < cols; i++) for(int j = 0; j < rows; j++) if(arri < min) min = arri;
- for(int i = 0; i < rows; i++) for(int j = 0; j < cols; j++) if(arri < min) min = arri;
- for(int i = 0; i < rows; i++) if(arr[i] < min) min = arr[i];
- Both A and B
Explanation:
The correct answer is option "b". In this method, every element of the 2D array arr is iterated over using a nested loop. Iterating over the rows (indexed by i) is as part of the outer loop, while the inner loop iterates over the columns (indexed by j). It determines if an element is less than the current minimum (min) for each arri. If it is accurate, it uses arri to update min value. By doing this, the smallest element in the complete array is ensured to be found.
- How can the elements of a 2D array be reversed column-wise?
- for(int i = 0; i < cols; i++) for(int j = 0, k = rows - 1; j < k; j++, k--) swap(arrj, arrk);
- for(int i = 0; i < rows; i++) for(int j = 0; j < cols; j++) swap(arrj, arrrows - j - 1);
- for(int i = 0; i < rows; i++) for(int j = 0; j < cols; j++) swap(arri, arrrows - i - 1);
- Both A and B
Explanation:
The correct answer is option "a". This method uses a nested loop to reverse the elements of each column in a 2D array arr. The inner loop switches the elements in the column, starting at the top and then moving towards the center, using two indices (j and k), while the outer loop iterates throughout the columns (indexed by i). The arrj and arrk methods are swapped until every element in every column is in the reverse order. After doing this, the required column-wise reversal occurs because each column's elemental order is ensured to be reversed.
- How can the diagonal elements of a 2D array "arr" be accessed?
- arri
- arri
- arrrows - i - 1
- arrrows - i - 1
Explanation:
The correct answer is option "b". The notation arri is used in a 2D array arr to access the diagonal elements (where the row index i equals the column index i). The diagonal elements of the array from the top-left to the bottom-right corners are immediately accessible using this expression. Iterating over or accessing diagonal elements in algorithms requiring diagonal processing, such as matrix operations or diagonal traversal tasks, is a simple and effective way.
- When int arr4; is declared, what is the index of the last element?
- arr5
- arr4
- arr4
- arr3
Explanation:
The accurate choice is option "d". An array that has 4 rows and 5 columns is denoted as "arr" in the statement int arr4;. Given that array indexing commences at 0 in C and C++, the indices for arr span from arr0 to arr3. The fourth row, identified by index 3, is retrieved using arr[3]. As the acceptable row indices range from 0 to 3, referencing arr[4] would exceed the bounds of the array.
The final element within the array, specified as int arr4; can be retrieved using arr3, representing the item located in the fourth row and fifth column.