KEMBAR78
Array Exercises | PDF | Matrix (Mathematics) | Functions And Mappings
0% found this document useful (0 votes)
24 views15 pages

Array Exercises

Uploaded by

ilakkya005
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)
24 views15 pages

Array Exercises

Uploaded by

ilakkya005
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/ 15

1 D Array Exercises

1. Write a program to input elements in array and print all negative elements.
Sample Input:
-1

-10

100

61

-2

-23

-90

51
Sample Output:
-1, -10, -2, -23, -90
2. Write a program to read elements in an array and find the sum of array elements.
Sample Input:
Input elements: 10, 20, 30, 40, 50
Sample Output:
Sum of all elements = 150
3. Write a program to input elements in an array from user, find maximum and
minimum element in array.
Sample Input:
Input array elements: 10, 50, 12, 16, 2
Sample Output:
Maximum = 50

Minimum = 2
4. Write a program to find second largest element in an array.
Sample Input:
Input array elements: -7 2 3 8 6 6 75 38 3 2
Sample Output:
Second largest = 38
5. Write a program to input elements in array from user and count even and odd
elements in array.
Sample Input:
Input array: 1 2 3 4 5 6 7 8 9
Sample Output:
Total even elements: 4

Total odd elements: 5


6. Write a program to input elements in array and count negative elements in array.
Sample Input:
Input array elements: 10, -2, 5, -20, 1, 50, 60, -50, -12, -9
Sample Output:
Total number of negative elements: 5
7. Write a Program to input elements in array and copy all elements of first array
into second array.
Sample Input:
Input array1 elements: 10 1 95 30 45 12 60 89 40 -4
Sample Output:
Array1: 10 1 95 30 45 12 60 89 40 -4

Array2: 10 1 95 30 45 12 60 89 40 -4
8. Write a program to insert element in array at specified position.
Sample Input:
Input array elements: 10, 20, 30, 40, 50

Input element to insert: 25

Input position where to insert: 3


Sample Output:
Elements of array are: 10, 20, 25, 30, 40, 50
9. Write a program to delete element from array at specified position.
Sample Input:
Input array elements: 10 20 30 40 50

Input position to delete: 2


Sample Output:
Array elements: 10, 30, 40, 50
10. Write a program to input elements in array and find frequency of each element
in array.
Sample Input:
Input array elements: 5, 10, 2, 5, 50, 5, 10, 1, 2, 2
Sample Output:
Frequency of 5 = 3

Frequency of 10 = 2

Frequency of 2 = 3

Frequency of 50 = 1

Frequency of 1 = 1
11. Write a program to input elements in array and print all unique elements in
array.
Sample Input:
Input array elements: 1, 2, 3, 5, 1, 5, 20, 2, 12, 10
Sample Output:
All unique elements in the array are: 3, 20, 12, 10
12. Write a program to input elements in array from user and count duplicate
elements in array.
Sample Input:
Input array elements: 1, 10, 20, 1, 25, 1, 10, 30, 25, 1
Sample Output:
Total number of duplicate elements = 5
13. Write a program to delete duplicate elements from array. How to remove
duplicate elements from array in Programming.
Sample Input:
Input array elements: 10, 20, 10, 1, 100, 10, 2, 1, 5, 10
Sample Output:
After removing all duplicate elements

Elements of array are: 10, 20, 1, 100, 2, 5


14. Write a program to input elements in two array and merge two array to third
array.
Input
Input first array elements: 1, 4, 6, 9, 15

Input second array elements: 2, 5, 8, 10


Output
Merged array in ascending order = 1, 2, 4, 5, 6, 8, 9, 10, 15
15. Write a program to input elements in array and find reverse of array.
Sample Input:
Input array elements: 10, 5, 16, 35, 500
Sample Output:
Array elements after reverse: 500, 35, 16, 5, 10
16. Write a program to input elements in array and put even and odd elements in
separate array.
Sample Input:
Input size of the array: 10

Input elements in array: 0 1 2 3 4 5 6 7 8 9


Sample Output:
Output even elements in array: 0 2 4 6 8

Output odd elements in array: 1 3 5 7 9


17. Write a program to input elements in array and search whether an element exists
in array or not.
Sample Input:
Input size of array: 10
Input elements in array: 10, 12, 20, 25, 13, 10, 9, 40, 60, 5
Sample Output:
Element to search is: 25

Element found at index 3


18. Write a program to input elements in array and sort array elements in ascending
or descending order.
Sample Input:
Input size of array: 10

Input array elements: 20, 2, 10, 6, 52, 31, 0, 45, 79, 40


Sample Output:
Array sorted in ascending order: 0, 2, 6, 10, 20, 31, 40, 45, 52, 79
19. Write a program to input elements in an array from user and sort all even and
odd elements of the given array separately without using any other array.
Sample Input:
Input size of array: 10

Input elements of array: 0 5 1 2 3 4 6 12 10 9


Sample Output:
Output in sorted order: 0 2 4 6 10 12 1 3 5 9
20. Write a program to left rotate an array by n position.

Sample Input:
Input 10 elements in array: 1 2 3 4 5 6 7 8 9 10

Input number of times to rotate: 3


Sample Output:
Array after left rotation 3 times: 4 5 6 7 8 9 10 1 2 3
21. Write a Program to right rotate an array by n position.

Sample Input:
Input 10 elements in array: 1 2 3 4 5 6 7 8 9 10

Input number of times to rotate: 3


Sample Output:
Array after right rotation: 8 9 10 1 2 3 4 5 6 7
2D Array Exercises
1. Write a program to read elements in two matrices and add elements of both
matrices.
Sample Input:
Input elements in 3x3 matrix1:

123

456

789

Input elements in 3x3 matrix2:

987

654

321
Sample Output:
Sum of both matrix =

10 10 10

10 10 10

10 10 10
2. Write a program to read elements in two matrices and find the difference of two
matrices.
Sample Input:
Input elements in 3x3 matrix1:

123

456

789

Input elements in 3x3 matrix2:

987

654

321
Sample Output:
Difference of both matrices =

-8 -6 -4

-2 0 2

4 6 8
3. Write a program to read elements in a matrix and perform scalar multiplication
of matrix.
Sample Input
Input elements of matrix A:

123

456
789
Input multiplier: 2
Sample Output:
2 4 6

8 10 12

14 16 18
Scalar multiplication of matrix
Scalar multiplication of matrix is the simplest and easiest way to multiply matrix.
Scalar multiplication of matrix is defined by –
(cA)ij = c . Aij (Where 1 ≤ i ≤ m and 1 ≤ j ≤ n)

4. Write a program to read elements in two matrices and multiply them.


Sample Input:
Input elements of matrix1:

123

456

789

Input elements of matrix2:

987

654

321
Sample Output:
Product of matrices =

30 24 18
84 69 54

138 114 90
5. Write a Program to enter elements in two matrices and check whether both
matrices are equal or not.
Sample Input:
Input elements of matrix1:

123

456

789

Input elements of matrix2:

123

456

789
Sample Output:
Both matrices are equal
6. Write a Program to read elements in a matrix and find the sum of main diagonal
(major diagonal) elements of matrix.
Sample Input:
Input array elements:

123

456

789
Sample Output:
Sum of main diagonal elements = 15
7. Write a Program to read elements in a matrix and find the sum of minor diagonal
(opposite diagonal) elements.
Sample Input:
Input elements in array:

123
456
789
Sample Output:
Sum of minor diagonal elements = 15
8. Write a Program to read elements in a matrix and find the sum of elements of
each row and columns of matrix.
Sample Input:
Input elements in array:

123

456

789
Sample Output:
Sum of row 1 = 6

Sum of row 2 = 15

...

...

Sum of column 3 = 18
9. Write a program to read elements in a matrix and interchange elements of
primary(major) diagonal with secondary(minor) diagonal.
Sample Input:
Input matrix elements:

123

456

789
Sample Output:
Matrix after interchanging its diagonal:

321

456

987
10. Write a Program to read elements in a matrix and check whether the matrix is
upper triangular matrix or not.
Sample Input:
Input elements of matrix:

123

056

009
Sample Output:
Matrix is upper triangular
11. Write a Program to read elements in a matrix and check whether the matrix is a
lower triangular matrix or not.
Sample Input:
Input elements in matrix:

100

450

789
Sample Output:
Matrix is lower triangular
12. Write a Program to read elements in a matrix and find sum of upper triangular
matrix.
Sample Input:
Input matrix elements:

123

056

009
Sample Output:
Sum of upper triangular matrix = 11
13. Write a Program to read elements in a matrix and find sum of lower triangular
matrix.
Sample Input:
Input elements in matrix:

100

450

789
Sample Output:
Sum of lower triangular matrix = 19
14. Write a Program to read elements in a matrix and find transpose of the given
matrix.
Sample Input:
Input elements in matrix:

123

456

789
Sample Output:
Transpose:

147

258

369
15. Write a Program to read elements in a matrix and find determinant of the given
matrix.
Sample Input:
Input elements in 2x2 matrix:

12

34
Sample Output:
Determinant of the matrix = -2
16. Write a Program to read elements in a matrix and check whether matrix is an
Identity matrix or not.
Sample Input:
Input elements in matrix:

100

010

001
Sample Output:
It is an Identity matrix
17. Write a Program to read elements in a matrix and check whether matrix is Sparse
matrix or not.
Sample Input:
Input elements in matrix:

103

004

600
Sample Output:
The given matrix is Sparse matrix
18. Write a Program to read elements in a matrix and check whether the given matrix
is symmetric matrix or not.
Sample Input:
Input matrix elements:
123

245

358
Sample Output:
Given matrix is symmetric matrix.
19. Given an integer n, generate an n x n matrix filled with elements from 1 to
n2n^2n2 in a spiral order. The matrix should be filled in a clockwise direction
starting from the top-left corner.
Input
• An integer n (1 ≤ n ≤ 20).
Output
• A 2D list (list of lists) representing the n x n matrix filled with integers in
spiral order.

20. Problem Statement: Rotate Matrix 90 Degrees Clockwise


Description
Given an n x n matrix, rotate the matrix by 90 degrees clockwise in-place.
Input
• An n x n 2D list (list of lists) representing the matrix, where 1≤n≤1001 \leq n
\leq 1001≤n≤100.
Output
• The same 2D list after it has been rotated 90 degrees clockwise.
Example
Example 1:
Input:
matrix = [

[1, 2, 3],

[4, 5, 6],

[7, 8, 9]

]
Output:
[

[7, 4, 1],

[8, 5, 2],

[9, 6, 3]

You might also like