-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSA5.c
More file actions
336 lines (282 loc) · 9.43 KB
/
Copy pathDSA5.c
File metadata and controls
336 lines (282 loc) · 9.43 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
This code base own and maintained by Tanmoy Samnata using standardization C23 (ISO/IEC 9899:2024)
*/
#include <stdio.h>
#include <string.h>
// Remove Duplicates Items In An Array
int duplicatesItemsInAnArray()
{
int sampleArray[] = {1, 2, 2, 3, 4, 4, 4, 5};
int n = sizeof(sampleArray) / sizeof(sampleArray[0]), j = 0;
for (int i = 0; i < n; i++)
{
if (i == 0 || sampleArray[i] != sampleArray[i - 1])
{
sampleArray[j++] = sampleArray[i];
}
}
for (int i = 0; i < j; i++)
printf("%d ", sampleArray[i]);
return 0;
}
int checkStringIsPalindromeOrNot()
{
char str[] = "radar";
int len = strlen(str), is_palindrome = 1;
// Loop up to the middle of the string
for (int i = 0; i < len / 2; i++)
{
if (str[i] != str[len - 1 - i])
{
is_palindrome = 0;
break;
}
}
printf(is_palindrome ? "Palindrome\n" : "Not Palindrome\n");
return 0;
}
int nameUsingArry (){
char name[] = "John Doe";
printf("%s\n", "I AM IDIOT");
return 0;
}
int convertAllInputStringSimultaneouslyIntoAsterisk()
{
char passwordPlaceHolder[20];
printf("Enter password: ");
scanf("%s", passwordPlaceHolder);
printf("You entered: %s\n", passwordPlaceHolder);
char password = passwordPlaceHolder;
// Loop through the string and replace each character with '*'
for (int i = 0; passwordPlaceHolder[i] != '\0'; i++)
{
passwordPlaceHolder[i] = '*';
}
printf("Masked String: %s\n", passwordPlaceHolder);
return 0;
}
// Read and print elements of the array. – using recursion.
// Recursive function to read array elements
void readArray(int arr[], int i, int n)
{
if (i < n)
{
scanf("%d", &arr[i]);
readArray(arr, i + 1, n); // Recursive call for next index
}
}
// Recursive function to print array elements
void printArray(int arr[], int i, int n)
{
if (i < n)
{
printf("%d ", arr[i]);
printArray(arr, i + 1, n); // Recursive call for next index
}
}
int wayToReadReadArray()
{
int n;
printf("Enter size: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
readArray(arr, 0, n);
printf("Array elements are: ");
printArray(arr, 0, n);
return 0;
}
// Print all negative elements in an array.
int negativeElementsInAnArray()
{
int arr[] = {10, -5, 20, -3, -1, 40};
int n = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < n; i++)
{
if (arr[i] < 0)
printf("%d ", arr[i]);
}
return 0;
}
// Recursive function to sum array elements
int sumArray(int arr[], int n)
{
if (n <= 0)
return 0; // Base case
return arr[n - 1] + sumArray(arr, n - 1); // Recursive step
}
// Call this function.
int wayToCallSumArray()
{
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Sum: %d\n", sumArray(arr, n));
return 0;
}
// Find a maximum and minimum element in an array. – using recursion.
// Recursive function to find min and max
void findMinMax(int arr[], int n, int *min, int *max)
{
if (n == 1)
{ // Base case: only one element
*min = *max = arr[0];
return;
}
// Recursive call for the rest of the array
findMinMax(arr, n - 1, min, max);
// Compare current element with min and max found so far
if (arr[n - 1] < *min)
*min = arr[n - 1];
if (arr[n - 1] > *max)
*max = arr[n - 1];
}
int WayToFindMinMax()
{
int arr[] = {12, 45, 2, 67, -3, 89, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int min, max;
findMinMax(arr, n, &min, &max);
printf("Minimum element: %d\nMaximum element: %d\n", min, max);
return 0;
}
// Get the second largest element in an array.
#include <stdio.h>
int secondLargestArray()
{
int arr[] = {12, 35, 1, 10, 34, 1};
int n = sizeof(arr) / sizeof(arr[0]);
int max1 = arr[0], max2 = -1; // Assumes positive numbers; change -1 to a very low number if negative numbers are possible
for (int i = 1; i < n; i++)
{
if (arr[i] > max1)
{
max2 = max1;
max1 = arr[i];
}
else if (arr[i] > max2 && arr[i] < max1)
{
max2 = arr[i];
}
}
printf("Second largest element: %d\n", max2);
return 0;
}
// Count the total number of even and odd elements in an array.
int numberEvenOddInArray()
{
// Descriptive variable names instead of short, cryptic ones
int sourceArrayOfNumbers[] = {1, 2, 3, 4, 5, 6, 7};
int totalElementsInArray = sizeof(sourceArrayOfNumbers) / sizeof(sourceArrayOfNumbers[0]);
int totalCountOfEvenNumbers = 0;
int totalCountOfOddNumbers = 0;
// Loop through the array using a clear index variable name
for (int currentArrayIndex = 0; currentArrayIndex < totalElementsInArray; currentArrayIndex++)
{
int currentElementValue = sourceArrayOfNumbers[currentArrayIndex];
// Check if the current number is perfectly divisible by 2
if (currentElementValue % 2 == 0)
{
totalCountOfEvenNumbers++;
}
else
{
totalCountOfOddNumbers++;
}
}
// Display the final results with clear descriptions
printf("Total Even Elements: %d\n", totalCountOfEvenNumbers);
printf("Total Odd Elements: %d\n", totalCountOfOddNumbers);
return 0;
}
// Count the total number of negative elements in an array.
int negativeInArray()
{
// Array containing both positive and negative numbers
int sourceArrayOfNumbers[] = {10, -5, 20, -3, -1, 40, -8};
int totalElementsInArray = sizeof(sourceArrayOfNumbers) / sizeof(sourceArrayOfNumbers[0]);
int totalCountOfNegativeNumbers = 0;
// Loop through the array to evaluate each element
for (int currentArrayIndex = 0; currentArrayIndex < totalElementsInArray; currentArrayIndex++)
{
int currentElementValue = sourceArrayOfNumbers[currentArrayIndex];
// Check if the current number is less than zero
if (currentElementValue < 0)
{
totalCountOfNegativeNumbers++;
}
}
printf("Total Negative Elements: %d\n", totalCountOfNegativeNumbers);
return 0;
}
// Copy all elements from an array to another array.
int copyElementsArray2Array()
{
int sourceArrayOfNumbers[] = {10, 20, 30, 40, 50};
int totalElementsInArray = sizeof(sourceArrayOfNumbers) / sizeof(sourceArrayOfNumbers[0]);
int destinationArrayOfNumbers[totalElementsInArray];
// Loop through the source array and duplicate each element into the destination array
for (int currentArrayIndex = 0; currentArrayIndex < totalElementsInArray; currentArrayIndex++)
{
destinationArrayOfNumbers[currentArrayIndex] = sourceArrayOfNumbers[currentArrayIndex];
}
// Display the elements of the destination array to confirm the copy was successful
printf("Elements in Destination Array: ");
for (int currentArrayIndex = 0; currentArrayIndex < totalElementsInArray; currentArrayIndex++)
{
printf("%d ", destinationArrayOfNumbers[currentArrayIndex]);
}
printf("\n");
return 0;
}
// Insert an element in an array.
int elementsInArray()
{
int sourceArrayOfNumbers[10] = {10, 20, 30, 40, 50};
int currentElementsCount = 5; // The actual number of elements currently in use
int valueToBeInserted = 25;
int targetInsertionPosition = 3; // Position 3 corresponds to index 2 (1-based counting)
int targetInsertionIndex = targetInsertionPosition - 1; // Convert to 0-based index
// Shift elements to the right from the end of the array down to the target index
for (int currentArrayIndex = currentElementsCount; currentArrayIndex > targetInsertionIndex; currentArrayIndex--)
{
sourceArrayOfNumbers[currentArrayIndex] = sourceArrayOfNumbers[currentArrayIndex - 1];
}
// Insert the new value at the targeted empty index slot
sourceArrayOfNumbers[targetInsertionIndex] = valueToBeInserted;
// Increase the active element count since a new number was added
currentElementsCount++;
printf("Array elements after insertion: ");
for (int currentArrayIndex = 0; currentArrayIndex < currentElementsCount; currentArrayIndex++)
{
printf("%d ", sourceArrayOfNumbers[currentArrayIndex]);
}
printf("\n");
return 0;
}
// Delete an element from an array at the specified position.
int deleteElementformPositionInArry()
{
int sourceArrayOfNumbers[] = {10, 20, 25, 30, 40, 50};
int currentElementsCount = 6;
int targetDeletionPosition = 3; // Position 3 corresponds to index 2 (1-based counting)
int targetDeletionIndex = targetDeletionPosition - 1; // Convert to 0-based index
// Shift elements to the left from the target index up to the end of the array
for (int currentArrayIndex = targetDeletionIndex; currentArrayIndex < currentElementsCount - 1; currentArrayIndex++)
{
sourceArrayOfNumbers[currentArrayIndex] = sourceArrayOfNumbers[currentArrayIndex + 1];
}
// Decrease the active element count since one number was removed
currentElementsCount--;
printf("Array elements after deletion: ");
for (int currentArrayIndex = 0; currentArrayIndex < currentElementsCount; currentArrayIndex++)
{
printf("%d ", sourceArrayOfNumbers[currentArrayIndex]);
}
printf("\n");
return 0;
}
int main(){
WayToFindMinMax();
wayToCallSumArray();
return 0;
}