Neha Das Assign2
Neha Das Assign2
Neha Das Assign2
MCA SEM 1
ASSIGNMENT 2
1. Write C++ program to find and remove duplicate elements from an array.
#include <iostream>
int index = 0;
if (arr[i] == arr[j])
duplicate = true;
break;
if (!duplicate)
arr[index++] = arr[i];
size = index;
}
int main()
removeDuplicates(arr, size);
return 0;
#include <iostream>
using namespace std;
int main()
{
int arr[] = {64, 25, 12, 22, 11};
int size = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, size);
return 0;
}
3. Write C++ program that adds two 2-D matrices and prints the result.
#include <iostream>
using namespace std;
int main()
{
int a[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int b[ROWS][COLS] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
int result[ROWS][COLS];
addMatrices(a, b, result);
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int matrix[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int result[COLS][ROWS];
transposeMatrix(matrix, result);
#include <iostream>
using namespace std;
const int ROWS = 3;
const int COLS = 3;
int main()
{
6. Write a program to traverse a 2D matrix in a spiral order and return the elements.
7. #include<iostream>
8. using namespace std;
9.
10. //given an n*m matrix , return all the elements in spiral
order.
11. void spiralOrder(int **arr , int row , int col){
12. int left=0;
13. int right= col-1;
14. int top=0;
15. int bottom=row-1;
16.
17. int direction=0;
18. while(left<=right && top<=bottom){
19. //left->right
20. if(direction==0){
21.
22. for(int i=left; i<=right; i++){
23. cout<<arr[top][i]<<" ";
24. }
25. top++;
26. }
27. //top->bottom
28. else if(direction==1){
29.
30. for(int i=top; i<=bottom; i++){
31. cout<<arr[i][right]<<" ";
32. }
33. right--;
34. }
35. //right->left
36. else if(direction==2){
37. for(int i=right; i>=left; i--){
38. cout<<arr[bottom][i]<<" ";
39. }
40. bottom--;
41. }
42. //bottom->top
43. else{
44. for(int i=bottom; i>=top; i--){
45. cout<<arr[i][left]<<" ";
46. }
47. left++;
48. }
49.
50. direction=(direction+1)%4;
51. }
52. }
53.
54. // given a positive integer 'n' generate an n*n matrix
filled with elements from 1 to n^2 in spiral order.
55.
56. void createSpiralMatrix(int n){
57. int arr[n][n];
58. int left=0;
59. int right= n-1;
60. int top=0;
61. int bottom=n-1;
62.
63. int direction=0;
64. int value=1;
65. while(left<=right && top<=bottom){
66. //left->right
67. if(direction==0){
68.
69. for(int i=left; i<=right; i++){
70. arr[top][i]=value++;
71. }
72. top++;
73. }
74. //top->bottom
75. else if(direction==1){
76.
77. for(int i=top; i<=bottom; i++){
78. arr[i][right]=value++;
79. }
80. right--;
81. }
82. //right->left
83. else if(direction==2){
84. for(int i=right; i>=left; i--){
85. arr[bottom][i]=value++;
86. }
87. bottom--;
88. }
89. //bottom->top
90. else{
91. for(int i=bottom; i>=top; i--){
92. arr[i][left]=value++;
93. }
94. left++;
95. }
96.
97. direction=(direction+1)%4;
98. }
99.
100. for(int i=0; i<n; i++){
101. for(int j=0; j<n; j++){
102. cout<<arr[i][j]<<" ";
103. }
104. cout<<endl;
105. }
106. cout<<endl;
107.
108. }
109.
110. int main(){
111. int row, col;
112. cout << "Enter no of rows : ";
113. cin >> row;
114. cout << endl;
115. cout << "Enter no of columns : ";
116. cin >> col;
117. cout << endl;
118.
119. // Dynamically allocate memory for the 2D array
120. int **arr = new int *[row];
121. for (int i = 0; i < row; i++)
122. {
123. arr[i] = new int[col];
124. }
125.
126. cout << "Enter array elements : ";
127. // taking 2D Array input from user
128. for (int i = 0; i < row; i++)
129. {
130. for (int j = 0; j < col; j++)
131. {
132. cin >> arr[i][j];
133. }
134. }
135. cout << endl;
136.
137. // printing the 2D array
138. cout << "Matrix : " << endl;
139. for (int i = 0; i < row; i++)
140. {
141. for (int j = 0; j < col; j++)
142. {
143. cout << arr[i][j] << " ";
144. }
145. cout << endl;
146. }
147. cout << endl;
148.
149. spiralOrder(arr , row , col);
150.
151. createSpiralMatrix(3);
152. return 0 ;
153. }