-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointers.c
More file actions
355 lines (284 loc) · 7.53 KB
/
Copy pathpointers.c
File metadata and controls
355 lines (284 loc) · 7.53 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Write a program to compute are and perimeter of circle using pointer.
#include <stdio.h>
#define PI 3.14159
void computeCircle(float radius, float *area, float *perimeter) {
*area = PI * radius * radius;
*perimeter = 2 * PI * radius;
}
int main() {
float radius, area, perimeter;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
computeCircle(radius, &area, &perimeter);
printf("Area of the circle: %.2f\n", area);
printf("Perimeter of the circle: %.2f\n", perimeter);
return 0;
}
// Write a program to convert decimal number entered by used in its equivalent binary
// number using pointer.
#include <stdio.h>
void decimalToBinary(int n, char *binary) {
int index = 0;
if (n == 0) {
binary[index++] = '0';
binary[index] = '\0';
return;
}
while (n > 0) {
binary[index++] = (n % 2) + '0';
n /= 2;
}
binary[index] = '\0';
// Reverse the binary string
int start = 0;
int end = index - 1;
while (start < end) {
char temp = binary[start];
binary[start] = binary[end];
binary[end] = temp;
start++;
end--;
}
}
int main() {
int decimal;
char binary[32]; // Assuming binary representation will not exceed 32 bits
printf("Enter a decimal number: ");
scanf("%d", &decimal);
decimalToBinary(decimal, binary);
printf("Binary representation: %s\n", binary);
return 0;
}
// Write a program to find lcm of two numbers using pointer.
#include <stdio.h>
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
void findLCM(int a, int b, int *lcm) {
*lcm = (a * b) / gcd(a, b);
}
int main() {
int num1, num2, lcm;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
findLCM(num1, num2, &lcm);
printf("LCM of %d and %d is %d\n", num1, num2, lcm);
return 0;
}
// Write a program to reverse a given array of 20 elements using pointers.
#include <stdio.h>
#define SIZE 20
void reverseArray(int *arr, int size) {
int *start = arr;
int *end = arr + size - 1;
int temp;
while (start < end) {
temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}
int main() {
int arr[SIZE];
printf("Enter %d elements:\n", SIZE);
for (int i = 0; i < SIZE; i++) {
scanf("%d", &arr[i]);
}
reverseArray(arr, SIZE);
printf("Reversed array:\n");
for (int i = 0; i < SIZE; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
// Write a program to enter a sorted array and an integer value. Insert the new value at
// correct place using pointer.
#include <stdio.h>
#define MAX_SIZE 21 // 20 elements + 1 for the new element
void insertInSortedArray(int *arr, int size, int value) {
int i = size - 1;
while (i >= 0 && arr[i] > value) {
arr[i + 1] = arr[i];
i--;
}
arr[i + 1] = value;
}
int main() {
int arr[MAX_SIZE - 1];
int size, value;
printf("Enter the number of elements in the sorted array (max 20): ");
scanf("%d", &size);
printf("Enter %d sorted elements:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the new value to insert: ");
scanf("%d", &value);
insertInSortedArray(arr, size, value);
printf("Array after insertion:\n");
for (int i = 0; i < size + 1; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
// Write a program to find inverse of a matrix using pointers.
#include <stdio.h>
#define SIZE 2
void invertMatrix(float *matrix, float *inverse) {
float det = matrix[0] * matrix[3] - matrix[1] * matrix[2];
if (det == 0) {
printf("Matrix is singular and cannot be inverted.\n");
return;
}
float invDet = 1.0 / det;
inverse[0] = matrix[3] * invDet;
inverse[1] = -matrix[1] * invDet;
inverse[2] = -matrix[2] * invDet;
inverse[3] = matrix[0] * invDet;
}
int main() {
float matrix[SIZE * SIZE];
float inverse[SIZE * SIZE];
printf("Enter the elements of a 2x2 matrix:\n");
for (int i = 0; i < SIZE * SIZE; i++) {
scanf("%f", &matrix[i]);
}
invertMatrix(matrix, inverse);
printf("Inverse matrix:\n");
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
printf("%.2f ", inverse[i * SIZE + j]);
}
printf("\n");
}
return 0;
}
// Write a program to find occurrences of given character in entered string using
// pointer.
#include <stdio.h>
void findOccurrences(const char *str, char ch, int *count) {
*count = 0;
while (*str) {
if (*str == ch) {
(*count)++;
}
str++;
}
}
int main() {
char str[100];
char ch;
int count;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
printf("Enter the character to find: ");
scanf("%c", &ch);
findOccurrences(str, ch, &count);
printf("Character '%c' occurs %d times.\n", ch, count);
return 0;
}
// Write a program to find the longest word from the entered string and also find its
// length using pointer.
#include <stdio.h>
#include <string.h>
void findLongestWord(const char *str, char *longestWord) {
int maxLength = 0;
char word[100];
int index = 0;
while (*str) {
if (*str == ' ' || *str == '\0') {
word[index] = '\0';
if (index > maxLength) {
maxLength = index;
strcpy(longestWord, word);
}
index = 0;
} else {
word[index++] = *str;
}
str++;
}
word[index] = '\0';
if (index > maxLength) {
strcpy(longestWord, word);
}
}
int main() {
char str[256];
char longestWord[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
findLongestWord(str, longestWord);
printf("Longest word: %s\n", longestWord);
printf("Length of longest word: %lu\n", strlen(longestWord));
return 0;
}
// Write a program to arrange the strings (3 entered strings) according to their length
// using array of pointers.
#include <stdio.h>
#define SIZE 10
void sortDescending(int *arr, int n) {
int *i, *j, temp;
for (i = arr; i < arr + n - 1; i++) {
for (j = i + 1; j < arr + n; j++) {
if (*i < *j) {
temp = *i;
*i = *j;
*j = temp;
}
}
}
}
int main() {
int arr[SIZE];
printf("Enter %d elements:\n", SIZE);
for (int i = 0; i < SIZE; i++) {
scanf("%d", &arr[i]);
}
sortDescending(arr, SIZE);
printf("Array sorted in descending order:\n");
for (int i = 0; i < SIZE; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
//Write a program to sort an array of 10 elements in
// descending order using pointer.
#include <stdio.h>
#define SIZE 10
void sortDescending(int *arr, int n) {
int *i, *j, temp;
for (i = arr; i < arr + n - 1; i++) {
for (j = i + 1; j < arr + n; j++) {
if (*i < *j) {
temp = *i;
*i = *j;
*j = temp;
}
}
}
}
int main() {
int arr[SIZE];
printf("Enter %d elements:\n", SIZE);
for (int i = 0; i < SIZE; i++) {
scanf("%d", &arr[i]);
}
sortDescending(arr, SIZE);
printf("Array sorted in descending order:\n");
for (int i = 0; i < SIZE; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}