-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSA6.c
More file actions
286 lines (239 loc) · 7.16 KB
/
Copy pathDSA6.c
File metadata and controls
286 lines (239 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
This code base own and maintained by Tanmoy Samnata using standardization C23 (ISO/IEC 9899:2024)
*/
#include <stdio.h>
// Merge two arrays to the third array.
int twoArray2NewArray()
{
int numbers[] = {4, 5, 9, 4, 7, 5, 2, 8, 2};
int total_elements = sizeof(numbers) / sizeof(numbers[0]);
printf("Unique elements in the array: ");
for (int i = 0; i < total_elements; i++)
{
int match_count = 0;
for (int j = 0; j < total_elements; j++)
{
if (numbers[i] == numbers[j])
{
match_count++;
}
}
// If the element appeared exactly once, it is unique
if (match_count == 1)
{
printf("%d ", numbers[i]);
}
}
}
// Search an element in an array
int elementsOfArray()
{
// Linear Search in an arry.
int numbers[] = {12, 45, 7, 93, 21, 5};
int size = sizeof(numbers) / sizeof(numbers[0]); // Calculates array length
int target = 93; // The element we want to find
int foundIndex = -1; // -1 means not found
for (int i = 0; i < size; i++)
{
if (numbers[i] == target)
{
foundIndex = i; // Store the index where it was found
break; // Exit loop early since we found it
}
}
if (foundIndex != -1)
{
printf("Element %d found at index %d.\n", target, foundIndex);
}
else
{
printf("Element %d not found.\n", target);
}
return 0;
}
// Sort array elements in ascending or descending order.
int arrayElementsInAscendingOrDescendingOrder()
{
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
// Bubble Sort Algorithm
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
// Change '>' to '<' for Descending order
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("Sorted array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
// Left rotate an array.
int leftRotateArray()
{
int arr[] = {10, 20, 30, 40, 50};
int size = sizeof(arr) / sizeof(arr[0]);
// Save the first element and shift everything left in one loop
int first = arr[0], i = 0;
for (; i < size - 1; i++)
{
arr[i] = arr[i + 1];
}
arr[size - 1] = first; // Place first element at the end
// Print result
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
return 0;
}
// Right rotate an array.
// Function to reverse a specific segment of the array
void reverseSegment(int arrayToRotate[], int startIndex, int endIndex)
{
while (startIndex < endIndex)
{
int temporaryStorage = arrayToRotate[startIndex];
arrayToRotate[startIndex] = arrayToRotate[endIndex];
arrayToRotate[endIndex] = temporaryStorage;
startIndex++;
endIndex--;
}
}
// Function to perform right rotation
void performRightRotation(int arrayToRotate[], int totalElements, int rotationSteps)
{
// Optimization: if rotationSteps > totalElements, use modulus
rotationSteps = rotationSteps % totalElements;
// 1. Reverse the entire array
reverseSegment(arrayToRotate, 0, totalElements - 1);
// 2. Reverse the first 'rotationSteps' elements
reverseSegment(arrayToRotate, 0, rotationSteps - 1);
// 3. Reverse the remaining elements
reverseSegment(arrayToRotate, rotationSteps, totalElements - 1);
}
int rightRotateOfArryCall()
{
int dataPoints[] = {10, 20, 30, 40, 50};
int numberOfElements = 5;
int shiftCount = 2;
performRightRotation(dataPoints, numberOfElements, shiftCount);
for (int i = 0; i < numberOfElements; i++)
{
printf("%d ", dataPoints[i]);
}
return 0;
}
// Add two matrices.
int additionOf2Matrices()
{
// Defining dimensions
int rows = 2;
int columns = 3;
// Initialize two 2D arrays (matrices)
int matrixA[2][3] = {
{1, 2, 3},
{4, 5, 6}};
int matrixB[2][3] = {
{7, 8, 9},
{10, 11, 12}};
// Matrix to store the result of the addition
int sumMatrix[2][3];
// Nested loops to traverse every row and every column
for (int rowIndex = 0; rowIndex < rows; rowIndex++)
{
for (int columnIndex = 0; columnIndex < columns; columnIndex++)
{
// Perform element-wise addition
sumMatrix[rowIndex][columnIndex] = matrixA[rowIndex][columnIndex] + matrixB[rowIndex][columnIndex];
}
}
// Displaying the result
printf("The sum of the two matrices is:\n");
for (int rowIndex = 0; rowIndex < rows; rowIndex++)
{
for (int columnIndex = 0; columnIndex < columns; columnIndex++)
{
printf("%d ", sumMatrix[rowIndex][columnIndex]);
}
printf("\n"); // Move to next line after printing a row
}
return 0;
}
// Subtract two matrices.
int subtract2Matrices()
{
// Defining dimensions
int rows = 2;
int columns = 3;
// Initialize two 2D arrays (matrices)
int matrixA[2][3] = {
{10, 20, 30},
{40, 50, 60}};
int matrixB[2][3] = {
{1, 2, 3},
{4, 5, 6}};
// Matrix to store the result of the subtraction
int diffMatrix[2][3];
// Nested loops to traverse every row and every column
for (int rowIndex = 0; rowIndex < rows; rowIndex++)
{
for (int columnIndex = 0; columnIndex < columns; columnIndex++)
{
// Perform element-wise subtraction
diffMatrix[rowIndex][columnIndex] = matrixA[rowIndex][columnIndex] - matrixB[rowIndex][columnIndex];
}
}
// Displaying the result
printf("The difference of the two matrices is:\n");
for (int rowIndex = 0; rowIndex < rows; rowIndex++)
{
for (int columnIndex = 0; columnIndex < columns; columnIndex++)
{
printf("%d ", diffMatrix[rowIndex][columnIndex]);
}
printf("\n"); // Move to next line after printing a row
}
return 0;
}
// Perform scalar matrix multiplication.
int scalarMatrixMultiplication()
{
int rows = 2;
int columns = 3;
int scalarValue = 5; // The constant to multiply by
int matrixA[2][3] = {
{1, 2, 3},
{4, 5, 6}};
int resultMatrix[2][3];
// Nested loops to traverse every element
for (int rowIndex = 0; rowIndex < rows; rowIndex++)
{
for (int columnIndex = 0; columnIndex < columns; columnIndex++)
{
// Multiply the current element by the scalar
resultMatrix[rowIndex][columnIndex] = matrixA[rowIndex][columnIndex] * scalarValue;
}
}
// Displaying the result
printf("The matrix after multiplying by scalar %d is:\n", scalarValue);
for (int rowIndex = 0; rowIndex < rows; rowIndex++)
{
for (int columnIndex = 0; columnIndex < columns; columnIndex++)
{
printf("%d ", resultMatrix[rowIndex][columnIndex]);
}
printf("\n");
}
return 0;
}
int main()
{
rightRotateOfArryCall();
return 0;
}