-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSA22.c
More file actions
206 lines (175 loc) · 6.13 KB
/
Copy pathDSA22.c
File metadata and controls
206 lines (175 loc) · 6.13 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
/*
This code base own and maintained by Tanmoy Samnata using standardization C23 (ISO/IEC 9899:2024)
*/
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
// Linear Search
int executeLinearSearch(int targetArray[], int totalElements, int searchTarget)
{
for (int currentIndex = 0; currentIndex < totalElements; currentIndex++)
{
if (targetArray[currentIndex] == searchTarget)
{
return currentIndex;
}
}
return -1; /* Target not found */
}
int executeIterativeBinarySearch(int sortedArray[], int totalElements, int searchTarget)
{
int leftBoundaryIndex = 0;
int rightBoundaryIndex = totalElements - 1;
while (leftBoundaryIndex <= rightBoundaryIndex)
{
int middleIndex = leftBoundaryIndex + (rightBoundaryIndex - leftBoundaryIndex) / 2;
if (sortedArray[middleIndex] == searchTarget)
{
return middleIndex;
}
if (sortedArray[middleIndex] < searchTarget)
{
leftBoundaryIndex = middleIndex + 1;
}
else
{
rightBoundaryIndex = middleIndex - 1;
}
}
return -1;
}
int executeRecursiveBinarySearch(int sortedArray[], int leftBoundaryIndex, int rightBoundaryIndex, int searchTarget)
{
if (leftBoundaryIndex <= rightBoundaryIndex)
{
int middleIndex = leftBoundaryIndex + (rightBoundaryIndex - leftBoundaryIndex) / 2;
if (sortedArray[middleIndex] == searchTarget)
{
return middleIndex;
}
if (sortedArray[middleIndex] < searchTarget)
{
return executeRecursiveBinarySearch(sortedArray, middleIndex + 1, rightBoundaryIndex, searchTarget);
}
return executeRecursiveBinarySearch(sortedArray, leftBoundaryIndex, middleIndex - 1, searchTarget);
}
return -1;
}
/* Star Pattern Printing (Pyramid) */
void printStarPatternPyramid(int totalRowsRequired)
{
for (int currentRow = 1; currentRow <= totalRowsRequired; currentRow++)
{
for (int spaceCount = 1; spaceCount <= totalRowsRequired - currentRow; spaceCount++)
{
printf(" ");
}
for (int starCount = 1; starCount <= (2 * currentRow - 1); starCount++)
{
printf("*");
}
printf("\n");
}
}
/* Print numbers from 1 to n without using a semicolon */
void printSequenceWithoutSemicolon(int maximumLimit)
{
int currentValue = 1;
while (currentValue <= maximumLimit && printf("%d ", currentValue) && currentValue++)
{
}
printf("\n");
}
/* Sum of two numbers without using any operator (meaning without arithmetic +, - etc) */
int calculateSumWithoutArithmetic(int firstValue, int secondValue)
{
int carryValueHolder;
while (secondValue != 0)
{
carryValueHolder = firstValue & secondValue;
firstValue = firstValue ^ secondValue;
secondValue = carryValueHolder << 1;
}
return firstValue;
}
/* 4. How to show memory representation of C variables */
void displayMemoryRepresentation(void *variablePointer, size_t sizeOfVariableInBytes)
{
unsigned char *bytePointer = (unsigned char *)variablePointer;
printf("Memory Bytes (Hex): ");
for (size_t byteIndex = 0; byteIndex < sizeOfVariableInBytes; byteIndex++)
{
printf("%02X ", bytePointer[byteIndex]);
}
printf("\n");
}
/* 5. Condition to print "HelloWorld" */
void executeHelloWorldPuzzle(void)
{
if (!printf("Hello"))
{
printf("Fail");
}
else
{
printf("World\n");
}
}
/* 6. Modify/add only one character and print '*' exactly 20 times */
void executeTwentyStarsPuzzle(void)
{
int maximumIterations = 20;
for (int loopCounter = 0; -loopCounter < maximumIterations; loopCounter--)
{
printf("*");
}
printf("\n");
}
/* Sum the digits of a given number in a single statement */
int sumDigitsInSingleStatement(int targetNumber)
{
return (targetNumber == 0) ? 0 : (targetNumber % 10) + sumDigitsInSingleStatement(targetNumber / 10);
}
int main(void)
{
printf("SEARCHING ALGORITHMS\n\n");
int sortedDataset[] = {12, 24, 35, 47, 59, 61, 78, 84, 93, 105};
int totalElementsInDataset = sizeof(sortedDataset) / sizeof(sortedDataset[0]);
int searchTargetValue = 78;
printf("Dataset: ");
for (int printIndex = 0; printIndex < totalElementsInDataset; printIndex++)
{
printf("%d ", sortedDataset[printIndex]);
}
printf("\nSearching for Target: %d\n\n", searchTargetValue);
int linearResultIndex = executeLinearSearch(sortedDataset, totalElementsInDataset, searchTargetValue);
printf("[Linear Search] Found at index: %d\n", linearResultIndex);
int iterativeBinaryResultIndex = executeIterativeBinarySearch(sortedDataset, totalElementsInDataset, searchTargetValue);
printf("[Iterative Binary Search] Found at index: %d\n", iterativeBinaryResultIndex);
int recursiveBinaryResultIndex = executeRecursiveBinarySearch(sortedDataset, 0, totalElementsInDataset - 1, searchTargetValue);
printf("[Recursive Binary Search] Found at index: %d\n\n", recursiveBinaryResultIndex);
printf(" PUZZLE QUESTIONS\n\n");
printf("Star Pattern Printing (5 Rows):\n");
printStarPatternPyramid(5);
printf("\n");
printf("Print 1 to 10 without semicolon:\n");
printSequenceWithoutSemicolon(10);
printf("\n");
int numberOne = 15, numberTwo = 27;
printf("Sum of %d and %d without arithmetic operators: %d\n\n",
numberOne, numberTwo, calculateSumWithoutArithmetic(numberOne, numberTwo));
float sampleFloatVariable = 3.14159f;
printf("Memory representation of float (Value: %f):\n", sampleFloatVariable);
displayMemoryRepresentation(&sampleFloatVariable, sizeof(sampleFloatVariable));
printf("\n");
printf("Condition to print \"HelloWorld\":\nResult: ");
executeHelloWorldPuzzle();
printf("\n");
printf("Print '*' 20 times by modifying one character in buggy loop:\nResult: ");
executeTwentyStarsPuzzle();
printf("\n");
int longNumber = 84592;
printf("Sum of digits in a single statement (Number: %d):\nResult: %d\n\n",
longNumber, sumDigitsInSingleStatement(longNumber));
return 0;
}