0% found this document useful (0 votes)
8 views9 pages

Array Exercises2

The document contains a series of programming tasks in C, each with specific requirements and expected outputs. Tasks include printing unique elements, merging arrays, counting frequencies, finding maximum and minimum elements, and performing matrix operations. Each task is accompanied by test data and expected results to guide implementation.

Uploaded by

hossamarpro
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)
8 views9 pages

Array Exercises2

The document contains a series of programming tasks in C, each with specific requirements and expected outputs. Tasks include printing unique elements, merging arrays, counting frequencies, finding maximum and minimum elements, and performing matrix operations. Each task is accompanied by test data and expected results to guide implementation.

Uploaded by

hossamarpro
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/ 9

1. Write a program in C to print all unique elements in an array.

Test Data :
Print all unique elements of an array:
------------------------------------------
Input the number of elements to be stored in the array: 4
Input 4 elements in the array :
element - 0 : 3
element - 1 : 2
element - 2 : 2
element - 3 : 5
Expected Output :
The unique elements found in the array are:
35

2. Write a program in C to merge two arrays of the same size sorted in descending order.
Test Data :
Input the number of elements to be stored in the first array :3
Input 3 elements in the array :
element - 0 : 1
element - 1 : 2
element - 2 : 3
Input the number of elements to be stored in the second array :3
Input 3 elements in the array :
element - 0 : 1
element - 1 : 2
element - 2 : 3
Expected Output :
The merged array in decending order is :
332211

3. Write a program in C to count the frequency of each element of an array.


Test Data :
Input the number of elements to be stored in the array :3
Input 3 elements in the array :
element - 0 : 25
element - 1 : 12
element - 2 : 43
Expected Output :
The frequency of all elements of an array :
25 occurs 1 times
12 occurs 1 times
43 occurs 1 times

4. Write a program in C to find the maximum and minimum elements in an array.


Test Data :
Input the number of elements to be stored in the array :3
Input 3 elements in the array :
element - 0 : 45
element - 1 : 25
element - 2 : 21
Expected Output :
Maximum element is : 45
Minimum element is : 21

5. Write a program in C to separate odd and even integers into separate arrays.
Test Data :
Input the number of elements to be stored in the array :5
Input 5 elements in the array :
element - 0 : 25
element - 1 : 47
element - 2 : 42
element - 3 : 56
element - 4 : 32
Expected Output :
The Even elements are :
42 56 32
The Odd elements are :
25 47

6. Write a program in C to sort elements of an array in ascending order.


Test Data :
Input the size of array : 5
Input 5 elements in the array :
element - 0 : 2
element - 1 : 7
element - 2 : 4
element - 3 : 5
element - 4 : 9
Expected Output :
Elements of array in sorted ascending order:
24579

7. Write a program in C to sort the elements of the array in descending order.


Test Data :
Input the size of array : 3
Input 3 elements in the array :
element - 0 : 5
element - 1 : 9
element - 2 : 1
Expected Output :
Elements of the array in sorted descending order:
951

8. Write a program in C to delete an element at a desired position from an array.


Test Data :
Input the size of array : 5
Input 5 elements in the array in ascending order:
element - 0 : 1
element - 1 : 2
element - 2 : 3
element - 3 : 4
element - 4 : 5
Input the position where to delete: 3
Expected Output :
The new list is : 1 2 4 5
9. Write a program in C to find the second largest element in an array.
Test Data :
Input the size of array : 5
Input 5 elements in the array :
element - 0 : 2
element - 1 : 9
element - 2 : 1
element - 3 : 4
element - 4 : 6
Expected Output :
The Second largest element in the array is : 6
10. Write a program in C to find the second smallest element in an array.
Test Data :
Input the size of array : 5
Input 5 elements in the array (value must be <9999) :
element - 0 : 0
element - 1 : 9
element - 2 : 4
element - 3 : 6
element - 4 : 5
Expected Output :
The Second smallest element in the array is : 4

11. Write a program in C for adding two matrices of the same size.
Test Data :
Input the size of the square matrix (less than 5): 2
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Input elements in the second matrix :
element - [0],[0] : 5
element - [0],[1] : 6
element - [1],[0] : 7
element - [1],[1] : 8
Expected Output :
The First matrix is :

12
34
The Second matrix is :

56
78
The Addition of two matrix is :

68
10 12

12. Write a program in C for the multiplication of two square matrices.


Test Data :
Input the rows and columns of first matrix : 2 2
Input the rows and columns of second matrix : 2 2
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Input elements in the second matrix :
element - [0],[0] : 5
element - [0],[1] : 6
element - [1],[0] : 7
element - [1],[1] : 8
Expected Output :
The First matrix is :

12
34
The Second matrix is :

56
78
The multiplication of two matrix is :
19 22
43 50
13. Write a program in C to find the sum of the right diagonals of a matrix.
Test Data :
Input the size of the square matrix : 2
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Expected Output :
The matrix is :
12
34
Addition of the right Diagonal elements is :5

14. Sum of Elements


Find the sum of all elements in a 1D array.

15. Average of Elements


Compute the average of array elements.

16. Maximum Element


Find the largest number in the array.

17. Minimum Element


Find the smallest number in the array.

18. Count Even and Odd


Count how many elements are even and how many are odd.

19. Search Element


Check if a given element exists in the array.

20. Reverse Array


Print the array in reverse order.

21. Copy Array


Copy one array to another.
22. Find Frequency of a Number
Count how many times a given number appears.

23. Print Elements at Even Indices


Print only elements at even positions.

24. Second Largest Element


Find the second largest number in the array.

25. Left Rotation


Rotate array left by a given number of steps.

26. Right Rotation


Rotate array right by a given number of steps.

27. Remove Duplicates


Remove duplicate values from the array.

28. Sort Ascending


Sort the array in ascending order.

29. Sort Descending


Sort the array in descending order.

30. Check Palindrome


Check if the array reads the same forward and backward.

31. Merge Two Arrays


Merge two arrays into one.

32. Find Kth Smallest Element


Find the k-th smallest number in the array.

33. Separate Even and Odd


Place even numbers before odd numbers.

34. Longest Increasing Subsequence


Find the length of the longest increasing sequence of elements.

35. Maximum Subarray Sum (Kadane’s Algorithm)


Find the maximum sum of a contiguous subarray.

36. Rearrange Positive and Negative Numbers


Rearrange array to alternate between positive and negative numbers.
37. Move Zeros to End
Move all 0s to the end without changing the order of non-zero elements.

38. Cyclically Rotate by One


Rotate the array to the right by one position cyclically.

39. Matrix Input and Output


Take a 2D matrix and print it row-wise.

40. Sum of All Elements


Find the total sum of all matrix elements.

41. Row-wise Sum


Print the sum of each row.

42. Column-wise Sum


Print the sum of each column.

43. Transpose Matrix


Find the transpose of a matrix.

44. Print Diagonal Elements


Print the main diagonal elements.

45. Count Zero Elements


Count how many zero elements are in the matrix.

46. Check Symmetric Matrix


Check if the matrix is symmetric.

47. Find Maximum Element


Find the largest number in the matrix.

48. Replace Negative with Zero


Replace all negative values in the matrix with 0.

49. Matrix Addition


Add two matrices.

50. Matrix Subtraction


Subtract one matrix from another.
51. Matrix Multiplication
Multiply two matrices.

52. Check Identity Matrix


Check if the matrix is an identity matrix.

53. Find Saddle Point


Find an element that is the minimum in its row and maximum in its column.

54. Spiral Print of Matrix


Print the matrix in spiral order.

55. Boundary Traversal


Print boundary elements of a matrix.

56. Diagonal Sum


Find the sum of both diagonals.

57. Count Even Numbers in Matrix


Count all even elements in the matrix.

58. Search an Element in Matrix


Search for a value and return its position.

59. Rotate Matrix 90 Degrees


Rotate the matrix clockwise by 90 degrees.

60. Find Submatrix with Max Sum


Find the submatrix with the highest sum (brute-force).

61. Check Magic Square


Check if the matrix is a magic square.

62. Zig-Zag Print


Print elements in a zig-zag row-wise pattern.

63. Matrix Zeroing


If an element is 0, set its entire row and column to 0.

You might also like