0% found this document useful (0 votes)
13 views10 pages

Neha Das Assign2

Uploaded by

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

Neha Das Assign2

Uploaded by

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

NEHA DAS

MCA SEM 1
ASSIGNMENT 2

1. Write C++ program to find and remove duplicate elements from an array.

#include <iostream>

using namespace std;

void removeDuplicates(int arr[], int &size)

int index = 0;

for (int i = 0; i < size; i++)

bool duplicate = false;

for (int j = 0; j < index; j++)

if (arr[i] == arr[j])

duplicate = true;

break;

if (!duplicate)

arr[index++] = arr[i];

size = index;
}

int main()

int arr[] = {1, 2, 2, 3, 4, 4, 5};

int size = sizeof(arr) / sizeof(arr[0]);

removeDuplicates(arr, size);

cout << "Array after removing duplicates: ";

for (int i = 0; i < size; i++)

cout << arr[i] << " ";

cout << std::endl;

return 0;

2. Write code in C++ to sort an array of integers in ascending order.

#include <iostream>
using namespace std;

void bubbleSort(int arr[], int size)


{
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
// Swap elements
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main()
{
int arr[] = {64, 25, 12, 22, 11};
int size = sizeof(arr) / sizeof(arr[0]);

bubbleSort(arr, size);

cout << "Sorted array: ";


for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;

return 0;
}

3. Write C++ program that adds two 2-D matrices and prints the result.

#include <iostream>
using namespace std;

const int ROWS = 3;


const int COLS = 3;

void addMatrices(int a[ROWS][COLS], int b[ROWS][COLS], int result[ROWS][COLS])


{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
result[i][j] = a[i][j] + b[i][j];
}
}
}

void printMatrix(int matrix[ROWS][COLS])


{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}

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);

cout << "Resultant matrix after addition:" << std::endl;


printMatrix(result);

return 0;
}

4. Write a program to transpose a given 2-D matrix.

#include <iostream>
using namespace std;

const int ROWS = 3;


const int COLS = 3;

void transposeMatrix(int matrix[ROWS][COLS], int result[COLS][ROWS])


{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
result[j][i] = matrix[i][j];
}
}
}

void printMatrix(int matrix[ROWS][COLS])


{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}

int main()
{
int matrix[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int result[COLS][ROWS];

transposeMatrix(matrix, result);

cout << "Transposed matrix:" << std::endl;


printMatrix(result);
return 0;
}

5. Write a C++ program to traverse a given 2-D matrix.

#include <iostream>
using namespace std;
const int ROWS = 3;
const int COLS = 3;

void traverseMatrix(int matrix[ROWS][COLS])


{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}

int main()
{

int matrix[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

cout << "Matrix traversal:" << std::endl;


traverseMatrix(matrix);
return 0;
}

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. }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy