Store Useful C Code

String in C

Only apply for school exs

  • Declare array as global variable
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <math.h>
#include <string.h> //strlen()
#include <stdlib.h> //atoi(): char to int
int main(){
char n[20];
scanf("%s",&n);
for (long long i = 0; i < strlen(n); i++)
{
if((n[i] - '0') == 7){
printf("number 7 encounter");
}
}
//the number entered:
long long num = (long long) (atoi(n));
printf("%lli", num);
}

Compare string:

1
n[i] == 'abc'
Resize array in C

Can’t get the length of element inside array, so you’ve to create another var as the length in order to get all the array’s element

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main(){

long long n,temp = 0,*array;
array = (long long *)malloc(6969 * sizeof(long long));
while(1){
scanf("%lli",&n);
if(n == 0){
break;
}
array[temp] = n;
temp++;
}
for (long long i = 0; i < temp; i++)
{
printf("So thu %lli la %lli\n",i,array[i]);
}
return 0;
}
Qsort & Bsearch & get Index in C

bsearch only works when the array is sorted from low to high (qsort)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <stdlib.h>

int cmpfunc (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}

void sortArray(int array[],int size){
qsort(array, size, sizeof(int), cmpfunc);
}

//only work when the array is sorted
int checkExist(int array[],int num,int array_amount){
int * ptr_item = (int*) bsearch (&num,array, array_amount, sizeof (int), cmpfunc);

if (ptr_item != NULL)
//found
return 1;
else
//not found
return 0;
}

get Index of a value in array, only works when bsearch return true;

1
2
int * ptr_item = (int*) bsearch (&num,array, array_amount, sizeof (int), cmpfunc);
int pos=(ptr_item - arrays)/sizeof(char);
get Closest number inside Array in C

source: https://www.geeksforgeeks.org/find-closest-number-array/
edit the getClosest() function to whether return the lower or higher value

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
int getClosest(int val1, int val2,
int target)
{
if(val1 < val2){
return val1;
}else{
return val2;
}
}

int findClosest(int arr[], int n, int target)
{
if (target <= arr[0])
return arr[0];
if (target >= arr[n - 1])
return arr[n - 1];
int i = 0, j = n, mid = 0;
while (i < j) {
mid = (i + j) / 2;
if (arr[mid] == target)
return arr[mid];
if (target < arr[mid]) {
if (mid > 0 && target > arr[mid - 1])
return getClosest(arr[mid - 1],
arr[mid], target);
j = mid;
}
else {
if (mid < n - 1 && target < arr[mid + 1])
return getClosest(arr[mid],
arr[mid + 1], target);
i = mid + 1;
}
}
return arr[mid];
}

call: int closest = findClosest(arrays, array_size (total of elements), number_to_find_closest);
Công thức Tổ hợp đệ quy Instead using C = n! / (k!(n-k)!) Ta dung C = C(k,n-1) \* n/(n-k)
1
2
3
4
5
6
C(k,n) k up,n down
long long tohop(int k, int n){
if(k==n || k==0) return 1;
return tohop(k,n-1) * n/(n-k);

}
Công thức truy hồi tìm số mũ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// x^n %k
#include <stdio.h>

long long power(int x,int n,int k){
if(n == 0) return 1;
long long rescurs = power(x,n/2,k);
rescurs = (rescurs*rescurs) %k;
if(n%2==0) return rescurs;
else return (rescurs*x) %k;
}

int main(){
long long x,n,k;
scanf("%lld %lld %lld",&x,&n,&k);
printf("%lld",power(x,n,k));

}

  • Some mistake that leads to unexpected bug
    • scanf(“%d”,&n);
      if the “%d” part is declared a wrong data type, or missing & in the second para, it will fatal
  • If you can use recursion function instead of loop, then use it, it will reduce execute time and easier to maintain