some notes of c
some notes of c
h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <ctype.h>
5 //GCD
6
7 int GCD(int a, int b)
8 {
9 Without Recursion
10 if(a < b){
11 int temp = a;
12 a = b;
13 b = temp;
14 }
15 while(a % b != 0){
16 b = a % b;
17 }
18 return b;
19 With Recursion
20 if(b == 0)
21 return a;
22 return GCD(b, a % b);
23
24
25 }
26
27 //triangle pattern 1 0
28
29 void trinagle(int r){
30 int tmp = 1;
31 int c = 1;
32 int unit = 0;
33 while (tmp <= r)
34 {
35 if(tmp % 2 == 0)
36 unit = 1;
37 else
38 unit = 0;
39 for(int i = 0 ; i < c && c <= r; i++){
40 unit = !unit;
41 printf("%d",unit);
42 }
43 c++;
44 printf("\n");
45 tmp++;
46 }
47 }
48
49 //check palindrome
50
51 int ispalindrome(int n){
52 int tmp = n;
53 int numberOfDigits = 0;
54 while (tmp / 10.0 != 0)
55 {
56 tmp = tmp / 10;
57 numberOfDigits++;
58 }
59 tmp = n;
60 int rev = 0;
61 for (int i = 0; i < numberOfDigits ; i++)
62 {
63 rev = (rev * 10) + (n % 10);
64 n = n/10;
65 }
66 for (int i = 0; i < numberOfDigits; i++)
67 {
68 if(rev % 10 != tmp % 10)
69 return 0;
70 else{
71 rev = rev / 10;
72 tmp = tmp /10;
73 }
74 }
75 return 1;
76 }
76 }
77
78
79 //check number if prime
80
81 int checkPrime(int n) {
82 int i, isPrime = 1;
83
84 0 and 1 are not prime numbers
85 if (n == 0 || n == 1) {
86 isPrime = 0;
87 }
88 else {
89 for(i = 2; i <= n/2; ++i) {
90 if(n % i == 0) {
91 isPrime = 0;
92 break;
93 }
94 }
95 }
96
97 return isPrime;
98 }
99
100
101
102 //Check number if can be expresses as sum of Two Prime *********
103
104 int checkTwoPrime(int n){
105 for (i = 2; i <= n / 2; ++i) {
106 condition for i to be a prime number
107 if (checkPrime(i) == 1) {
108 condition for n-i to be a prime number
109 if (checkPrime(n - i) == 1) {
110 printf("%d = %d + %d\n", n, i, n - i);
111 flag = 1;
112 }
113 }
114 }
115
116 if (flag == 0)
117 printf("%d cannot be expressed as the sum of two prime numbers.");
118 return flag;
119 }
120
121
122
123 //swapping uisng pointers
124
125 void swapp(int *a,int *b){
126 int tmp = *a;
127 *a = *b;
128 *b = tmp;
129 }
130 in main
131 int x = 10;
132 int y = 5;
133 swapp(&x,&y);
134
135
136
137 /*
138 void Count_Grades(int *countA, int *countB, int *countC, int *countD, int *countF)
139 {
140 char stopKey = ' ';
141
142 while (1)
143 {
144 scanf(" %c", &stopKey);
145 if (stopKey == 'Z' || stopKey == 'z')
146 break;
147
148 switch (stopKey)
149 {
150 case 'A':
151 case 'a':
152 (*countA)++;
153 break;
153 break;
154
155 case 'B':
156 case 'b':
157 (*countB)++;
158 break;
159
160 case 'C':
161 case 'c':
162 (*countC)++;
163 break;
164
165 case 'D':
166 case 'd':
167 (*countD)++;
168 break;
169 case 'F':
170 case 'f':
171 (*countF)++;
172 break;
173
174 default:
175 printf("Invalid option\n");
176 break;
177 }
178 }
179 }
180 */
181
182
183 //Different Ways to read and input elements in array
184
185 //input:
186 for(int i = 0; i < length_of_array; i++)
187 (1) scanf("%d",&Array[i]);
188 (2) scanf("%d", Array+i)
189
190
191 //read:
192 for(int i =0; i < length_of_array; i++)
193 (1)printf("%d",A[i]);
194 (2)pritnf("%d",*(A+i))
195
196
197
198 // Notes on Pointers
199
200 {
201 int* a,b,c is the same thing int *a,b,c
202 as a is pointer and b , c are integers
203 int *a,*b,*c; all of them are pointers
204
205 1)
206 int *Z;
207 int *Y = 5;
208 Z = *Y + 2; This is error can`t assign integer value to pointer;
209 ------------------------------------------------------------------------
210 2)
211 If you have an array like this {1,2,3,4,5}
212 and pointer y int *y = 2;
213 this is not mean that y will carry the address of element 2 in the array this will lead to error as must to pass
214 ------------------------------------------------------------------------
215 3)
216 int x = 5, y=7;
217 int *pt1 = &y;
218 int *pt2 = &x;
219 *pt1 = pt1 - pt2 ; True this calc the difference position between pt1 and pt2 and the value is int
220 *pt1 = pt1 + pt2 ; Wrong
221 pt1 = pt1 - pt2 ; Wrong
222 pt1 = pt1 + pt2 ; Wrong
223 ------------------------------------------------------------------------
224 4)
225 int arr[] = {1,2,3,4,5}
226 int *ptr = &arr[2];
227 *(ptr++) this will get value of arr[2] then moving the pointer to the next element
228 *(ptr+1) this will move the pointer first to the next element then get the value of it
229 can generalize that the increment after the pointer name happened latest thing
230 but the +1 +2 +3 or the preincremement happened first
230 but the +1 +2 +3 or the preincremement happened first
231 ------------------------------------------------------------------------
232 5)
233 ++ moving the pointer permanently and +1 just move to the element until the proccess end then back to its first place
234 ex:
235 int arr[] = {1,2,3,4,5,6,7,8,9,10};
236 int *ptr1 = &arr[3];
237 int *ptr2 = ptr1++;
238 ptr1++;
239 printf("%d %d",*ptr1,*ptr2); output : 6 4 cuz we increased ptr1 twice and ptr2 4 cuz the ++ happen
240 lates thing mean the line will run and after finish increament
241
242 int arr[] = {1,2,3,4,5,6,7,8,9,10};
243 int *ptr1 = &arr[3];
244 int *ptr2 = ptr1+1;
245 ptr1+1;
246 printf("%d %d",*ptr1,*ptr2); output : 4 6 notice that the first place we put in ptr1 not changed when we wrote
247 ------------------------------------------------------------------------------
248 */
249 }
250
251 //Important Algorithms
252
253 1) Bubble Sort:
254
255 int flag = 0;
256 for (int i = 0; i < n - 1; i++)
257 {
258 flag = 0;
259 for (int j = 0; j < n - i - 1; j++)
260 {
261 change > to < to sort in descending order
262 if (a[j] > a[j+1])
263 {
264 int tmp = a[j];
265 a[j] = a[j+1];
266 a[j+1] = tmp;
267 flag = 1;
268 }
269 }
270 if (flag == 0)
271 break;
272 }
273 -----------------------------------------------------------------------------
274 2)Frequency Array
275
276 int occu(int *a,int sizea,int y){
277 int frequencyArray[100000];
278 for (int i = 0; i < 100000; i++)
279 {
280 frequencyArray[i] = 0;
281 }
282 for (int i = 0; i < sizea; i++)
283 {
284 frequencyArray[a[i]]++;
285 }
286 printf("The value %d reapted %d times\n",y,frequencyArray[y]);
287 return frequencyArray[y];
288 }
289 int a[10] = {1,1,3,5,10,10,10,99,99,90};
290 occu(a,10,1);
291 occu(a,10,3);
292 occu(a,10,5);
293 occu(a,10,10);
294 occu(a,10,90);
295 occu(a,10,99);
296 -----------------------------------------------------------------------
297 3)copy string to another without built-in function
298
299 void strcpy(char *s, char*t)
300 {
301 int i;
302 i = 0;
303 while ((s[i] = t[i]) != '\0')
304 i++;
305 Or
306 while ((*s = *t) != '\0'){
307 s++; t++;
307 s++; t++;
308 }
309 }
310
311 Recuresive Reverse Array (changing the array itself not only printing reverse)
312
313 void reverse(int *a, int n) {
314 if (n <= 1) {
315 return;
316 }
317 int tmp = a[0];
318 a[0] = a[n-1];
319 a[n-1] = tmp;
320 reverse(a + 1, n - 2);
321 }
322
323 //merge two sorted arrays
324
325 int *merge(int *a, int *b, int sizea, int sizeb)
326 {
327 int *z = (int *)malloc((sizea + sizeb) * sizeof(int));
328 int zindex = 0;
329 int i = 0, j = 0;
330 while (i < sizea && j < sizeb)
331 {
332 if (a[i] < b[j])
333 {
334 z[zindex] = a[i];
335 zindex++;
336 i++;
337 }
338 else
339 {
340 z[zindex] = b[j];
341 zindex++;
342 j++;
343 }
344 }
345
346 while (i < sizea)
347 {
348 z[zindex] = a[i];
349 zindex++;
350 i++;
351 }
352 while (j < sizeb)
353 {
354 z[zindex] = b[j];
355 zindex++;
356 j++;
357 }
358 return z;
359 }
360
361 //delete element in specific index in array
362
363
364 void del(int *a,int size,int index){
365 a[index] = 0;
366 for (int i = index; i < size-1; i++)
367 {
368 int tmp = *(a+i);
369 *(a+i) = *(a+i+1);
370 *(a+i+1) = tmp;
371 }
372 }
373
374 //Recursive search in array
375
376 int search(int *a,int size, int n){
377 if (*a == n)
378 return 1;
379 if(size <= 0)
380 return 0;
381 search(a+1,size-1,n);
382 }
383
384 //Insert in sorted manner in sorted array
385
386 int insert(int *a,int sizeA, int n){
387 int tmp; int inserted = 0;
388 for (int i = 0; i < sizeA; i++)
389 {
390 if(n < *(a+i) && !inserted){
391 tmp = *(a+i);
392 *(a+i) = n;
393 inserted = 1;
394 }
395 if(inserted){
396 int temp = *(a+i+1);
397 *(a+i+1) = tmp;
398 tmp = temp;
399 }
400 }
401 return sizeA+1;
402 }
403
404 void rotate(int *x, int size, int N, char D)
405 {
406 if (D == 'R')
407 {
408 for (int i = 0; i < size-N; i++)
409 x[i+N] = x[i];
410 for (int i = 0; i < N ; i++)
411 x[i] = 0;
412 }
413 else
414 {
415 for (int i = 0; i < size-N; i++)
416 x[i] = x[i+N];
417 for (int i = 0; i < N ; i++)
418 x[size-i-1] = 0;
419 }
420 }
421
422 Dynamic allocation
423
424 to make function return any pointer you need to store size in the heap to not lose the address after end the function
425
426
427 // Dynamic Allocation for 2D arrays
428 int **dynamicArray;
429 int rows, cols;
430 printf("Enter dimensions of the 2D array:\n");
431 printf("Rows: ");
432 scanf("%d", &rows);
433 printf("Columns: ");
434 scanf("%d", &cols);
435 dynamicArray = (int **)malloc(rows * sizeof(int *));
436 if (dynamicArray == NULL) {
437 printf("Memory allocation failed. Exiting...");
438 exit(1);
439 }
440 for (int i = 0; i < rows; i++) {
441 dynamicArray[i] = (int *)malloc(cols * sizeof(int));
442 if (dynamicArray[i] == NULL) {
443 printf("Memory allocation failed. Exiting...");
444 exit(1);
445 }
446 }
447 printf("Enter elements of the 2D array:\n");
448 inputArrayElements(dynamicArray, rows, cols);
449 printf("\n2D Array:\n");
450 displayArray(dynamicArray, rows, cols);
451 freeDynamicArray(dynamicArray, rows);
452 return 0;
453 }
454 void inputArrayElements(int** array, int rows, int cols) {
455 for (int i = 0; i < rows; i++) {
456 for (int j = 0; j < cols; j++) {
457 printf("Enter element at position (%d, %d): ", i + 1, j + 1);
458 scanf("%d", &array[i][j]);
459 }
460 }
460 }
461 }
462 void displayArray(int** array, int rows, int cols) {
463 for (int i = 0; i < rows; i++) {
464 for (int j = 0; j < cols; j++) {
465 printf("%d\t", array[i][j]);
466 }
467 printf("\n");
468 }
469 }
470 void freeDynamicArray(int** array, int rows) {
471 for (int i = 0; i < rows; i++) {
472 free(array[i]);
473 }
474 free(array);
475 }
476
477 ------------------------------------------------------------------------
478
479 //manual atoi
480
481 int manAtoi(char* str){
482 int i = 0;
483 int num = 0;
484 int sign = 1;
485 while (str[i] != '\0')
486 {
487 if (str[i]=='-')
488 sign = -1;
489 if (isdigit(str[i]))
490 num = 10*num + (str[i]-'0');
491 i++;
492 }
493 return sign*num;
494 }
495
496 //Remove first letter in each word then move the spaces to the end
497
498 void removeFC(char *str) {
499 int i = 0, j = 0;
500 int startOfWord = 1;
501 while (str[i] != '\0') {
502 if (isalpha(str[i])) {
503 if (startOfWord) {
504 i++;
505 startOfWord = 0;
506 } else {
507 str[j++] = str[i++];
508 }
509 } else {
510 str[j++] = str[i++];
511 startOfWord = (str[i-1] == ' ');
512 }
513 }
514 str[j] = '\0';
515 }
516
517
518 int occurrence(char* str1,char* str2){
519 //without functions
520 //WithoutOverlapping
521 int i = 0, j = 0;int len = strlen(str2);
522 int count = 0;
523 while (str1[i] != '\0')
524 {
525 if(str1[i] == str2[j]){
526 j++;
527 }
528 else{
529 j=0;
530 }
531 if (j == len)
532 {
533 count++;
534 j = 0;
535 i--; add this to make overlapping
536 }
537 i++;
537 i++;
538 }
539 return count;
540 with functions
541 int count = 0;int i =0;
542 while (str1[i] != '\0')
543 {
544 if(!strncmp(str1,str2,strlen(str2)))
545 count++;
546 str1+=strlen(str2); with overlapping
547 str1++; with over lapping
548 }
549 return count;
550 }