Unit 4 subjective questions Arrays in C
CSE101 — Computer Programming 20 questions with model answers. Write yours first, then compare.
-
Question 1 of 20
Define an array in C. Explain the syntax for declaring and initializing a One-Dimensional (1D) array with examples.
Model answer
Definition: An array is a collection of variables of the same data type that are referenced by a common name. The specific element in an array is accessed by an index.\n\n1. Declaration Syntax:\nTo declare an array, you specify the type of the elements, the name of the array, and the number of elements (size).\n\nExample:
int marks[5];declares an integer array of size 5.\n\n2. Initialization:\nArrays can be initialized at the time of declaration.\n\n Complete Initialization:\nint numbers[5] = {10, 20, 30, 40, 50};\n Partial Initialization:\nint numbers[5] = {10, 20};(Remaining elements become 0).\n* Initialization without Size:\nint numbers[] = {1, 2, 3};(Compiler determines size as 3). -
Question 2 of 20
Explain how elements of a 1D array are stored in memory. If the base address of an integer array is 1000 and the size of an integer is 2 bytes, calculate the address of .
Model answer
Memory Storage:\nElements of a 1D array are stored in contiguous memory locations. The index starts from 0.\n\nAddress Calculation Formula:\nThe address of the element is calculated as:\n\n\nCalculation:\n Base Address () = 1000\n Size of integer () = 2 bytes\n* Index () = 5\n\n\n\n\n\nTherefore, the element is stored at address 1010.
-
Question 3 of 20
Write a C program to find the sum and average of elements stored in an array.
Model answer
Here is the C program to find the sum and average:\n\nc\n#include <stdio.h>\n\nint main() {\n int n, i;\n float sum = 0, average;\n int arr[100];\n\n printf("Enter the number of elements: ");\n scanf("%d", &n);\n\n // Input array elements\n for(i = 0; i < n; ++i) {\n printf("Enter number %d: ", i + 1);\n scanf("%d", &arr[i]);\n sum += arr[i]; // Add to sum\n }\n\n average = sum / n;\n\n printf("Sum = %.2f\n", sum);\n printf("Average = %.2f\n", average);\n\n return 0;\n}\n\n\nLogic:\n1. Read the number of elements .\n2. Use a loop to accept input and simultaneously add the value to the
sumvariable.\n3. Dividesumbynto get theaverage. -
Question 4 of 20
Differentiate between One-Dimensional and Two-Dimensional arrays. Provide the syntax for declaring a 2D array.
Model answer
Differences:\n\n| Feature | One-Dimensional (1D) Array | Two-Dimensional (2D) Array |\n| :--- | :--- | :--- |\n| Structure | Linear list of elements. | Table of elements (Rows and Columns). |\n| Subscript | Uses one subscript (e.g.,
a[i]). | Uses two subscripts (e.g.,a[i][j]). |\n| Memory | Stored contiguously. | Stored as a sequence of 1D arrays (Row-major in C). |\n| Use Case | Storing lists, vectors. | Storing matrices, tables. |\n\n2D Array Syntax:\n\n\nExample:int matrix[3][3];declares a matrix with 3 rows and 3 columns. -
Question 5 of 20
Explain Row-Major and Column-Major ordering for storing 2D arrays. Derive the address calculation formula for Row-Major order.
Model answer
1. Row-Major Order:\nIn this method, elements are stored row by row. The entire first row is stored, followed by the second row, and so on. C uses Row-Major ordering.\n\n2. Column-Major Order:\nElements are stored column by column. The entire first column is stored, followed by the second column, etc.\n\nAddress Calculation (Row-Major):\nGiven an array (where is rows, is columns), Base Address , and element size .\nTo find the address of element at row and column ():\n\nWe must skip full rows to reach the starting of the row. Each row has elements.\nTotal elements skipped = .\n\nFormula:\n
-
-
Question 6 of 20
Write a C program to perform Matrix Addition for two matrices.
Model answer
c\n#include <stdio.h>\n\nint main() {\n int a[3][3], b[3][3], sum[3][3];\n int i, j;\n\n // Input Matrix A\n printf("Enter elements of 1st matrix:\n");\n for (i = 0; i < 3; ++i)\n for (j = 0; j < 3; ++j)\n scanf("%d", &a[i][j]);\n\n // Input Matrix B\n printf("Enter elements of 2nd matrix:\n");\n for (i = 0; i < 3; ++i)\n for (j = 0; j < 3; ++j)\n scanf("%d", &b[i][j]);\n\n // Adding Matrices\n for (i = 0; i < 3; ++i)\n for (j = 0; j < 3; ++j)\n sum[i][j] = a[i][j] + b[i][j];\n\n // Displaying Result\n printf("Sum of two matrices: \n");\n for (i = 0; i < 3; ++i) {\n for (j = 0; j < 3; ++j) {\n printf("%d ", sum[i][j]);\n }\n printf("\n");\n }\n return 0;\n}\n
-
Question 7 of 20
Describe the mechanism of passing arrays to functions in C. How does passing a 1D array differ from passing a single variable?
Model answer
Passing Mechanism:\nWhen an array is passed to a function, the base address (address of the first element) of the array is passed, not a copy of the entire array. This is effectively Call by Reference.\n\nSyntax:\n Function Prototype:
void myFunction(int arr[], int size);\n Function Call:myFunction(myArray, 5);(Note: brackets[]are not used in the call).\n\nDifference from Single Variable:\n1. Single Variable: Usually passed by value (a copy is created). Changes inside the function do not affect the original variable.\n2. Array: Passed by address. Any modification made to the array elements inside the function will modify the original array in the calling function. -
Question 8 of 20
Write a C program to multiply two matrices. Include checks for compatibility of matrix dimensions.
Model answer
Logic: Two matrices and can be multiplied only if . The resulting matrix will be .\n\nc\n#include <stdio.h>\n\nint main() {\n int a[10][10], b[10][10], mul[10][10];\n int m, n, p, q, i, j, k;\n\n printf("Enter rows and columns for first matrix: ");\n scanf("%d %d", &m, &n);\n printf("Enter rows and columns for second matrix: ");\n scanf("%d %d", &p, &q);\n\n if (n != p) {\n printf("Error! Matrices cannot be multiplied.");\n } else {\n // Input A\n printf("Enter elements of matrix A:\n");\n for (i = 0; i < m; i++)\n for (j = 0; j < n; j++)\n scanf("%d", &a[i][j]);\n\n // Input B\n printf("Enter elements of matrix B:\n");\n for (i = 0; i < p; i++)\n for (j = 0; j < q; j++)\n scanf("%d", &b[i][j]);\n\n // Initialization of result matrix\n for (i = 0; i < m; i++)\n for (j = 0; j < q; j++)\n mul[i][j] = 0;\n\n // Multiplication Logic\n for (i = 0; i < m; i++) {\n for (j = 0; j < q; j++) {\n for (k = 0; k < n; k++) {\n mul[i][j] += a[i][k] * b[k][j];\n }\n }\n }\n\n // Output\n printf("Resultant Matrix:\n");\n for (i = 0; i < m; i++) {\n for (j = 0; j < q; j++)\n printf("%d\t", mul[i][j]);\n printf("\n");\n }\n }\n return 0;\n}\n
-
Question 9 of 20
Explain the algorithm to insert an element into a specific position in a 1D array.
Model answer
Algorithm for Insertion:\nAssuming an array
arrwithnelements and capacity to hold more.\n\n1. Input: Read the element to be inserted (key) and the position (pos) where it needs to be inserted.\n2. Validation: Check if the array is full. If yes, print "Overflow".\n3. Shift Elements:\n Start a loop from the last index (n-1) down to the target index (pos-1).\n Move the element at current indexitoi+1.\n \n4. Insert: Place thekeyat the specified position.\n \n5. Update Size: Increment the number of elements by 1. -
Question 10 of 20
Write a function in C to delete an element from an array given its position.
Model answer
c\nvoid deleteElement(int arr[], int n, int pos) {\n // pos is the user-friendly position (1-based)\n // n is the pointer to the current size of array\n \n if (pos < 1 || pos > n) {\n printf("Invalid position!\n");\n return;\n }\n\n // Shifting elements to the left to overwrite the deleted element\n // Loop starts from index pos-1 (the element to delete)\n for (int i = pos - 1; i < n - 1; i++) {\n arr[i] = arr[i + 1];\n }\n\n // Decrement the size of the array\n (*n)--;\n printf("Element deleted successfully.\n");\n}\n\nExplanation:\n1. The function takes the array, a pointer to the size (so the change persists), and the position.\n2. It shifts all elements from
posto the end one step to the left ().\n3. Finally, it reduces the count of elements. -
Question 11 of 20
What is Linear Search? Write a C program to implement Linear Search.
Model answer
Definition:\nLinear Search (or Sequential Search) is the simplest searching algorithm. It traverses the array sequentially from the first element to the last element, checking if the current element matches the search key.\n\nC Program:\nc\n#include <stdio.h>\n\nint main() {\n int array[100], search, c, n;\n\n printf("Enter number of elements in array\n");\n scanf("%d", &n);\n\n printf("Enter %d integer(s)\n", n);\n for (c = 0; c < n; c++)\n scanf("%d", &array[c]);\n\n printf("Enter a number to search\n");\n scanf("%d", &search);\n\n for (c = 0; c < n; c++) {\n if (array[c] == search) {\n printf("%d is present at location %d.\n", search, c+1);\n break;\n }\n }\n\n if (c == n)\n printf("%d isn't present in the array.\n", search);\n\n return 0;\n}\n
-
Question 12 of 20
Explain the logic of Binary Search. What is the mandatory prerequisite for Binary Search? Calculate its Time Complexity.
Model answer
Logic:\nBinary Search is a divide-and-conquer algorithm.\n1. Compare the search key with the middle element of the array.\n2. If the key matches the middle element, the search is successful.\n3. If the key is smaller than the middle element, repeat the search in the left half.\n4. If the key is larger, repeat the search in the right half.\n5. Repeat until the key is found or the interval is empty.\n\nPrerequisite:\nThe array must be sorted (in ascending or descending order).\n\nTime Complexity:\nIn every step, the search space is divided by 2.\n Best Case: (Element found at middle).\n Worst Case: .
-
Question 13 of 20
Write a C program to implement Binary Search algorithm recursively or iteratively.
Model answer
Iterative Binary Search Implementation:\nc\n#include <stdio.h>\n\nint binarySearch(int arr[], int l, int r, int x) {\n while (l <= r) {\n int m = l + (r - l) / 2;\n\n // Check if x is present at mid\n if (arr[m] == x)\n return m;\n\n // If x greater, ignore left half\n if (arr[m] < x)\n l = m + 1;\n\n // If x is smaller, ignore right half\n else\n r = m - 1;\n }\n return -1; // Not found\n}\n\nint main() {\n int arr[] = {2, 3, 4, 10, 40};\n int n = sizeof(arr) / sizeof(arr[0]);\n int x = 10;\n int result = binarySearch(arr, 0, n - 1, x);\n \n if(result == -1)\n printf("Element not found");\n else\n printf("Element found at index %d", result);\n return 0;\n}\n
-
Question 14 of 20
Compare Linear Search and Binary Search based on efficiency and requirements.
Model answer
| Criteria | Linear Search | Binary Search |\n| :--- | :--- | :--- |\n| Prerequisite | None (Array can be unsorted). | Array must be sorted. |\n| Approach | Sequential access. | Divide and Conquer. |\n| Time Complexity | (Proportional to size). | (Logarithmic). |\n| Efficiency | Slower for large datasets. | Very fast for large datasets. |\n| Implementation | Simple. | Slightly complex (Index calculations). |\n| Dataset Access | Can work on Random or Sequential access (like Linked List). | Requires Random Access (Direct indexing needed). |
-
Question 15 of 20
What is Bubble Sort? Explain its working principle with an example pass.
Model answer
Definition:\nBubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.\n\nWorking Principle:\n1. Compare and . If , swap them.\n2. Compare and . Swap if necessary.\n3. Continue this until the end of the array. After the first pass, the largest element "bubbles up" to the last position.\n4. Repeat the process for the remaining elements (, , etc.).\n\nExample Pass (Array: 5, 1, 4, 2, 8):\n ( 5 1 4 2 8 ) ( 1 5 4 2 8 ) // Swap 5, 1\n ( 1 5 4 2 8 ) ( 1 4 5 2 8 ) // Swap 5, 4\n ( 1 4 5 2 8 ) ( 1 4 2 5 8 ) // Swap 5, 2\n ( 1 4 2 5 8 ) ( 1 4 2 5 8 ) // No Swap\n* Largest element 8 is now at the end.
-
Question 16 of 20
Write a C program to sort an array of integers in ascending order using Bubble Sort.
Model answer
c\n#include <stdio.h>\n\nint main() {\n int array[100], n, c, d, swap;\n\n printf("Enter number of elements\n");\n scanf("%d", &n);\n\n printf("Enter %d integers\n", n);\n for (c = 0; c < n; c++)\n scanf("%d", &array[c]);\n\n // Bubble Sort Logic\n for (c = 0; c < n - 1; c++) {\n for (d = 0; d < n - c - 1; d++) {\n if (array[d] > array[d+1]) { / For descending order use < /\n swap = array[d];\n array[d] = array[d+1];\n array[d+1] = swap;\n }\n }\n }\n\n printf("Sorted list in ascending order:\n");\n for (c = 0; c < n; c++)\n printf("%d\n", array[c]);\n\n return 0;\n}\n
-
Question 17 of 20
Trace the Bubble Sort algorithm for the input array:
{64, 34, 25, 12, 22}. Show the array status after every pass.Model answer
Input:
{64, 34, 25, 12, 22}\n\nPass 1:\n Compare 64, 34 -> Swap ->{34, 64, 25, 12, 22}\n Compare 64, 25 -> Swap ->{34, 25, 64, 12, 22}\n Compare 64, 12 -> Swap ->{34, 25, 12, 64, 22}\n Compare 64, 22 -> Swap ->{34, 25, 12, 22, 64}\n Result: 64 is sorted at the end.\n\nPass 2:\n Compare 34, 25 -> Swap ->{25, 34, 12, 22, 64}\n Compare 34, 12 -> Swap ->{25, 12, 34, 22, 64}\n Compare 34, 22 -> Swap ->{25, 12, 22, 34, 64}\n Result: 34 is sorted.\n\nPass 3:\n Compare 25, 12 -> Swap ->{12, 25, 22, 34, 64}\n Compare 25, 22 -> Swap ->{12, 22, 25, 34, 64}\n Result: 25 is sorted.\n\nPass 4:\n Compare 12, 22 -> No Swap ->{12, 22, 25, 34, 64}\n Result: Array is fully sorted. -
Question 18 of 20
Write a C program to find the Transpose of a given Matrix.
Model answer
The transpose of a matrix is obtained by swapping rows with columns. Element at
[i][j]moves to[j][i].\n\nc\n#include <stdio.h>\nint main() {\n int a[10][10], transpose[10][10], r, c, i, j;\n printf("Enter rows and columns: ");\n scanf("%d %d", &r, &c);\n\n printf("Enter matrix elements:\n");\n for (i = 0; i < r; ++i)\n for (j = 0; j < c; ++j)\n scanf("%d", &a[i][j]);\n\n // Computing transpose\n for (i = 0; i < r; ++i)\n for (j = 0; j < c; ++j) {\n transpose[j][i] = a[i][j];\n }\n\n printf("Transpose of the matrix:\n");\n for (i = 0; i < c; ++i) { // Note: Loop runs c times then r times\n for (j = 0; j < r; ++j) {\n printf("%d ", transpose[i][j]);\n }\n printf("\n");\n }\n return 0;\n}\n -
Question 19 of 20
What are the advantages and limitations of using Arrays in C?
Model answer
Advantages:\n1. Random Access: Any element can be accessed instantly using the index ( complexity).\n2. Code Optimization: Less code required to store multiple variables of the same type.\n3. Traversal: Easy to traverse using loops.\n4. Sorting/Searching: Algorithms like Binary search work efficiently on arrays.\n\nLimitations:\n1. Fixed Size: The size of the array must be known at compile time (static arrays). It cannot be resized dynamically at runtime.\n2. Contiguous Memory: Requires a single block of contiguous memory, which might cause allocation failures if memory is fragmented.\n3. Insertion/Deletion: Expensive operations because they require shifting existing elements to maintain order.
-
Question 20 of 20
Write a C program to find the largest and smallest element in an array along with their indices.
Model answer
c\n#include <stdio.h>\n\nint main() {\n int arr[50], n, i, max, min, maxPos, minPos;\n\n printf("Enter number of elements: ");\n scanf("%d", &n);\n\n for (i = 0; i < n; i++) {\n scanf("%d", &arr[i]);\n }\n\n // Initialize with first element\n max = arr[0]; min = arr[0];\n maxPos = 0; minPos = 0;\n\n for (i = 1; i < n; i++) {\n if (arr[i] > max) {\n max = arr[i];\n maxPos = i;\n }\n if (arr[i] < min) {\n min = arr[i];\n minPos = i;\n }\n }\n\n printf("Largest element is %d at index %d\n", max, maxPos);\n printf("Smallest element is %d at index %d\n", min, minPos);\n\n return 0;\n}\n
That's unit 4
Long answers stick best when they're tested again soon. Run the MCQs on this unit, then come back to the questions you found hardest.
Nearly through. Did unit 4 of CSE101 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 4 of CSE101 save you a night before the exam?
Then you can help keep it free for the next batch. Ads cover part of the server bill; the rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks.