From 4d2df6170062ac7d52b5404c68849a17dbdf1eaa Mon Sep 17 00:00:00 2001 From: kaitou-1412 <47006057+kaitou-1412@users.noreply.github.com> Date: Tue, 1 Oct 2019 14:30:59 +0530 Subject: [PATCH] Add a file full of widely used C functions The following file contains extremely repeated C function snippets that can be quickly re-purposed for speed and efficiency. --- tricks.c | 143 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 tricks.c diff --git a/tricks.c b/tricks.c new file mode 100644 index 0000000..d496b93 --- /dev/null +++ b/tricks.c @@ -0,0 +1,143 @@ +#include +#include +#define MAX 101 +#define SWAP(X, Y, T) (T=X, X=Y, Y=T) +#define COMPARE(X,Y) ((XMAX)) + { + fprintf(stderr, "Improper value of n\n"); + exit(EXIT_FAILURE); + } + for (i = 0; i < n; ++i) + { + list[i] = rand()%1000; + printf("%d ", list[i]); + } + printf("\n"); +} +void print_array(int a[], int n) +{ + int j; + for (j = 0; j a[j+1]) + SWAP(a[j], a[j+1], t); +} +void linear_search(int a[], int size, int fai) +{ + int ele, c=0; + printf("Enter element\n"); + scanf("%d", &ele); + for (int i = 0; i <=fai; ++i) + { + if (ele == a[i]) + { + printf("Found it's position: %d\n", (i+1)); + c++; + } + } + if (c == 0) + { + printf("Not Found\n"); + } +} +int dec2bin(int n) +{ + if (n < 2) + return n; + int x = 0, i = 0; + while(n != 0) + { + x += (n%2)*pow(10, i++); + n = n/2; + } + return x; +} +int binary_search(int a[], int ele, int left, int right) +{ + int middle; + if(left<=right) + { + middle = (left+right)/2; + switch(COMPARE(a[middle], ele)) + { + case -1:return binary_search(a, ele, middle+1, right); + case 0:return middle; + case 1:return binary_search(a, ele, left, middle-1); + } + return -1; + } +} +int main() +{ + int t; + scanf("%d", &t); + while(t--) + { + int n; + printf("Enter number of elements: "); + scanf("%d", &n); + char s[n]; + fgets(s, sizeof(s), stdin); + int a[n]; + } + return 0; +}