0% found this document useful (0 votes)
11 views

Lab 3

Uploaded by

Aryan Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lab 3

Uploaded by

Aryan Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

EGC 111-Programming 1A (C Programming)

Lab - 3

Due: 10 November, 2024 @ 11:59 pm

Part A
1. Write a C program to reverse an array.
Input:
The first line contains a single integer ‘t’ denoting the number of test cases. The description
of the test cases follows. The first line of each test case contains a number n the number of
elements in the array and the next line contains the elements of the array.
Output:
Print the reversed array
Sample input:
2
3
123
4
4356

Sample Output:
321
6534

2. Write a C program to delete an element from an array at specified position


Input:
The first line contains a single integer ‘t’ denoting the number of test cases. The description of
the test cases follows. The first line contains two numbers n,k the number of elements in the
array and the index of the element to be deleted respectively. Next line contains n elements
which are the array’s elements.
Output:
Print the array after deleting the element at the specified index.
Sample Input:
2
42

1
3456
51
5 8 9 10 11
Sample Output:
346
5 9 10 11
3. Write a function in C to right-rotate an array. The function should take the array and
the number of times the array has to be right-rotated as parameters.
Input:
The first line contains a single integer ‘t’ denoting the number of test cases. The description
of the test cases follows. The first line contains n,k the number of elements in the array and
the number of times the array has to be right rotated respectively. The next line contains n
elements which are the array’s elements.
Output:
Print the array after performing the right-rotation operation k times.
Sample Input:
2
43
3891
31
821
Sample Output:
8913
182
4. Write a function in C to multiply two matrices.
Input:
The first line contains a single integer ‘t’ denoting the number of test cases. The description of the
test cases follows. The first line contains n the order of matrices. The following n lines contain n
elements representing the elements of the first matrix and the next n lines represent elements of the
second matrix.
Output:
Print the product of the two matrices.
Sample Input:
1
2
15

2
37
11
21
Sample Output:
11 6
17 10
5. Write a function in C to find the transpose of a matrix
Input:
The first line contains a single integer ‘t’ denoting the number of test cases. The following t
lines contain the description of the test cases. n is the order of the matrix. The next n lines
contain n elements representing the elements of matrix.
Output:
Print the transposed matrix.
Sample input:
1
3
113
234
561
Sample output:
125
136
341
6. Write a C program to print the matrix elements in spiral order, starting from the top-left
element and moving towards the centre.
Spiral order means traversing the outermost elements first, then moving inwards layer by layer.
Input:
The first line contains a single integer ‘t’ denoting the number of test cases. The following t
lines contain the description of the test cases. n is the order of the matrix. The next n lines
contain n elements representing the elements of matrix.
Output:
Print the elements of the matrix in spiral order.
Sample input:
1
3

3
788
123
756
Sample output:
788365712

4
Part B
Theory:
1. Which of the following statements about arrays in C is true?
a) The size of an array can be changed after its declaration.
b) Elements in an array are stored in non-contiguous memory locations.
c) The first element of an array is stored at index 1.
d) Arrays in C are zero-indexed, meaning the first element is at index 0.
2. Which of the following is not a valid operation for an array in C?
a) Accessing elements using an index.
b) Modifying elements directly using the index.
c) Determining the size of the array with sizeof(arr).
d) Deleting an element from the middle of an array.
3. What will happen if you try to access an array element with an index greater than the array’s size in
C?
a) The program will automatically correct the index to the last element.
b) The compiler will throw an error during compilation.
c) The program may access unintended memory, causing undefined behavior.
d) The element will be initialized to 0 by default.
4. Which of the following statements is true about initializing an array in C?
a) Only the first element can be initialized, leaving the rest uninitialized.
b) Initializing an array with fewer values than its size will set the remaining elements to 0.
c) Initializing an array requires values for all elements; partial initialization is not allowed.
d) Initializing an array with more values than its size will ignore the extra values.
5. What is the output of the following program:

a) 6
b) 9
c) 1

5
d) Undefined behavior
6. How is memory allocated for arrays in C?
a) Memory is allocated on the heap for all arrays.
b) Memory is allocated on the stack by default for static arrays.
c) Memory allocation depends on the values in the array.
d) The programmer must always allocate memory explicitly using malloc.
7. Which of the following would correctly find the middle element of an array arr of n elements in C?
(a) arr[(n+1) / 2]
(b) arr[n / 2]
(c) arr[(n-1) / 2]
(d) arr[(n/2) -1]
8. Output of the following program:

(a) 4 5 6 7
(b) 4 3 2 1
(c) 1 3 5 7
(d) 7 6 5 4
9. Given an array int arr[4] = {10, 20, 30, 40};, what is the size of arr in bytes on a typical
32-bit system?
a) 4
b) 8
c) 16
d) 32
10. Which of the following best describes the concept of "row-major order" in the context of arrays?
a) Arrays are stored with rows and columns reversed in memory.

6
b) Rows are stored continuously in memory, followed by columns.
c) Elements of each row are stored contiguously in memory before moving to the next row.
d) Elements of each column are stored contiguously in memory before moving to the next column.

7
Programming:
1. Write a C program to check whether the given matrix is symmetric or not.
2. Write a C program to interchange the diagonals of a matrix.
3. Write a C program to find the sum of the lower triangular matrix.

You might also like