0% found this document useful (0 votes)
5 views24 pages

Practice Problems On Array

The document contains a series of practice problems related to arrays and 2D matrices in C programming. Each problem includes a description, sample input, and expected output, covering tasks such as summing elements, finding maximum/minimum values, sorting, and performing set operations. Additionally, it addresses operations on 2D matrices, including transposition, symmetry checks, and generating identity matrices.
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)
5 views24 pages

Practice Problems On Array

The document contains a series of practice problems related to arrays and 2D matrices in C programming. Each problem includes a description, sample input, and expected output, covering tasks such as summing elements, finding maximum/minimum values, sorting, and performing set operations. Additionally, it addresses operations on 2D matrices, including transposition, symmetry checks, and generating identity matrices.
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/ 24

Page 1 of 24

Practice Problems on Array


*Assume no array input will be greater than 10000.

1. Take an array of size n. Print their sum.


Sample Input Sample Output
4 Sum: 23
4 7 2 10
6 Sum: 21
123456
7 Sum: 20
9 3 2 1 -2 3 4

2. Write a C program that replaces the value at a given index of an array.


In this program, you should accept an array and an index. The program should
replace the value at the specified index with a new value.

Sample Input Sample Output


3 4 5 10
4 7 10
1
5

3. Write a C program to find the maximum and minimum elements, along with their
indices, in an array.
The program should take an array and return both the maximum and minimum
elements along with their indices in the array.

Sample Input Sample Output


4 Maximum: 10, Index: 2
4 2 10 7 Minimum: 2, Index: 1
4. Write a C program to find the 2nd and 3rd maximum elements in an array.
This program should return the 2nd and 3rd largest elements from the given array,
as well as their indices.
Sample Input Sample Output
5 2nd Maximum: 7, Index: 3
4 2 10 7 5 3rd Maximum: 5, Index: 4

C Programming Course,
Instructor – Md. Abdullah (EEE-19,BUET) , 01521706996(Whatsapp)
Page 2 of 24

5. Write a C program to find the first and last indices of a given integer in an array. If
the integer is not found, return -1.
The program should take an integer array and an integer value as input and output
the first and last index of that integer in the array.

Sample Input Sample Output


5 First Index: 1, Last Index: 3
4 7 10 7 7
7
5 -1
4 2 10 7 5
3
6. Write a C program to count the occurrences of a given number in an array.
The program should take an array and an integer, and count how many times that
integer appears in the array.
Sample Input Sample Output
5 Occurrences: 3
4 7 10 7 7
7

7. Write a C program that removes all occurrences of a given integer from an array
and shifts the remaining elements to the left, placing -1 at the end.
This program should take an array and an integer, remove all occurrences of the
integer from the array, and shift the remaining elements, placing -1 at the end.

Sample Input Sample Output


5 4 10 5 -1 -1
4 7 10 7 5
7
8. Write a C program to find the mode (the number that appears most frequently)
in an array.
The program should take an array of numbers and return the mode, i.e., the number
that appears the most times.

Sample Input Sample Output


5 Mode: 7
4 7 10 7 7

C Programming Course,
Instructor – Md. Abdullah (EEE-19,BUET) , 01521706996(Whatsapp)
Page 3 of 24

9. Write a C program that takes two arrays of integers, and stores the sum of
corresponding elements in a third array.
The program should take two arrays of integers and return a new array containing
the sum of corresponding elements.

Sample Input Sample Output


3 6 8 13
4 7 10
213

10. Write a C program to reverse the elements of an array.


This program should reverse the array, such that the first element becomes the last,
the second becomes the second last, and so on.

Sample Input Sample Output


5 2 5 10 7 4
4 7 10 5 2

11. Write a C program to sort an array in ascending and descending order.


The program should sort the array in both ascending and descending order.

Sample Input Sample Output


5 Sorted Ascending: 2 4 5 7 10
4 7 10 5 2 Sorted Descending: 10 7 5 4 2

12.Write a function that takes an array as input, sorts the array from index x to y.
Description:
This problem requires sorting only a specific part of the array, from index x to index
y. The rest of the array should remain unchanged. Sorting a portion of the array
helps in understanding range-based operations on arrays.
Sample Output
Sample Input

7 4 7 1 3 8 10 9
4 7 10 3 8 1 9
2 5

C Programming Course,
Instructor – Md. Abdullah (EEE-19,BUET) , 01521706996(Whatsapp)
Page 4 of 24

7 9235687
9253687
1 4

7 1984567
1984756
3 6
13.Take an array of integers. Output the array of distinct elements and print it’s
length.
Sample Output
Sample Input

8 12463
1211463326 5

5 12
11121 2
3 123
123 3

14. Write a C program to check if there is a pair in an array that sums up to a given
value.
The program should check if there are any two elements in the array that add up to
a given sum.

Sample Input Sample Output


4 False
4 7 10 5
10
5 True
65391
8
5 True
65391
11

C Programming Course,
Instructor – Md. Abdullah (EEE-19,BUET) , 01521706996(Whatsapp)
Page 5 of 24

15. Write a C program to find the union (set operation) of two arrays.
The program should take two arrays and return a new array containing all unique
elements from both arrays.
Sample Input Sample Output
3 4 5 7 10
4 7 10
3
10 5 4

16. Write a C program to find the intersection (set operation) of two arrays.
The program should return the common elements between two arrays.

Sample Input Sample Output


3 4 10
4 7 10
3
10 5 4

17.Write a program to compute various statistical parameters of an array of integers.

The first line of each input contains the number of elements in the array, then there will
be n integers in the second line. For each input, you have to compute and print the
following statistical parameters:

Mode: The most frequently occurring number in the array. For multiple modes, mention
all.
Median: The middle value of the array when sorted. If the number of elements is even,
the median is the average of the two middle values.
Mean (Average): The sum of the numbers divided by the number of elements.
Range: The difference between the maximum and minimum values in the array.
Variance: A measure of the spread of the numbers in the array. It is the average of the
squared differences from the mean.
Standard Deviation: The square root of the variance, providing a measure of the spread
of the values.
Formulas:
The variance ( ) is calculated as:

C Programming Course,
Instructor – Md. Abdullah (EEE-19,BUET) , 01521706996(Whatsapp)
Page 6 of 24

Sample Input Sample Output


7 Mode: 3
1738369 Median: 6
Mean: 5.29
Range: 8
Variance: 7.63
Standard Deviation: 2.76
6 Mode: 3
173836 Median: 4.5
Mean: 4.67
Range: 7
Variance: 6.22
Standard Deviation: 2.49
5 Mode: 5
55555 Median: 5
Mean: 5.0
Range: 0
Variance: 0.0
Standard Deviation: 0.0

4 Mode: 2
1223 Median: 2
Mean: 2.0
Range: 2
Variance: 0.50
Standard Deviation: 0.71

C Programming Course,
Instructor – Md. Abdullah (EEE-19,BUET) , 01521706996(Whatsapp)
Page 7 of 24

8 Mode: 8 9
8 8 9 10 9 8 10 9 Median: 9
Mean: 8.88
Range: 2
Variance: 0.61
Standard Deviation: 0.78
3 Mode: 4
446 Median: 4
Mean: 4.67
Range: 2
Variance: 0.67
Standard Deviation: 0.82
5 Mode: 2 3
12233 Median: 2
Mean: 2.2
Range: 2
Variance: 0.56
Standard Deviation: 0.75

18.Take an array of length N as input. While termination is not requested, perform


the following operations:

Operation 1 : Insert at index i integer e


Operation 2: Remove element at index i
Operation 3: Remove all integers e from the array
Operation 4: Replace element at index i with integer e
Operation 5: Terminate

Output the array after each operation. Perform no changes if index i is out of bounds.

Sample Input Sample Output

5 123545
12345 13545
1 134
35 734

C Programming Course,
Instructor – Md. Abdullah (EEE-19,BUET) , 01521706996(Whatsapp)
Page 8 of 24

2
1
3
5
4
07
5

5 4132
54132 41532
2
0
1
25
5

19. Write a C program to print a 2D matrix both row-wise and column-wise.


Description:
In this problem, you are asked to print a 2D matrix both row-wise and column-
wise. First, you will print each row in the matrix, then print each column by
iterating through the matrix column by column.
Sample Input Sample Output
3 Row-wise:
123 123
456 456
789 789
Column-wise:
147
258
369

C Programming Course,
Instructor – Md. Abdullah (EEE-19,BUET) , 01521706996(Whatsapp)
Page 9 of 24

20.Write a C program to transpose a 2D square matrix and store the


transposed matrix in a new 2D array.
Description:
You need to transpose the matrix, meaning the rows should become columns
and vice versa. For example, the first row will become the first column, the
second row will become the second column, and so on. The result should be
stored in a new matrix or in the original matrix itself.
Sample Input Sample Output
3 Transposed Matrix:
123 147
456 258
789 369

21.Write a C program to check if a 2D square matrix is symmetric or not.


Description:
A matrix is symmetric if it is equal to its transpose. In this problem, you need
to check if the matrix is symmetric. This means that the element at position (i,
j) should be equal to the element at position (j, i).

Sample Input Sample Output


3 Yes
123
245
356
3 No
123
-2 4 5
9 56

22.Write a C program to check if a 2D square matrix is symmetric or not


with respect to the right diagonal.
This problem requires you to check if the matrix is symmetric when
transposed with respect to the right diagonal (from bottom-left to top-right).
For a matrix to be symmetric with respect to the right diagonal, the element at
position (i, j) should be equal to the element at position (n-1-j, n-1-i), where n
is the size of the matrix.

C Programming Course,
Instructor – Md. Abdullah (EEE-19,BUET) , 01521706996(Whatsapp)
Page 10 of 24

Sample Input Sample Output


3 No
123
456
789
3 Yes
123
452
641

23.Write a C program to generate the 2D identity matrix of a given size.


Description:
An identity matrix is a square matrix where all the diagonal elements are 1
and all other elements are 0. In this problem, you are given a size n and need
to generate an identity matrix of size n x n.

Sample Input Sample Output


3 100
010
001
24.Write a C program to print both the diagonals (major, minor) of a 2D
square matrix.
Description:
In this problem, you need to print both the major diagonal (top-left to bottom-
right) and minor diagonal (top-right to bottom-left) of a 2D square matrix.

Sample Input Sample Output


3 Major Diagonal: 1 5 9
123 Minor Diagonal: 3 5 7
456
789

25.Write a C program to print the trace of a 2D square matrix.


Description:
The trace of a matrix is the sum of the elements along the main diagonal (top-
left to bottom-right). This problem asks you to compute the trace of a given

C Programming Course,
Instructor – Md. Abdullah (EEE-19,BUET) , 01521706996(Whatsapp)
Page 11 of 24

2D square matrix.

Sample Input Sample Output


3 Trace: 15
123
456
789

26.Write a C program to check if a 2D square matrix is a diagonal matrix or


not.
Description:
A diagonal matrix is one in which all elements off the main diagonal are zero.
In this problem, you need to check whether a given matrix is a diagonal
matrix.

Sample Input Sample Output


3 Yes
100
050
009

27.Write a C program to swap any two given rows/columns of a 2D matrix.


Description:
In this problem, you need to swap two rows or two columns of a 2D matrix
based on the user’s input. After swapping, print the modified matrix.

Sample Input Sample Output


3 456
123 123
456 789
789
12

28.Write a C program to multiply two 2D matrices.


Description:
C Programming Course,
Instructor – Md. Abdullah (EEE-19,BUET) , 01521706996(Whatsapp)
Page 12 of 24

Matrix multiplication involves taking two matrices and performing a series of


dot products. The number of columns in the first matrix must be equal to the
number of rows in the second matrix. This problem requires multiplying two
matrices and printing the resulting matrix.
Sample Input Sample Output
2 19 22
12 43 50
34
2
56
78

C Programming Course,
Instructor – Md. Abdullah (EEE-19,BUET) , 01521706996(Whatsapp)
Page 13 of 24

Term Final Questions Of BUET

C Programming Course,
Instructor – Md. Abdullah (EEE-19,BUET) , 01521706996(Whatsapp)
Page 14 of 24

Practice pervious year Term final questions seriously as many


as you can.

Online and Offline Problems Of CSE Lab Of BUET

C Programming Course,
Instructor – Md. Abdullah (EEE-19,BUET) , 01521706996(Whatsapp)
Page 15 of 24

Problem #3

C Programming Course,
Instructor – Md. Abdullah (EEE-19,BUET) , 01521706996(Whatsapp)
Page 16 of 24

Problem #4

Problem #5

C Programming Course,
Instructor – Md. Abdullah (EEE-19,BUET) , 01521706996(Whatsapp)
Page 17 of 24

Problem #6

Problem #7

Problem #7

C Programming Course,
Instructor – Md. Abdullah (EEE-19,BUET) , 01521706996(Whatsapp)
Page 18 of 24

Problem #8

C Programming Course,
Instructor – Md. Abdullah (EEE-19,BUET) , 01521706996(Whatsapp)
Page 19 of 24

Problem #9

C Programming Course,
Instructor – Md. Abdullah (EEE-19,BUET) , 01521706996(Whatsapp)
Page 20 of 24

Problem #10

C Programming Course,
Instructor – Md. Abdullah (EEE-19,BUET) , 01521706996(Whatsapp)
Page 21 of 24

Problem #11

C Programming Course,
Instructor – Md. Abdullah (EEE-19,BUET) , 01521706996(Whatsapp)
Page 22 of 24

Problem #12

C Programming Course,
Instructor – Md. Abdullah (EEE-19,BUET) , 01521706996(Whatsapp)
Page 23 of 24

Problem #13

Problem #14

C Programming Course,
Instructor – Md. Abdullah (EEE-19,BUET) , 01521706996(Whatsapp)
Page 24 of 24

Problem #15

The Source of the Problems Of This Sheet –

 Self-created Problems
 Term Final Questions of BUET
 Lab materials of CSE Lab of BUET
 Taken Help from AI

C Programming Course,
Instructor – Md. Abdullah (EEE-19,BUET) , 01521706996(Whatsapp)

You might also like