Question
A C function print_matrix(int rows, int cols, int matrix[rows][cols]) is designed to print a matrix. #include void print_matrix(int rows, int cols, int matrix[rows][cols]) { Β Β for (int i = 0; i
Solution
Correct Answer: B (The outer loop i <= rows will iterate for i = 0, 1, 2. For i = 2, it tries to access matrix[2], which is out of bounds for a 2-row matrix (indices 0 and 1).) β’ Dry Run: o Call print_matrix(2, 3, my_matrix): rows is 2, cols is 3. o The outer loop for (int i = 0; i <= rows; i++) will iterate for i = 0, i = 1, and i = 2. o When i = 0: The first row (matrix[0]) is printed correctly. o When i = 1: The second row (matrix[1]) is printed correctly. o When i = 2: The loop attempts to access matrix[2]. For a 2x3 matrix, the valid row indices are 0 and 1. Accessing matrix[2] is an out-of-bounds memory access. o This out-of-bounds access will lead to undefined behavior, most likely a segmentation fault (program crash). β’ Why Correct Answer (B): It will print the matrix and then attempt to access memory out of bounds, potentially causing a segmentation fault. o The first two rows will be printed correctly. Then, the loop will try to access a non-existent third row (matrix[2]), causing a memory access violation.
- Which layer is not in OSI but in TCP/IP
- The LRU replacement algorithm will select the page that:
- How do you correctly create a new Map object in JavaScript?
- What is a "smart object" in IoT?
- The Naive Pattern Searching algorithm has a worst-case time complexity of O(MN), where 'M' is the length of the pattern and 'N' is the length of the text. ...
- Which of the following statements accurately describes the use of the SQL CREATE statement?
- Which of the following statements is true regarding user-level threads compared to kernel-level threads?
- What is the time complexity for performing enqueue and dequeue operations on a queue implemented using a linked list?
- What is the time complexity of the KMP algorithm for searching a pattern of length 'M' in a text of length 'N'?
- For a sparse matrix, which representation method is generally preferred to save memory and improve computational efficiency?