├── exercism ├── acronym │ ├── acronym.h │ └── acronym.c ├── hello-world │ ├── hello_world.h │ └── hello_world.c ├── isogram │ ├── isogram.h │ └── isogram.c ├── rna-transcription │ ├── rna_transcription.h │ └── rna_transcription.c ├── README.md └── word-count │ ├── word_count.h │ └── word_count.c ├── Hash ├── README.md ├── test_program.c ├── hash.h └── hash.c ├── misc ├── GCD.c ├── Factorial.c ├── Fibonacci.c ├── isArmstrong.c ├── Prime.c ├── TowerOfHanoi.c ├── strongNumber.c ├── palindrome.c ├── catalan.c ├── factorial_trailing_zeroes.c ├── Collatz.c ├── QUARTILE.c ├── demonetization.c ├── rselect.c ├── mirror ├── lexicographicPermutations.c ├── sudokusolver.c └── LongestSubSequence.c ├── Data Structures ├── Array │ ├── README.md │ ├── CArray.h │ ├── CArrayTests.c │ └── CArray.c ├── stack │ ├── README.md │ ├── stack.h │ ├── balanced parenthesis using stack in C │ ├── main.c │ └── stack.c ├── binary_trees │ ├── create_node.c │ └── recursive_traversals.c ├── dictionary │ ├── test_program.c │ ├── README.md │ ├── dict.h │ └── dict.c ├── linked_list │ ├── stack_using_linkedlists.c │ ├── singly_link_list_deletion.c │ └── mergeLinkedLists.c ├── queue.c ├── stack.c └── trie │ └── trie.c ├── Project Euler ├── Problem 01 │ ├── sol1.c │ ├── sol2.c │ └── sol3.c ├── Problem 03 │ ├── sol2.c │ └── sol1.c ├── Problem 02 │ └── so1.c └── README.md ├── CodingGuidelines.md ├── Sorts ├── countingSort.c ├── BogoSort.c ├── shaker_sort.c ├── OtherBubbleSort.c ├── shellSort.c ├── BubbleSort.c ├── binary_insertion_sort.c ├── InsertionSort.c ├── mergesort.c ├── HeapSort.c ├── SelectionSort.c └── QuickSort.c ├── conversions ├── binary2hexa.c ├── binary_to_decimal.c ├── binary2octal.c └── decimal _to_binary.c ├── Searches ├── LinearSearch.c ├── Jump_Search.c ├── interpolation_search.c ├── Other_Binary_Search.c ├── Binary_Search.c ├── fibonacciSearch.c └── modifiedBinarySearch.c ├── computer-oriented-statistical-methods ├── Seidal.C ├── simpson's 1-3rd rule.c ├── MEAN.C ├── MEDIAN.C ├── lagrange_theorem.C ├── variance.c └── Gauss_Elimination.c ├── Computer Oriented Statistical Methods ├── Simpson's_1-3rd_rule.c └── statistic │ ├── src │ ├── statistic.h │ └── statistic.c │ ├── README.md │ └── test │ └── test.c ├── Conversions ├── decimal_to_octal.c ├── toDecimal.c ├── decimal_to_hexa.c └── decimal _to_binary.c ├── README.md ├── sorting ├── HeapSort.c └── BucketSort.c └── data_structures ├── graphs ├── Floyd-Warshall.c ├── Dijkstra.c └── Bellman-Ford.c └── binary_trees └── binary_search_tree.c /exercism/acronym/acronym.h: -------------------------------------------------------------------------------- 1 | #ifndef ACRONYM_H 2 | #define ACRONYM_H 3 | 4 | char *abbreviate(const char *phrase); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /exercism/hello-world/hello_world.h: -------------------------------------------------------------------------------- 1 | #ifndef HELLO_WORLD_H 2 | #define HELLO_WORLD_H 3 | 4 | const char *hello(void); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /Hash/README.md: -------------------------------------------------------------------------------- 1 | # Hash algorithms 2 | 3 | Overview files **hash.h** and **hash.c** 4 | * sdbm 5 | * djb2 6 | * xor8 (8 bit) 7 | * adler_32 (32 bit) -------------------------------------------------------------------------------- /exercism/isogram/isogram.h: -------------------------------------------------------------------------------- 1 | #ifndef ISOGRAM_H 2 | #define ISOGRAM_H 3 | 4 | #include 5 | 6 | bool is_isogram(const char phrase[]); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /exercism/rna-transcription/rna_transcription.h: -------------------------------------------------------------------------------- 1 | #ifndef __RNA_TRANSCRIPTION__H 2 | #define __RNA_TRANSCRIPTION__H 3 | 4 | /* to_rna: compiles a DNA strand in its RNA complement */ 5 | char * to_rna(const char s[]); 6 | 7 | #endif -------------------------------------------------------------------------------- /exercism/README.md: -------------------------------------------------------------------------------- 1 | # Sample solutions for [exercism.io](http://exercism.io/) 2 | 3 | This directory contains some sample solutions for **exercism.io** 4 | 5 | ### Overview 6 | 7 | In this directory you will find (in the right order): 8 | * hello-world 9 | * isogram 10 | * acronym 11 | * word-count 12 | * rna-transcription 13 | 14 | -------------------------------------------------------------------------------- /exercism/hello-world/hello_world.c: -------------------------------------------------------------------------------- 1 | #include "hello_world.h" 2 | #include 3 | #include 4 | 5 | const char *hello(void) 6 | { 7 | char * ans = malloc(sizeof(char) * strlen("Hello, World!")); 8 | strcpy(ans,"Hello, World!"); 9 | 10 | /* string is pointer of the first character */ 11 | return ans; 12 | } 13 | -------------------------------------------------------------------------------- /misc/GCD.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Euclid's algorithm 4 | int GCD(int x, int y) { 5 | if (y == 0) 6 | return x; 7 | return GCD(y, x%y); 8 | } 9 | 10 | int main() { 11 | int a, b; 12 | printf("Input two numbers:\n"); 13 | scanf("%d %d", &a, &b); 14 | printf("Greatest common divisor: %d\n", GCD(a, b)); 15 | } 16 | -------------------------------------------------------------------------------- /misc/Factorial.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int fat(int number){ 4 | if (number == 1 || number == 0) 5 | return 1; 6 | else 7 | return number*fat(number-1); 8 | } 9 | 10 | int main(){ 11 | int number; 12 | 13 | //Asks the factorial of the number n 14 | printf("Number: "); 15 | scanf("%d", &number); 16 | 17 | printf("%d\n", fat(number)); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Data Structures/Array/README.md: -------------------------------------------------------------------------------- 1 | # Array 2 | 3 | Simple array of integers. With I/O functions, Sort Functions and Search Functions. 4 | 5 | ## Structure 6 | 7 | ```C 8 | typedef struct CArray { 9 | int *array; 10 | int size; 11 | } CArray; 12 | ``` 13 | 14 | ## Files 15 | 16 | * CArray.c - Array Implementations 17 | * CArray.h - Import for Usage 18 | * CArrayTests.c - Usage Examples and tests 19 | -------------------------------------------------------------------------------- /misc/Fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //Fibonnacci function 4 | int fib(int number){ 5 | if(number==1||number==2) return 1; 6 | else return fib(number-1)+fib(number-2); 7 | } 8 | 9 | int main(){ 10 | int number; 11 | 12 | //Asks for the number that is in n position in Fibonnacci sequence 13 | printf("Number: "); 14 | scanf("%d", &number); 15 | 16 | printf("%d \n", fib(number)); 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /misc/isArmstrong.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n, sum=0, i, num; 6 | printf("Enter number: "); 7 | scanf("%d",&n); 8 | num=n; 9 | while (n!=0) 10 | { 11 | i=n%10; 12 | sum=sum+(i*i*i); 13 | n=n/10; 14 | } 15 | if (sum==num) 16 | { 17 | printf("%d is an armstrong number!\n", num); 18 | } 19 | else 20 | { 21 | printf("%d is not an armstrong number!\n", num); 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Project Euler/Problem 01/sol1.c: -------------------------------------------------------------------------------- 1 | /* 2 | If we list all the natural numbers below 10 that are multiples of 3 or 5, 3 | we get 3, 5, 6 and 9. The sum of these multiples is 23. 4 | Find the sum of all the multiples of 3 or 5 below 1000. 5 | */ 6 | #include 7 | 8 | int main() { 9 | int n = 0; 10 | int sum = 0; 11 | scanf("%d", &n); 12 | for (int a = 0; a < n; a++) { 13 | if ((a % 3 == 0) || (a % 5 == 0)) { 14 | sum += a; 15 | } 16 | } 17 | 18 | printf("%d\n", sum); 19 | } -------------------------------------------------------------------------------- /CodingGuidelines.md: -------------------------------------------------------------------------------- 1 | # Code style convention 2 | 3 | Please orient on this guide before you sent a pull request. 4 | 5 | --- 6 | 7 | ## User-interface 8 | 9 | Please write a simple user interface for your programs. Not a blinking cursor! 10 | What does the program do? 11 | What want the program an user informations? 12 | 13 | --- 14 | 15 | ## Code style conventions 16 | 17 | See [here](https://users.ece.cmu.edu/~eno/coding/CCodingStandard.html) 18 | Don't push all code in one line! 19 | 20 | -------------------------------------------------------------------------------- /misc/Prime.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int isPrime(int x) { 5 | for (int i = 2; i < sqrt(x); i++) { 6 | if (x%i == 0) 7 | return 0; 8 | } 9 | return 1; 10 | 11 | } 12 | 13 | int main() { 14 | int a; 15 | printf("Input a number to see if it is a prime number:\n"); 16 | scanf("%d", &a); 17 | if (isPrime(a)) 18 | printf("%d is a prime number.\n", a); 19 | else 20 | printf("%d is not a prime number.\n", a); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Hash/test_program.c: -------------------------------------------------------------------------------- 1 | /* 2 | author: Christian Bender 3 | This file contains a simple test program for each hash-function. 4 | */ 5 | 6 | #include 7 | #include "hash.h" 8 | 9 | int main(void) 10 | { 11 | char s[] = "name"; 12 | 13 | /* actual tests */ 14 | printf("sdbm: %s --> %lld\n", s, sdbm(s)); 15 | printf("djb2: %s --> %lld\n", s, djb2(s)); 16 | printf("xor8: %s --> %i\n", s, xor8(s)); /* 8 bit */ 17 | printf("adler_32: %s --> %i\n", s, adler_32(s)); /* 32 bit */ 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Project Euler/Problem 03/sol2.c: -------------------------------------------------------------------------------- 1 | /* 2 | Problem: 3 | The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor of a given number N? 4 | e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17. 5 | */ 6 | #include 7 | 8 | int main() { 9 | int n = 0; 10 | scanf("%d", &n); 11 | int prime = 1; 12 | int i = 2; 13 | while (i*i <= n) { 14 | while (n%i == 0) { 15 | prime = i; 16 | n /= i; 17 | } 18 | i += 1; 19 | } 20 | if (n > 1) 21 | prime = n; 22 | printf("%d\n", prime); 23 | } -------------------------------------------------------------------------------- /Sorts/countingSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int i,n,l=0; 5 | scanf("%d",&n); 6 | int a[n]; 7 | for(i=0;il) 11 | l=a[i]; 12 | } 13 | int b[l+1]={0}; 14 | for(i=0;i0) 19 | { 20 | while(b[i]!=0) //for case when number exists more than once 21 | { 22 | printf("%d ",i); 23 | b[i]--; 24 | } 25 | } 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /conversions/binary2hexa.c: -------------------------------------------------------------------------------- 1 | /* 2 | * C Program to Convert Binary to Hexadecimal 3 | */ 4 | #include 5 | 6 | int main() 7 | { 8 | long int binary, hexa = 0, i = 1, remainder; 9 | 10 | printf("Enter the binary number: "); 11 | scanf("%ld", &binary); 12 | while (binary != 0) 13 | { 14 | remainder = binary % 10; 15 | hexa = hexa + remainder * i; 16 | i = i * 2; 17 | binary = binary / 10; 18 | } 19 | printf("THe Equivalent hexadecimal value: %lX", hexa); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /conversions/binary_to_decimal.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Modified 07/12/2017, Kyler Smith 3 | * 4 | */ 5 | 6 | #include 7 | 8 | int main() { 9 | 10 | int remainder, number = 0, decimal_number = 0, temp = 1; 11 | printf("Enter any binary number= "); 12 | scanf("%d", &number); 13 | 14 | // Iterate over the number until the end. 15 | while(number > 0) { 16 | 17 | remainder = number % 10; 18 | number = number / 10; 19 | decimal_number += remainder * temp; 20 | temp = temp*2; //used as power of 2 21 | 22 | } 23 | 24 | printf("%d\n", decimal_number); 25 | } 26 | -------------------------------------------------------------------------------- /misc/TowerOfHanoi.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | // Function for Tower of Hanoi algorithm 6 | void hanoi(int noOfDisks,char where,char to,char extra){ 7 | if(noOfDisks == 0 ) 8 | { 9 | return; 10 | } 11 | else 12 | { 13 | hanoi(noOfDisks-1, where, extra , to); 14 | printf("Move disk : %d from %c to %c\n",noOfDisks,where,to); 15 | hanoi(noOfDisks-1,extra,to,where); 16 | } 17 | } 18 | int main(void){ 19 | int noOfDisks; 20 | 21 | //Asks the number of disks in the tower 22 | printf("Number of disks: \n"); 23 | scanf("%d", &noOfDisks); 24 | 25 | hanoi(noOfDisks,'A','B','C'); 26 | 27 | return 0; 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Searches/LinearSearch.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int linearsearch(int *arr, int size, int val){ 4 | int i; 5 | for (i = 0; i < size; i++){ 6 | if (arr[i] == val) 7 | return 1; 8 | } 9 | return 0; 10 | } 11 | 12 | void main(){ 13 | int s,i,v; 14 | printf("Enter the size of the array:\n"); 15 | scanf("%d",&s); 16 | 17 | int a[s]; 18 | printf("Enter the contents for an array of size %d:\n", s); 19 | for (i = 0; i < s; i++) scanf("%d", &a[i]); 20 | 21 | printf("Enter the value to be searched:\n"); 22 | scanf("%d", &v); 23 | if (linearsearch(a, s, v)) 24 | printf("Value %d is in the array.\n", v); 25 | else 26 | printf("Value %d is not in the array.\n", v); 27 | } 28 | -------------------------------------------------------------------------------- /misc/strongNumber.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Modified on 07/12/2017, Kyler Smith 3 | * 4 | * A number is called strong number if sum of the 5 | * factorial of its digit is equal to number itself. 6 | */ 7 | 8 | #include 9 | 10 | 11 | void strng(int a) 12 | { 13 | int j=a; 14 | int sum=0; 15 | int b,i,fact=1; 16 | while(a>0) 17 | { 18 | fact=1; 19 | b=a%10; 20 | for(i=1;i<=b;i++) 21 | { 22 | fact=fact*i; 23 | } 24 | a=a/10; 25 | sum=sum+fact; 26 | } 27 | if(sum==j) 28 | printf("%d is a strong number",j); 29 | else 30 | printf("%d is not a strong number",j); 31 | } 32 | void main() 33 | { 34 | int a; 35 | printf("Enter the number to check"); 36 | scanf("%d",&a); 37 | strng(a); 38 | } 39 | -------------------------------------------------------------------------------- /misc/palindrome.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n, reversedInteger = 0, remainder, originalInteger; 5 | 6 | printf("Enter an integer: "); 7 | scanf("%d", &n); 8 | 9 | originalInteger = n; 10 | 11 | // reversed integer is stored in variable 12 | while( n!=0 ) 13 | { 14 | remainder = n%10; 15 | reversedInteger = reversedInteger*10 + remainder; 16 | n /= 10; 17 | } 18 | 19 | // palindrome if orignalInteger and reversedInteger are equal 20 | if (originalInteger == reversedInteger) 21 | printf("%d is a palindrome.", originalInteger); 22 | else 23 | printf("%d is not a palindrome.", originalInteger); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Project Euler/Problem 02/so1.c: -------------------------------------------------------------------------------- 1 | /* 2 | Problem: 3 | Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, 4 | the first 10 terms will be: 5 | 1,2,3,5,8,13,21,34,55,89,.. 6 | By considering the terms in the Fibonacci sequence whose values do not exceed n, find the sum of the even-valued terms. 7 | e.g. for n=10, we have {2,8}, sum is 10. 8 | */ 9 | #include 10 | 11 | int main() { 12 | int n = 0; 13 | int sum = 0; 14 | int i = 1; 15 | int j = 2; 16 | int temp; 17 | scanf("%d", &n); 18 | 19 | while (j <= n) { 20 | if ((j & 1) == 0) //can also use(j%2 == 0) 21 | sum += j; 22 | temp = i; 23 | i = j; 24 | j = temp + i; 25 | } 26 | 27 | printf("%d\n", sum); 28 | } -------------------------------------------------------------------------------- /Project Euler/Problem 01/sol2.c: -------------------------------------------------------------------------------- 1 | /* 2 | If we list all the natural numbers below 10 that are multiples of 3 or 5, 3 | we get 3,5,6 and 9. The sum of these multiples is 23. 4 | Find the sum of all the multiples of 3 or 5 below N. 5 | ''' 6 | ''' 7 | This solution is based on the pattern that the successive numbers in the series follow: 0+3,+2,+1,+3,+1,+2,+3. 8 | */ 9 | #include 10 | 11 | int main() { 12 | int n = 0; 13 | int sum = 0; 14 | scanf("%d", &n); 15 | 16 | int terms = (n - 1) / 3; 17 | sum += ((terms)*(6 + (terms - 1) * 3)) / 2; //sum of an A.P. 18 | terms = (n - 1) / 5; 19 | sum += ((terms)*(10 + (terms - 1) * 5)) / 2; 20 | terms = (n - 1) / 15; 21 | sum -= ((terms)*(30 + (terms - 1) * 15)) / 2; 22 | 23 | printf("%d\n", sum); 24 | } -------------------------------------------------------------------------------- /computer-oriented-statistical-methods/Seidal.C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | void main() 5 | { 6 | float a,b,c,a1,a2,a3,b1,b2,b3,c1,c2,c3,d1,d2,d3,x1,x2,x3; 7 | clrscr(); 8 | printf("Enter values of eq1:"); 9 | scanf("%f%f%f%f",&a1,&a2,&a3,&d1); 10 | printf("Enter values of eq2:"); 11 | scanf("%f%f%f%f",&b1,&b2,&b3,&d2); 12 | printf("Enter values of eq3:"); 13 | scanf("%f%f%f%f",&c1,&c2,&c3,&d3); 14 | x1=x2=x3=0.0; 15 | do 16 | { 17 | a=x1; 18 | b=x2; 19 | c=x3; 20 | x1=(1/a1)*(d1-(a2*x2)-(a3*x3)); 21 | x2=(1/b2)*(d2-(b1*x1)-(b3*x3)); 22 | x3=(1/c3)*(d3-(c1*x1)-(c2*x2)); 23 | }while(fabs(x1-a)>0.0001&&fabs(x2-b)>0.0001&&fabs(x3-c)>0.0001); 24 | printf("x1=%f\nx2=%f\nx3=%f",x1,x2,x3); 25 | getch(); 26 | } -------------------------------------------------------------------------------- /misc/catalan.c: -------------------------------------------------------------------------------- 1 | /* 2 | code for computing nth catalan number 3 | */ 4 | #include 5 | long int factorial(int x) //long int for more than 10 factorial 6 | { 7 | int i; 8 | long int fac; //fac stores x factorial 9 | fac=x; 10 | for(i=1;i 2 | #include 3 | #include 4 | float f(float x) 5 | { 6 | return 1.0+x*x*x; 7 | } 8 | void main() 9 | { 10 | int i,n; 11 | float a,b,h,x,s2,s3,sum,integral; 12 | printf("enter the lower limit of the integration"); 13 | sacnf("%f",&a); 14 | printf("enter the upper limit of the integration"); 15 | sacnf("%f",&b); 16 | printf("enter the number of intervals"); 17 | sacnf("%d",&n); 18 | h=(b-a)/n; 19 | sum=f(a)+f(b); 20 | s2=s3=0.0; 21 | for(i=1;i 2 | #include 3 | #include 4 | float f(float x) 5 | { 6 | return 1.0+x*x*x; 7 | } 8 | void main() 9 | { 10 | int i,n; 11 | float a,b,h,x,s2,s3,sum,integral; 12 | printf("enter the lower limit of the integration"); 13 | sacnf("%f",&a); 14 | printf("enter the upper limit of the integration"); 15 | sacnf("%f",&b); 16 | printf("enter the number of intervals"); 17 | sacnf("%d",&n); 18 | h=(b-a)/n; 19 | sum=f(a)+f(b); 20 | s2=s3=0.0; 21 | for(i=1;i 2 | #include 3 | #include 4 | 5 | bool check_sorted(int *a, int n) 6 | { 7 | while ( --n >= 1 ) { 8 | if ( a[n] < a[n-1] ) return false; 9 | } 10 | return true; 11 | } 12 | 13 | void shuffle(int *a, int n) 14 | { 15 | int i, t, r; 16 | for(i=0; i < n; i++) { 17 | t = a[i]; 18 | r = rand() % n; 19 | a[i] = a[r]; 20 | a[r] = t; 21 | } 22 | } 23 | 24 | void sort(int *a, int n) 25 | { 26 | while ( !check_sorted(a, n) ) shuffle(a, n); 27 | } 28 | 29 | int main() 30 | { 31 | int numbers[6]; 32 | int i; 33 | printf("Enter 6 numbers unsorted \n\n"); 34 | for(i=0;i<6;i++){ 35 | scanf("%d",&numbers[i]); 36 | } 37 | sort(numbers, 6); 38 | for (i=0; i < 6; i++) printf("%d ", numbers[i]); 39 | printf("\n"); 40 | } 41 | -------------------------------------------------------------------------------- /misc/factorial_trailing_zeroes.c: -------------------------------------------------------------------------------- 1 | /* 2 | programme for computing number of zeroes at the end of factorial of a given number n 3 | */ 4 | #include 5 | #include //including math.h header file to use pow function 6 | int main() 7 | { 8 | int i,n,test=0,count=0; 9 | //taking input number n 10 | scanf("%d",&n); 11 | 12 | //looping from 1 till loop break 13 | for(i=1;;i++) 14 | { 15 | test=n/pow(5,i);//division of n by ith power of 5(storing in integer form) 16 | if(test!=0) //condition for zeroes at end corresponding individual ith case 17 | { 18 | count=count+test; 19 | } 20 | else 21 | break; //break the loop for if test=0 22 | } 23 | printf("%d\n",count); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Sorts/shaker_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void swap(int *a, int *b){ 4 | int temp; 5 | temp = *a; 6 | *a = *b; 7 | *b = temp; 8 | } 9 | void shakersort(int a[], int n) 10 | { 11 | int p, i; 12 | for (p = 1; p <= n / 2; p++) 13 | { 14 | for (i = p - 1; i < n - p; i++) 15 | if (a[i] > a[i+1]){ 16 | swap(&a[i], &a[i + 1]); 17 | } 18 | for (i = n - p - 1; i >= p; i--) 19 | if (a[i] < a[i-1]){ 20 | swap(&a[i], &a[i - 1]); 21 | } 22 | } 23 | } 24 | int main() 25 | { 26 | int n; 27 | scanf("%d",&n); 28 | int arr[n] ,i; 29 | for (i = 0 ; i < n; i++) 30 | scanf("%d ", &arr[i]); 31 | shakersort(arr, n); 32 | for (i = 0 ; i < n; i++) 33 | printf("%d ", arr[i]); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /misc/Collatz.c: -------------------------------------------------------------------------------- 1 | /* 2 | collatz conjecture: a series for a number n in which if n even then the next number is n/2 ,but if n is odd then the next number is 3n+1. 3 | this series continues till it reaches 1*/ 4 | 5 | #include 6 | int main() 7 | { 8 | int n,curr_no; 9 | scanf("%d",&n); //input number 10 | curr_no=n; //curr_no stores input number n 11 | while(curr_no!=1) //loop till series reaches 1 12 | { 13 | if(curr_no%2==0) //condition for even number 14 | { 15 | curr_no=curr_no/2; 16 | printf("%d->",curr_no); 17 | } 18 | else 19 | { 20 | curr_no=(curr_no*3)+1; //condition for odd number 21 | printf("%d->",curr_no); 22 | } 23 | } 24 | printf("1"); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Conversions/decimal_to_octal.c: -------------------------------------------------------------------------------- 1 | /*****Decimal to octal conversion*******************/ 2 | #include 3 | void decimal2Octal(long decimalnum); 4 | 5 | int main(){ 6 | 7 | long decimalnum; 8 | 9 | printf("Enter the decimal number: "); 10 | scanf("%ld", &decimalnum); 11 | 12 | decimal2Octal(decimalnum); 13 | 14 | return 0; 15 | } 16 | 17 | /********function for convert decimal numbers to octal numbers************/ 18 | void decimal2Octal(long decimalnum){ 19 | long remainder, quotient; 20 | 21 | int octalNumber[100], i = 1, j; 22 | quotient = decimalnum; 23 | 24 | while (quotient != 0){ 25 | octalNumber[i++] = quotient % 8; 26 | 27 | quotient = quotient / 8; 28 | } 29 | 30 | for (j = i - 1; j > 0; j--) 31 | 32 | printf("%d", octalNumber[j]); 33 | 34 | printf("\n"); 35 | } 36 | -------------------------------------------------------------------------------- /Searches/Jump_Search.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define min(X,Y) ((X) < (Y) ? (X) : (Y)) 4 | int jump_search(int* arr, int x); 5 | int n; 6 | 7 | int main() { 8 | int arr[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610 }; 9 | n = sizeof(arr) / sizeof(int); 10 | int x = 55; 11 | int index = jump_search(arr, x); 12 | printf("\nNumber %d is at index %d\n", x, index); 13 | } 14 | 15 | int jump_search(int* arr, int x) { 16 | int step = floor(sqrt(n)); 17 | int prev = 0; 18 | while (*(arr + (min(step, n) - 1)) < x) { 19 | prev = step; 20 | step += floor(sqrt(n)); 21 | if (prev >= n) 22 | return -1; 23 | } 24 | 25 | while (*(arr + prev) < x) { 26 | prev = prev + 1; 27 | if (prev == fmin(step, n)) 28 | return -1; 29 | } 30 | if (*(arr + prev) == x) 31 | return prev; 32 | return -1; 33 | } 34 | -------------------------------------------------------------------------------- /Conversions/toDecimal.c: -------------------------------------------------------------------------------- 1 | /* 2 | * convert from any base to decimal 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | int main(void) { 9 | int base, i, j; 10 | char number[100]; 11 | unsigned long decimal = 0; 12 | 13 | printf("Enter the base: "); 14 | scanf("%d", &base); 15 | printf("Enter the number: "); 16 | scanf("%s", &number[0]); 17 | 18 | for (i = 0; number[i] != '\0'; i++) { 19 | if (isdigit(number[i])) 20 | number[i] -= '0'; 21 | else if (isupper(number[i])) 22 | number[i] -= 'A' - 10; 23 | else if (islower(number[i])) 24 | number[i] -= 'a' - 10; 25 | else 26 | number[i] = base + 1; 27 | 28 | if (number[i] > base) 29 | printf("invalid number\n"); 30 | } 31 | 32 | for (j = 0; j < i; j++) { 33 | decimal *= base; 34 | decimal += number[j]; 35 | } 36 | 37 | printf("%lu\n", decimal); 38 | } 39 | -------------------------------------------------------------------------------- /computer-oriented-statistical-methods/MEAN.C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | void main() 5 | { 6 | int a[10],n,i,j,temp,sum=0; 7 | float mean; 8 | clrscr(); 9 | printf("Enter no. for Random Numbers :"); 10 | scanf("%d",&n); 11 | for(i=0;i 2 | #include 3 | #include 4 | 5 | char *to_rna(const char s[]) 6 | { 7 | 8 | /* determines the length of the given string */ 9 | int len = strlen(s); 10 | 11 | /* creates a return string */ 12 | char *ans = malloc(sizeof(char) * len); 13 | 14 | /* for the loop */ 15 | int i = 0; 16 | 17 | /* actual compile process */ 18 | for (i = 0; i < len; i++) 19 | { 20 | switch (s[i]) 21 | { 22 | case 'G': 23 | ans[i] = 'C'; 24 | break; 25 | case 'C': 26 | ans[i] = 'G'; 27 | break; 28 | case 'T': 29 | ans[i] = 'A'; 30 | break; 31 | case 'A': 32 | ans[i] = 'U'; 33 | break; 34 | } 35 | } 36 | 37 | return ans; 38 | } -------------------------------------------------------------------------------- /Data Structures/stack/README.md: -------------------------------------------------------------------------------- 1 | # Simple generic Stack 2 | 3 | This is a modular generic stack data-structure. The stack is self growing. 4 | 5 | ### Content 6 | 7 | * stack-Header file for import. 8 | * stack.c implementation of the stack 9 | * main.c framework program for testing. 10 | 11 | You need to only import the **stack.h** 12 | 13 | ### Public interface 14 | 15 | ``` void initStack(); ``` 16 | 17 | Initializes the stack with a capacity of 10 elements. 18 | 19 | ``` void push(void * object); ``` 20 | 21 | pushs the argument onto the stack 22 | 23 | ``` void * pop(); ``` 24 | 25 | pop: pops the top element of the stack from the stack. 26 | 27 | assumes: stack not empty. 28 | 29 | ``` int size(); ``` 30 | 31 | gets the number of elements of the stack. 32 | 33 | ``` int isEmpty(); ``` 34 | 35 | returns 1 if stack is empty otherwise 0. 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /misc/QUARTILE.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | void main() 5 | { 6 | int a[10],n,i,j,temp; 7 | float q1,q3,iqr; 8 | clrscr(); 9 | printf("Enter no. for Random Numbers :"); 10 | scanf("%d",&n); 11 | for(i=0;i 2 | int interpolationSearch(int arr[], int n, int x) 3 | { 4 | int q=NULL; 5 | while(q 2 | //#include 3 | #include 4 | 5 | void main() 6 | { 7 | int a[10],n,i,j,temp; 8 | float mean,median; 9 | clrscr(); 10 | printf("Enter no. for Random Numbers :"); 11 | scanf("%d",&n); 12 | for(i=0;i 2 | #include 3 | #define len 5 4 | 5 | 6 | int binarySearch(int array[], int leng, int searchX) 7 | { 8 | int pos = -1, right, left, i = 0; 9 | 10 | left = 0; 11 | right = leng - 1; 12 | 13 | for (i = 0; i < leng; i++) 14 | { 15 | pos = (left + right) / 2; 16 | 17 | if (array[pos] == searchX) 18 | return pos; 19 | else 20 | { 21 | if (array[pos] < searchX) 22 | left = pos + 1; 23 | else 24 | { 25 | right = pos - 1; 26 | } 27 | } 28 | } 29 | 30 | 31 | 32 | } 33 | 34 | 35 | void main(int argc, char *argv[]) 36 | { 37 | int array[len] = { 5, 8 , 10 , 14 ,16 }; 38 | 39 | int position; 40 | position = binarySearch(array, len, 5); 41 | 42 | if (position < 0) 43 | printf("The number %d doesnt exist in array\n", 5); 44 | else 45 | { 46 | printf("The number %d exist in array at position : %d \n", 5, position); 47 | } 48 | 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Hash/hash.h: -------------------------------------------------------------------------------- 1 | /* 2 | author: Christian Bender 3 | This file contains the public interface 4 | 5 | Overview about hash-functions: 6 | 7 | - sdbm 8 | - djb2 9 | - xor8 (8 bit) 10 | - adler_32 (32 bits) 11 | */ 12 | 13 | #ifndef __HASH__H 14 | #define __HASH__H 15 | 16 | /* 17 | sdbm: implements the sdbm hash-algorithm 18 | returns a whole number of type long long. 19 | */ 20 | long long sdbm(char[]); 21 | 22 | /* 23 | djb2: implements the djb2 hash-algorithm 24 | returns a whole number of type long long. 25 | */ 26 | long long djb2(char[]); 27 | 28 | /* 29 | xor8: implements the xor8 hash-algorithm 30 | returns a whole number of type char. 31 | length: 8 bit 32 | */ 33 | char xor8(char[]); 34 | 35 | /* 36 | adler_32: implements the adler-32 hash-algorithm 37 | returns a whole number of type int. 38 | length: 32 bit 39 | assumes: int has a length of 32 bits. 40 | */ 41 | int adler_32(char[]); 42 | 43 | #endif -------------------------------------------------------------------------------- /computer-oriented-statistical-methods/lagrange_theorem.C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | void main() 6 | { 7 | float x[20],y[20],a,sum,p; 8 | int n,i,j; 9 | clrscr(); 10 | printf("Enter the no of entry to insert->"); 11 | scanf("%d",&n); 12 | 13 | for(i=0;i",i); 16 | scanf("%f",&x[i]); 17 | printf("enter the value of y%d->",i); 18 | scanf("%f",&y[i]); 19 | } 20 | printf("\n X \t\t Y \n"); 21 | printf("----------------------------\n"); 22 | for(i=0;i%f",sum); 43 | getch(); 44 | 45 | }} -------------------------------------------------------------------------------- /Data Structures/binary_trees/create_node.c: -------------------------------------------------------------------------------- 1 | /* Includes structure for a node and a newNode() function which 2 | can be used to create a new node in the tree. 3 | It is assumed that the data in nodes will be an integer, though 4 | function can be modified according to the data type, easily. 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | struct node 11 | { 12 | struct node *leftNode; 13 | int data; 14 | struct node *rightNode; 15 | }; 16 | 17 | struct node *newNode(int data) 18 | { 19 | struct node *node = (struct node *)malloc(sizeof(struct node)); 20 | 21 | node->leftNode = NULL; 22 | node->data = data; 23 | node->rightNode = NULL; 24 | 25 | return node; 26 | } 27 | 28 | int main(void) 29 | { 30 | /* new node can be created here as :- 31 | 32 | struct node *nameOfNode = newNode(data); 33 | 34 | and tree can be formed by creating further nodes at 35 | nameOfNode->leftNode and so on. 36 | */ 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /misc/demonetization.c: -------------------------------------------------------------------------------- 1 | // Recursion problem 2 | //Given the denominations of currencies available in a system, find the number of ways an ATM machine can 3 | //generate notes for an entered amount N. 4 | 5 | #include 6 | 7 | int ways(int n, int a[], int k) 8 | { 9 | if(n<0 || k<0) return 0; 10 | if(n == 0) return 1; 11 | if(k == 0) return 0; 12 | return ways(n, a, k-1) + ways(n-a[k-1], a, k); 13 | } 14 | 15 | int main() 16 | { 17 | int m; 18 | int t; 19 | int n; 20 | 21 | printf("Number of coins? "); 22 | scanf("%d", &m); 23 | int coin[m], i; 24 | for(i=0; i 10 | 11 | int main() { 12 | int n = 0; 13 | int sum = 0; 14 | int num = 0; 15 | scanf("%d", &n); 16 | 17 | while (1) { 18 | num += 3; 19 | if (num >= n) 20 | break; 21 | sum += num; 22 | num += 2; 23 | if (num >= n) 24 | break; 25 | sum += num; 26 | num += 1; 27 | if (num >= n) 28 | break; 29 | sum += num; 30 | num += 3; 31 | if (num >= n) 32 | break; 33 | sum += num; 34 | num += 1; 35 | if (num >= n) 36 | break; 37 | sum += num; 38 | num += 2; 39 | if (num >= n) 40 | break; 41 | sum += num; 42 | num += 3; 43 | if (num >= n) 44 | break; 45 | sum += num; 46 | } 47 | 48 | printf("%d\n", sum); 49 | } -------------------------------------------------------------------------------- /exercism/word-count/word_count.h: -------------------------------------------------------------------------------- 1 | #ifndef WORD_COUNT_H 2 | #define WORD_COUNT_H 3 | 4 | #define MAX_WORDS 20 // at most MAX_WORDS can be found in the test input string 5 | #define MAX_WORD_LENGTH 50 // no individual word can exceed this length 6 | 7 | // results structure 8 | typedef struct word_count_word { 9 | char text[MAX_WORD_LENGTH]; 10 | int count; 11 | } word_count_word_t; 12 | 13 | #define EXCESSIVE_LENGTH_WORD -1 14 | #define EXCESSIVE_NUMBER_OF_WORDS -2 15 | 16 | // word_count - routine to classify the unique words and their frequency in a test input string 17 | // inputs: 18 | // input_text = a null-terminated string containing that is analyzed 19 | // 20 | // outputs: 21 | // words = allocated structure to record the words found and their frequency 22 | // uniqueWords - number of words in the words structure 23 | // returns a negative number if an error. 24 | // words will contain the results up to that point. 25 | int word_count(const char *input_text, word_count_word_t * words); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /Conversions/decimal_to_hexa.c: -------------------------------------------------------------------------------- 1 | /*****Decimal to Hexadecimal conversion*******************/ 2 | #include 3 | void decimal2Hexadecimal(long num); 4 | 5 | 6 | int main(){ 7 | 8 | long decimalnum; 9 | 10 | printf("Enter decimal number: "); 11 | scanf("%ld", &decimalnum); 12 | 13 | decimal2Hexadecimal(decimalnum); 14 | 15 | return 0; 16 | } 17 | 18 | /********function for convert decimal number to hexadecimal number****************/ 19 | void decimal2Hexadecimal(long num){ 20 | 21 | long decimalnum=num; 22 | long quotient, remainder; 23 | int i, j = 0; 24 | char hexadecimalnum[100]; 25 | 26 | quotient = decimalnum; 27 | 28 | while (quotient != 0){ 29 | 30 | remainder = quotient % 16; 31 | if (remainder < 10) 32 | hexadecimalnum[j++] = 48 + remainder; 33 | 34 | else 35 | hexadecimalnum[j++] = 55 + remainder; 36 | 37 | quotient = quotient / 16;} 38 | 39 | // print the hexadecimal number 40 | 41 | for (i = j; i >= 0; i--){ 42 | printf("%c", hexadecimalnum[i]);} 43 | 44 | printf("\n"); 45 | } 46 | 47 | -------------------------------------------------------------------------------- /Sorts/OtherBubbleSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 20 4 | #define TRUE 1 5 | #define FALSE 0 6 | 7 | 8 | int main() 9 | { 10 | 11 | int i , arraySort[MAX] ={0} , isSort = FALSE, changePlace; 12 | 13 | 14 | /* For example 15 | Insertion random values in array to test 16 | */ 17 | 18 | 19 | for(i = 0 ; i < MAX; i++) 20 | { 21 | arraySort[i] = rand()%101 ; 22 | } 23 | 24 | 25 | /* Algorithm of bubble methods */ 26 | 27 | while(isSort) 28 | { 29 | isSort = FALSE; 30 | 31 | for( i = 0 ; i < MAX - 1 ; i++) 32 | { 33 | if(arraySort[i] > arraySort[i+1]) 34 | { 35 | changePlace = arratSort[i]; 36 | arraySort[i] = arraySort[i+1]; 37 | arraySort[i+1] = changePlace ; 38 | isSort = TRUE; 39 | } 40 | 41 | } 42 | } 43 | 44 | /* See if it works */ 45 | 46 | for(i = 0 ; i < MAX; i++) 47 | { 48 | printf("%d\n", arraySort[i]); 49 | } 50 | 51 | 52 | 53 | 54 | return EXIT_SUCCESS; 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Data Structures/binary_trees/recursive_traversals.c: -------------------------------------------------------------------------------- 1 | /* Includes the functions for Recursive Traversals 2 | of a Binary Tree. It is assumed that nodes and 3 | tree have been created as per create_node.c 4 | */ 5 | 6 | #include 7 | 8 | void inOrderTraversal(struct node *node) 9 | { 10 | if(node == NULL) //if tree is empty 11 | return; 12 | 13 | inOrderTraversal(node->leftNode); 14 | printf("\t%d\t", node->data); 15 | inOrderTraversal(node->rightNode); 16 | } 17 | 18 | void preOrderTraversal(struct node *node) 19 | { 20 | if(node == NULL) //if tree is empty 21 | return; 22 | 23 | printf("\t%d\t", node->data); 24 | preOrderTraversal(node->leftNode); 25 | preOrderTraversal(node->rightNode); 26 | } 27 | 28 | void postOrderTraversal(struct node *node) 29 | { 30 | if(node == NULL) //if tree is empty 31 | return; 32 | 33 | postOrderTraversal(node->leftNode); 34 | postOrderTraversal(node->rightNode); 35 | printf("\t%d\t",node->data); 36 | } 37 | 38 | int main(void) 39 | { 40 | /* traversals can be done by simply invoking the 41 | function with a pointer to the root node. 42 | */ 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Project Euler/Problem 03/sol1.c: -------------------------------------------------------------------------------- 1 | /* 2 | Problem: 3 | The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor of a given number N? 4 | e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17. 5 | */ 6 | #include 7 | #include 8 | 9 | int isprime(int no) { 10 | int sq; 11 | 12 | if (no == 2) { 13 | return 1; 14 | } 15 | else if (no%2 == 0) { 16 | return 0; 17 | } 18 | sq = ((int)(sqrt(no))) + 1; 19 | for (int i = 3; i < sq; i + 2) { 20 | if (no%i == 0) { 21 | return 0; 22 | } 23 | } 24 | return 1; 25 | } 26 | 27 | int main() { 28 | int maxNumber = 0; 29 | int n = 0; 30 | int n1; 31 | scanf("%d", &n); 32 | if (isprime(n) == 1) 33 | printf("%d", n); 34 | else { 35 | while (n % 2 == 0) { 36 | n = n / 2; 37 | } 38 | if (isprime(n) == 1) { 39 | printf("%d\n", n); 40 | } 41 | else { 42 | n1 = ((int)(sqrt(n))) + 1; 43 | for (int i = 3; i < n1; i + 2) { 44 | if (n%i == 0) { 45 | if (isprime((int)(n / i)) == 1) { 46 | maxNumber = n / i; 47 | break; 48 | } 49 | else if (isprime(i) == 1) { 50 | maxNumber = i; 51 | } 52 | } 53 | } 54 | printf("%d\n", maxNumber); 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /Searches/Binary_Search.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Recursive Function- It returns location of x assumiung array arr[l..r] is present, otherwise -1 4 | 5 | int binarysearch(int arr[], int l, int r, int x) 6 | { 7 | if (r >= l) 8 | { 9 | int mid = l + (r - l)/2; 10 | 11 | // If element is present at middle 12 | if (arr[mid] == x) return mid; 13 | 14 | // If element is smaller than middle 15 | if (arr[mid] > x) return binarysearch(arr, l, mid-1, x); 16 | 17 | // Else element is in right subarray 18 | return binarysearch(arr, mid+1, r, x); 19 | } 20 | 21 | // When element is not present in array 22 | return -1; 23 | } 24 | 25 | int main(void) 26 | { 27 | // give function an array to work with 28 | int arr[] = {2, 3, 4, 10, 40}; 29 | // get size of array 30 | int n = sizeof(arr)/ sizeof(arr[0]); 31 | //set value to look for 32 | int x = 10; 33 | // set result to what is returned from binarysearch 34 | int result = binarysearch(arr, 0, n-1, x); 35 | // print out result 36 | (result == -1) ? printf("Element is not in the array\n") 37 | : printf("Element is present at index %d\n", result); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Sorts/shellSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void shellSort(int array[], int value){ 6 | int i = value; 7 | int j, k, tmp; 8 | for (i = value / 2; i > 0; i = i / 2){ 9 | for (j = i; j < value; j++){ 10 | for(k = j - i; k >= 0; k = k - i){ 11 | if (array[k+i] >= array[k]){ 12 | break; 13 | } 14 | else{ 15 | tmp = array[k]; 16 | array[k] = array[k+i]; 17 | array[k+i] = tmp; 18 | } 19 | } 20 | } 21 | } 22 | } 23 | 24 | 25 | int main(){ 26 | 27 | int array[20]; 28 | int range = 500; 29 | for(int i = 0; i < 100; i++){ 30 | array[i] = rand() % range + 1; 31 | } 32 | int size = sizeof array / sizeof array[0]; 33 | 34 | 35 | 36 | clock_t start = clock(); 37 | shellSort(array,size); 38 | clock_t end = clock(); 39 | double time_spent = (double)(end - start) / CLOCKS_PER_SEC; 40 | 41 | 42 | printf("Data Sorted\n"); 43 | printf("%s\n", "Shell Sort Big O Notation:\n--> Best Case: O(n log(n))\n--> Average Case: depends on gap sequence\n--> Worst Case: O(n)\n"); 44 | printf("Time spent sorting: %f\n", time_spent); 45 | 46 | return 0; 47 | } 48 | 49 | -------------------------------------------------------------------------------- /conversions/binary2octal.c: -------------------------------------------------------------------------------- 1 | // Binary number to octal number conversion 2 | #include 3 | 4 | //Function that returns the last three digits 5 | int three_digits(int n) 6 | { 7 | int r, d = 0, p=1; 8 | 9 | for(int i=0; i<3; i++) 10 | { 11 | r = n%10; 12 | d += r * p; 13 | p *= 10; 14 | n /= 10; 15 | } 16 | return d; 17 | } 18 | 19 | int main(void) 20 | { 21 | int binary_num, d=0, base=1, remainder, td, res=0, ord=1; 22 | 23 | printf("Enter the binary no: "); 24 | scanf("%d", &binary_num); 25 | 26 | while(binary_num > 0) 27 | { 28 | if(binary_num > 111) //Checking if binary number is greater than three digits 29 | td = three_digits(binary_num); 30 | 31 | else td = binary_num; 32 | 33 | binary_num /= 1000; 34 | 35 | d = 0, base =1; 36 | 37 | // Converting the last three digits to decimal 38 | while(td > 0) 39 | { 40 | remainder = td % 10; 41 | td /= 10; 42 | d += (base * remainder); 43 | base *= 2; 44 | } 45 | 46 | res += d * ord; // Calculating the octal value 47 | ord *= 10; 48 | } 49 | 50 | printf("\nOctal equivalent is: %d", res); 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /exercism/isogram/isogram.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* 5 | is_isogram: returns true if the given string a isogram, otherwise false. 6 | */ 7 | bool is_isogram(const char phrase[]) 8 | { 9 | 10 | /* use 'unsigned' because of the function strlen(...) */ 11 | unsigned int i = 0; 12 | unsigned int j = 0; 13 | 14 | /* the current read character in the first for-loop */ 15 | char current_char = ' '; 16 | 17 | /* return status */ 18 | bool status = true; 19 | 20 | /* contains the length of the given string */ 21 | unsigned int len_phrase = strlen(phrase); 22 | 23 | for (i = 0; i < len_phrase; i++ ) 24 | { 25 | current_char = phrase[i]; 26 | 27 | /* makes sure the current character has no repetition */ 28 | for (j = i+1; j < len_phrase; j++) 29 | { 30 | if (current_char == phrase[j]) 31 | { 32 | status = false; 33 | 34 | /* 35 | because the given string is none isogram. 36 | that means we can exit the nested for-loop. 37 | */ 38 | goto end; 39 | } 40 | } 41 | } 42 | 43 | /* exit label */ 44 | end: 45 | return status; 46 | } -------------------------------------------------------------------------------- /Hash/hash.c: -------------------------------------------------------------------------------- 1 | /* 2 | author: Christian Bender 3 | This is the implementation unit of the hash-functions. 4 | 5 | Overview about hash-functions: 6 | 7 | - sdbm 8 | - djb2 9 | - xor8 (8 bits) 10 | - adler_32 (32 bits) 11 | */ 12 | 13 | long long sdbm(char s[]) 14 | { 15 | long long hash = 0; 16 | int i = 0; 17 | while (s[i] != '\0') 18 | { 19 | hash = s[i] + (hash << 6) + (hash << 16) - hash; 20 | i++; 21 | } 22 | return hash; 23 | } 24 | 25 | long long djb2(char s[]) 26 | { 27 | long long hash = 5381; /* init value */ 28 | int i = 0; 29 | while (s[i] != '\0') 30 | { 31 | hash = ((hash << 5) + hash) + s[i]; 32 | i++; 33 | } 34 | return hash; 35 | } 36 | 37 | char xor8(char s[]) 38 | { 39 | int hash = 0; 40 | int i = 0; 41 | while (s[i] != '\0') 42 | { 43 | hash = (hash + s[i]) & 0xff; 44 | i++; 45 | } 46 | return (((hash ^ 0xff) + 1) & 0xff); 47 | } 48 | 49 | int adler_32(char s[]) 50 | { 51 | int a = 1; 52 | int b = 0; 53 | const int MODADLER = 65521; 54 | 55 | int i = 0; 56 | while (s[i] != '\0') 57 | { 58 | a = (a + s[i]) % MODADLER; 59 | b = (b + a) % MODADLER; 60 | i++; 61 | } 62 | return (b << 16) | a; 63 | } -------------------------------------------------------------------------------- /Data Structures/dictionary/test_program.c: -------------------------------------------------------------------------------- 1 | /* 2 | author: Christian Bender 3 | This is a simple test program for the dictionary. 4 | */ 5 | 6 | #include 7 | 8 | /* includes the dictionary */ 9 | #include "dict.h" 10 | 11 | int main(void) 12 | { 13 | Dictionary * testObj1; 14 | Dictionary * testObj2; 15 | 16 | int value = 28; 17 | 18 | testObj1 = create_dict(); 19 | testObj2 = create_dict(); 20 | 21 | add_item_label(testObj1,"age",&value); 22 | add_item_label(testObj2,"name","Christian"); 23 | 24 | 25 | /* 26 | test for function add_item_label 27 | 28 | attention: 29 | The void* pointer must be convert into an int* pointer. 30 | After that you can dereference it. 31 | */ 32 | printf("My age is %d\n",*((int *)get_element_label(testObj1,"age"))); 33 | printf("My name is %s\n",get_element_label(testObj2,"name")); 34 | 35 | /* test for function add_item_index */ 36 | if (!add_item_index(testObj1,0,&value)) 37 | { 38 | printf("My age at index %d is %d\n",0,*((int *)get_element_index(testObj1,0))); 39 | } 40 | 41 | /* error scenario */ 42 | /* get_element_label(testObj,"none"); */ 43 | 44 | /* tidy up */ 45 | destroy(testObj1); 46 | destroy(testObj2); 47 | 48 | return 0; 49 | } -------------------------------------------------------------------------------- /misc/rselect.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | void swap(int *a ,int *b) 5 | {int t;t =*a;*a=*b;*b=t;} 6 | int part(int a[],int l,int r,int n,int pivot,int pindex) 7 | {int p1=l,p2=r; 8 | while(p2>p1) 9 | { 10 | if (a[p1] > pivot && a[p2]=pivot) 17 | {p2--;} 18 | } 19 | } 20 | swap(&a[pindex],&a[p2]); 21 | return p2; 22 | } 23 | int rselect(int a[],int l,int r,int n,int o) 24 | { 25 | int pivot,pindex,pactual; 26 | if (r>l) 27 | { 28 | pindex = rand()%(r-l+1); 29 | pivot = a[pindex]; 30 | pactual = part(a,l,r,n,pivot,pindex); 31 | 32 | if (pactual == o) 33 | {return a[pactual];} 34 | 35 | if (o < pactual) 36 | {rselect(a,l,pactual-1,n,o);} 37 | 38 | if (o>pactual) 39 | {rselect(a,pactual+1,r,n,o-pactual);} 40 | } 41 | if (r==l) 42 | {return a[l];} 43 | } 44 | int main() 45 | {srand(time(NULL)); 46 | int n,o,i,*a; 47 | scanf("%d %d",&n,&o); 48 | a = (int*)malloc(n*sizeof(int)); 49 | for (i=0;i 2 | #include 3 | 4 | #define MAXBITS 100 5 | 6 | int main() 7 | { 8 | 9 | // input of the user 10 | int inputNumber; 11 | 12 | // for the remainder 13 | int re; 14 | 15 | // contains the bits 0/1 16 | int bits[MAXBITS]; 17 | 18 | // for the loops 19 | int j; 20 | int i=0; 21 | 22 | printf("\t\tConverter decimal --> binary\n\n"); 23 | 24 | // reads a decimal number from the user. 25 | printf("\nenter a positive integer number: "); 26 | scanf("%d",&inputNumber); 27 | 28 | // make sure the input number is a positive integer. 29 | if (inputNumber < 0) 30 | { 31 | printf("only positive integers >= 0\n"); 32 | return 1; 33 | } 34 | 35 | // actual processing 36 | while(inputNumber>0) 37 | { 38 | 39 | // computes the remainder by modulo 2 40 | re = inputNumber % 2; 41 | 42 | // computes the quotient of division by 2 43 | inputNumber = inputNumber / 2; 44 | 45 | bits[i] = re; 46 | i++; 47 | 48 | } 49 | 50 | printf("\n the number in binary is: "); 51 | 52 | // iterates backwards over all bits 53 | for(j=i-1; j>=0; j--) 54 | { 55 | printf("%d",bits[j]); 56 | } 57 | 58 | // for the case the input number is 0 59 | if (i == 0) 60 | { 61 | printf("0"); 62 | } 63 | 64 | return 0; 65 | } 66 | 67 | -------------------------------------------------------------------------------- /conversions/decimal _to_binary.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAXBITS 100 5 | 6 | int main() 7 | { 8 | 9 | // input of the user 10 | int inputNumber; 11 | 12 | // for the remainder 13 | int re; 14 | 15 | // contains the bits 0/1 16 | int bits[MAXBITS]; 17 | 18 | // for the loops 19 | int j; 20 | int i=0; 21 | 22 | printf("\t\tConverter decimal --> binary\n\n"); 23 | 24 | // reads a decimal number from the user. 25 | printf("\nenter a positive integer number: "); 26 | scanf("%d",&inputNumber); 27 | 28 | // make sure the input number is a positive integer. 29 | if (inputNumber < 0) 30 | { 31 | printf("only positive integers >= 0\n"); 32 | return 1; 33 | } 34 | 35 | // actual processing 36 | while(inputNumber>0) 37 | { 38 | 39 | // computes the remainder by modulo 2 40 | re = inputNumber % 2; 41 | 42 | // computes the quotient of division by 2 43 | inputNumber = inputNumber / 2; 44 | 45 | bits[i] = re; 46 | i++; 47 | 48 | } 49 | 50 | printf("\n the number in binary is: "); 51 | 52 | // iterates backwards over all bits 53 | for(j=i-1; j>=0; j--) 54 | { 55 | printf("%d",bits[j]); 56 | } 57 | 58 | // for the case the input number is 0 59 | if (i == 0) 60 | { 61 | printf("0"); 62 | } 63 | 64 | return 0; 65 | } 66 | 67 | -------------------------------------------------------------------------------- /Sorts/BubbleSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main(){ 6 | 7 | int* ARRAY=NULL; 8 | int ContinueFilling=1; //This is to know if we should continue filling our array 9 | int ARRAY_LENGTH=0,isSorted=0,i,TEMPORARY_ELEMENT; 10 | 11 | //This code part is for filling our array 12 | while(ContinueFilling){ 13 | printf("Enter the value number %d \n",ARRAY_LENGTH+1); 14 | ARRAY=(int *)realloc(ARRAY,sizeof(int)*(ARRAY_LENGTH)); 15 | scanf("%d",&ARRAY[ARRAY_LENGTH]); 16 | ARRAY_LENGTH+=1; 17 | printf("would you enter an other value (1:Continue/0:Sort the actual array)?\n"); 18 | scanf("%d",&ContinueFilling); 19 | } 20 | 21 | //Then we sort it using Bubble Sort.. 22 | 23 | while(!isSorted){ //While our array's not sorted 24 | isSorted=1; //we suppose that it's sorted 25 | for(i=0;iARRAY[i+1]){ // if the two elements aren't sorted 27 | isSorted=0; //it means that the array is not sorted 28 | TEMPORARY_ELEMENT=ARRAY[i]; //and we switch these elements using TEMPORARY_ELEMENT 29 | ARRAY[i]=ARRAY[i+1]; 30 | ARRAY[i+1]=TEMPORARY_ELEMENT; 31 | } 32 | } 33 | } 34 | //And we display it 35 | for(i=0;i 6 | 7 | int binarySearch(int a[], int item, int low, int high) 8 | { 9 | if (high <= low) 10 | return (item > a[low])? (low + 1): low; 11 | 12 | int mid = (low + high)/2; 13 | 14 | if(item == a[mid]) 15 | return mid+1; 16 | 17 | if(item > a[mid]) 18 | return binarySearch(a, item, mid+1, high); 19 | return binarySearch(a, item, low, mid-1); 20 | } 21 | 22 | // Function to sort an array a[] of size 'n' 23 | void insertionSort(int a[], int n) 24 | { 25 | int i, loc, j, k, selected; 26 | 27 | for (i = 1; i < n; ++i) 28 | { 29 | j = i - 1; 30 | selected = a[i]; 31 | 32 | // find location where selected sould be inseretd 33 | loc = binarySearch(a, selected, 0, j); 34 | 35 | // Move all elements after location to create space 36 | while (j >= loc) 37 | { 38 | a[j+1] = a[j]; 39 | j--; 40 | } 41 | a[j+1] = selected; 42 | } 43 | } 44 | 45 | int main() 46 | { 47 | int n; 48 | scanf("%d",&n) ; 49 | int a[n],i; 50 | for(i = 0; i 2 | #incude 3 | #define MAX 20 4 | 5 | //i and j act as counters 6 | //arraySort is the array that is to be sorted 7 | //elmtToInsert will be the element that we will be trying to move to its correct index in the current iteration 8 | 9 | int main() 10 | { 11 | int i, elmtToInsert , j , arraySort[MAX] = {0}; 12 | 13 | for(i = 1 ; i < MAX ; i++) //This array is being sorted in the ascending order. 14 | { 15 | elmtToInsert = arraySort[i]; //Pick up the ith indexed element of the array. It will be the elmtToInsert. 16 | j = i - 1 ; 17 | 18 | while(j >= 0 && elmtToInsert < arraySort[j]) /*compare it with each (i-1)th, (i-2)th... max 0th element, till the correct 19 | position of the elmtToInsert, where it is finally greater than the element just 20 | before it, is found */ 21 | { 22 | // You'll enter the loop if the elmtToInsert is less than the element just before it. 23 | 24 | arraySort[j+1] = arraySort[j]; //shift the current element one place forward to create room for insertion of the elmtToInsert 25 | j--; 26 | } 27 | //when we exit the loop, j+1 will be the index of the correct position of the elmtToInsert 28 | 29 | arraySort[j+1] = elmtToInsert ; //'insert' the elmtToInsert into its correct position 30 | 31 | } 32 | 33 | 34 | return EXIT_SUCCESS; 35 | } 36 | -------------------------------------------------------------------------------- /misc/mirror: -------------------------------------------------------------------------------- 1 | #include 2 | #include // we include the library string.h to the use of string 3 | 4 | void saisie (char *cpointeur); // Prototypes of the three functions used in the program 5 | int compte (char *s); 6 | char* miroir (char * s); 7 | 8 | int main (int argc , char *argv[]) 9 | { 10 | char chaine[20]; 11 | saisie(chaine); 12 | printf("miroir est %s",miroir(chaine)); 13 | 14 | } 15 | // this function is used to put a string 16 | void saisie (char *cpointeur) 17 | { 18 | printf("saisir une chaine\n"); 19 | scanf("%s",cpointeur); 20 | } 21 | /* the function miroir (in french ) it means "mirror" , the major idea is to permute the first caractere with the last using an auxilary 22 | variable (aux) the the 2nd character with the penultimate one and so on . 23 | we made a call to the function (compte) which counts the length of the string . As you can see clearly , I substruct 1 from the equation 24 | k = compte(s)-1 ; to get rid of the EOF caractere which is '\0' because it is not a caractere from the string typed */ 25 | char* miroir (char *s) 26 | { 27 | int i ; 28 | char aux ; 29 | int k ; 30 | k = compte(s)-1 ; 31 | i = 0 ; 32 | while(i<=k) 33 | { 34 | aux = s[i]; 35 | s[i]=s[k]; 36 | s[k]=aux ; 37 | k-- ; 38 | i++ ; 39 | } 40 | 41 | return s ; 42 | } 43 | 44 | // compte plays the role of strlen so we can change it by an strlen function if you want that 45 | int compte (char *s) 46 | { 47 | char *p ; 48 | int k ; 49 | p=s ; 50 | k=0 ; 51 | while(*p!='\0') 52 | { 53 | p++ ; 54 | k++ ; 55 | } 56 | return k ; 57 | } 58 | -------------------------------------------------------------------------------- /Sorts/mergesort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void swap (int *a,int *b) 4 | { 5 | int t; 6 | t= *a; 7 | *a=*b; 8 | *b=t; 9 | 10 | } 11 | 12 | void merge(int a[],int l,int r,int n) 13 | { int *b = (int*)malloc(n*sizeof(int)); 14 | int c=l; 15 | int p1,p2; 16 | p1 = l;p2=((l+r)/2)+1; 17 | while ((p1<((l+r)/2)+1) &&(p2a[r]) 54 | swap(&a[l],&a[r]); 55 | 56 | } 57 | else if(l==r) 58 | {} 59 | else 60 | {mergesort(a,n,l,(l+r)/2); 61 | mergesort(a,n,((l+r)/2)+1,r); 62 | merge(a,l,r,n); 63 | 64 | } 65 | 66 | } 67 | int main(void) { 68 | int *a,n,i; 69 | scanf("%d",&n); 70 | a = (int*)malloc(n*sizeof(int)); 71 | for (i=0;i 2 | #include 3 | #include 4 | 5 | void swap(char* left, char* right){ 6 | char temp = *left; 7 | *left = *right; 8 | *right = temp; 9 | } 10 | 11 | int compare (const void * a, const void * b){ 12 | return ( *(char*)a - *(char*)b ); 13 | } 14 | 15 | void PrintSortedPermutations(char str[]) 16 | { 17 | int strSize = strlen(str); 18 | qsort(str, strSize, sizeof(char), compare); 19 | 20 | int largerPermFound = 1; 21 | do{ 22 | // 1. Print permutation 23 | printf("%s\n", str); 24 | // 2. Find rightmost char that is smaller than char to its right 25 | int i; 26 | for (i = strSize - 2; i >= 0 && str[i] >= str[i+1]; --i){} 27 | 28 | // if we couldn't find one, we're finished, else we can swap 29 | if (i >= 0){ 30 | // 3. find character at index j such that str[j] = min(str[k]) && str[k] > str[i] for all k > i 31 | int j = i+1, k; 32 | for(k=j; k str[i] && str[k] < str[j]) 34 | j = k; 35 | } 36 | // 3. Swap chars at i and j 37 | swap(&str[i], &str[j]); 38 | // 4. Sort string to the right of i 39 | qsort(str+i+1, strSize-i-1, sizeof(char), compare); 40 | } 41 | else largerPermFound = 0; 42 | } 43 | while(largerPermFound); 44 | } 45 | 46 | int main() { 47 | int n; //size of string 48 | scanf("%d\n",&n); 49 | char str[n]; 50 | scanf("%s",str); 51 | PrintSortedPermutations(str); 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /Sorts/HeapSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void heapify(int *unsorted, int index, int heap_size); 4 | void heap_sort(int *unsorted, int n); 5 | 6 | int main() { 7 | int n = 0; 8 | int i = 0; 9 | char oper; 10 | 11 | int* unsorted; 12 | printf("Enter the size of the array you want\n"); 13 | scanf("%d", &n); 14 | unsorted = (int*)malloc(sizeof(int) * n); 15 | while (getchar() != '\n'); 16 | printf("Enter numbers separated by a comma:\n"); 17 | while (i != n) { 18 | scanf("%d,", (unsorted + i)); 19 | i++; 20 | } 21 | heap_sort(unsorted, n); 22 | 23 | printf("["); 24 | printf("%d", *(unsorted)); 25 | for (int i = 1; i < n; i++) { 26 | printf(", %d", *(unsorted + i)); 27 | } 28 | printf("]"); 29 | } 30 | 31 | void heapify(int *unsorted, int index, int heap_size) { 32 | int temp; 33 | int largest = index; 34 | int left_index = 2 * index; 35 | int right_index = 2 * index + 1; 36 | if (left_index < heap_size && *(unsorted + left_index) > *(unsorted + largest)) { 37 | largest = left_index; 38 | } 39 | if (right_index < heap_size && *(unsorted + right_index) > *(unsorted + largest)) { 40 | largest = right_index; 41 | } 42 | 43 | if (largest != index) { 44 | temp = *(unsorted + largest); 45 | *(unsorted + largest) = *(unsorted + index); 46 | *(unsorted + index) = temp; 47 | heapify(unsorted, largest, heap_size); 48 | } 49 | } 50 | 51 | void heap_sort(int *unsorted, int n) { 52 | int temp; 53 | for (int i = n / 2 - 1; i > -1; i--) { 54 | heapify(unsorted, i, n); 55 | } 56 | for (int i = n - 1; i > 0; i--) { 57 | temp = *(unsorted); 58 | *(unsorted) = *(unsorted + i); 59 | *(unsorted + i) = temp; 60 | heapify(unsorted, 0, i); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Data Structures/dictionary/dict.h: -------------------------------------------------------------------------------- 1 | /* 2 | author: Christian Bender 3 | public interface for the dictionary. 4 | 5 | The dictionary prepares space for 1000 elements. 6 | */ 7 | 8 | #ifndef __DICT__H 9 | #define __DICT__H 10 | 11 | #define MAXELEMENTS 1000 12 | 13 | /* 14 | special data type called 'Dictionary' 15 | for generic use 16 | */ 17 | typedef struct Dict 18 | { 19 | /* 20 | void* array for generic use of the dictionary. 21 | there actual saves the entries. 22 | */ 23 | void * elements[MAXELEMENTS]; 24 | 25 | /* contains the number of elements in this dictionary */ 26 | int number_of_elements; 27 | 28 | } Dictionary; 29 | 30 | /* 31 | create_dict: is a simple constructor for creating 32 | a dictionary and setting up the 33 | member field 'number_of_elements' 34 | and prepares the inner array 'elements' 35 | */ 36 | Dictionary * create_dict(void); 37 | 38 | /* 39 | add_item_label: adds item (void*) to the dictionary at given label 40 | returns 0 if adding was sucessful otherwise -1 41 | */ 42 | int add_item_label(Dictionary *,char label[],void *); 43 | 44 | /* 45 | add_item_index: adds item (void*) to the dictionary at given index (int) 46 | returns 0 if adding was sucessful otherwise -1 47 | */ 48 | int add_item_index(Dictionary *, int index, void *); 49 | 50 | 51 | /* 52 | get_element: returns the element at given label 53 | */ 54 | void * get_element_label(Dictionary *, char []); 55 | 56 | 57 | /* 58 | get_element: returns the element at given index 59 | */ 60 | void * get_element_index(Dictionary *, int ); 61 | 62 | /* 63 | simple destrcutor function 64 | */ 65 | void destroy(Dictionary *); 66 | 67 | 68 | #endif -------------------------------------------------------------------------------- /Data Structures/linked_list/stack_using_linkedlists.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node 4 | { 5 | int info; 6 | struct node *link; 7 | }; 8 | struct node *top=NULL,*temp; 9 | void push(struct node*); 10 | void pop(struct node*); 11 | void display(struct node*); 12 | 13 | int main() 14 | { 15 | int x=0,item; 16 | printf("\t****stack using linked list****\n"); 17 | while(x!=4) 18 | { 19 | printf("enter your choice"); 20 | printf("\n1.push\n2.pop\n3.display\n4.exit\n"); 21 | scanf("%d",&x); 22 | switch(x) 23 | { 24 | case 1: 25 | push(top); 26 | break; 27 | case 2: pop(top); 28 | break; 29 | case 3: display(top); 30 | break; 31 | case 4: return 0; 32 | 33 | } 34 | } 35 | 36 | } 37 | 38 | void push(struct node *p) 39 | { 40 | int item; 41 | struct node *temp; 42 | temp=(struct node *)malloc(sizeof(struct node)); 43 | printf("enter element to be inserted\n"); 44 | scanf("%d",&item); 45 | temp->info=item; 46 | 47 | 48 | 49 | temp->link=top; 50 | top=temp; 51 | 52 | 53 | 54 | printf("inserted succesfully\n"); 55 | } 56 | void pop(struct node *p) 57 | { 58 | int item; 59 | struct node *temp; 60 | 61 | if(top==NULL) 62 | printf("stack is empty\n"); 63 | else{ 64 | item=top->info; 65 | temp=top; 66 | top=top->link; 67 | free(temp); 68 | printf("Element popped is%d\n",item); 69 | } 70 | 71 | } 72 | 73 | 74 | void display(struct node *p) 75 | { 76 | 77 | if(top==NULL) 78 | printf("stack is empty\n"); 79 | else 80 | { printf("Elements in the stack are\n"); 81 | while(p!=NULL) 82 | { 83 | printf("%d\n",p->info); 84 | p=p->link; 85 | } 86 | // printf("%d\n",p->info); 87 | 88 | } 89 | 90 | } 91 | 92 | 93 | -------------------------------------------------------------------------------- /computer-oriented-statistical-methods/variance.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | 7 | int *ARRAY=NULL,ARRAY_LENGTH,i,TEMPORARY_ELEMENT,isSorted=0; 8 | float MEAN=0,VARIANCE=0,STAND; 9 | 10 | 11 | printf("Enter no. for Random Numbers :"); 12 | scanf("%d",&ARRAY_LENGTH); 13 | ARRAY=(int *)realloc(ARRAY,ARRAY_LENGTH*(sizeof(int))); //We allocate the dedicated memory 14 | for(i=0;iARRAY[i+1]){ // if the two elements aren't sorted 27 | isSorted=0; //it means that the array is not sorted 28 | TEMPORARY_ELEMENT=ARRAY[i]; //and we switch these elements using TEMPORARY_ELEMENT 29 | ARRAY[i]=ARRAY[i+1]; 30 | ARRAY[i+1]=TEMPORARY_ELEMENT; 31 | } 32 | } 33 | } 34 | for(i=0;i 3 | 4 | void printArr(int *A, int size) 5 | { 6 | for (int i = 0; i < size; i++) 7 | { 8 | printf("%d ", A[i]); 9 | } 10 | printf("\n"); 11 | } 12 | 13 | void swap(int *a, int *b) 14 | { 15 | int temp = *a; 16 | *a = *b; 17 | *b = temp; 18 | } 19 | 20 | // heap looks like 21 | // 9 1 2 8 3 4 7 5 22 | // 9 8 4 7 23 | // 9 7 24 | // 9 25 | // the array is heap mean arr[i]>max(arr[i*2+1],arr[i*2+2]) 26 | // the arr[0] is max of the array 27 | void heapShift(int *arr, int index, int size) 28 | { 29 | // put arr[index] to arr[index+1..size-1] follow heap 30 | int i = index; 31 | int j = 2 * i + 1; 32 | while (j < size) 33 | { 34 | if (j != size - 1) // there exist j and j+1 35 | { 36 | if (arr[j + 1] > arr[j]) // find max of arr[j] and arr[j+1] 37 | j++; 38 | } 39 | if (arr[i] > arr[j]) // already heap 40 | break; 41 | swap(&arr[i], &arr[j]); 42 | // move i, j 43 | i = j; 44 | j = i * 2 + 1; 45 | } 46 | } 47 | 48 | void heapSort(int *arr, int size) 49 | { 50 | // the array is split to to half 51 | // just care the first half to put heap 52 | for (int i = size / 2; i >= 0; i--) 53 | { 54 | heapShift(arr, i, size); 55 | } 56 | for (int i = size - 1; i >= 0; i--) 57 | { 58 | swap(&arr[0], &arr[i]); // move max of array to the end 59 | heapShift(arr, 0, i); // find max of arr[0..i-1], move to arr[0] 60 | } 61 | } 62 | 63 | int main() 64 | { 65 | int A[] = {9, 8, 7, 6, 5, 4, 3, 2, 1}; 66 | int size = sizeof(A) / sizeof(A[0]); 67 | heapSort(A, size); 68 | printArr(A, size); 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /Sorts/SelectionSort.c: -------------------------------------------------------------------------------- 1 | //sorting of linked list using selection sort 2 | #include 3 | struct node 4 | { 5 | int info; 6 | struct node *link; 7 | }; 8 | struct node *start=NULL; 9 | //func to create node 10 | struct node *createnode() 11 | { 12 | struct node *p; 13 | p=(struct node*)malloc(sizeof(struct node)); 14 | return(p); 15 | } 16 | //program to insert at begining 17 | void insert() 18 | {struct node *t; 19 | t=createnode(); 20 | printf("\nenter the value to insert"); 21 | scanf("%d",&t->info); 22 | if(start==NULL) 23 | {start=t; 24 | } 25 | else 26 | {strutc node *p; 27 | p=start; 28 | t->link=p; 29 | start=t; 30 | } 31 | //program to sort the linked list using selection sort 32 | void sort() 33 | { 34 | struct node *p,*t; 35 | t=start; 36 | int tmp; 37 | for(t=start;t->link!=NULL;t=t->link) 38 | { 39 | for(p=t->link;p!=NULL;p=p->link) 40 | { 41 | if(t->info>p->info) 42 | tmp=t->info; 43 | t->info=p->info; 44 | p->info=tmp; 45 | } 46 | } 47 | //program to view sorted list 48 | void viewlist() 49 | { 50 | struct node *p; 51 | if(start==NULL) 52 | { 53 | printf("\nlist is empty"); 54 | } 55 | else 56 | { 57 | p=start; 58 | while(p!=NULL) 59 | { 60 | printf("%d",p->info); 61 | p=p->link; 62 | } 63 | } 64 | int main() 65 | { 66 | int n; 67 | whhile(1) 68 | { 69 | printf("\n1.insert value at beg"); 70 | printf("\n2.sort the list"); 71 | printf("\n3.view value"); 72 | printf("\nenter your choice"); 73 | scanf("%d",&n); 74 | switch(n) 75 | {case 1: 76 | insert(); 77 | break; 78 | case 2: 79 | sort(); 80 | break; 81 | case 3: 82 | viewlist(); 83 | break; 84 | default: 85 | printf("\ninvalid choice"); 86 | } 87 | } 88 | return(0); 89 | } 90 | -------------------------------------------------------------------------------- /exercism/acronym/acronym.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | char *abbreviate(const char *phrase) 6 | { 7 | char str[80]; 8 | strcpy(str, phrase); 9 | char *p_str = str; 10 | static char acr[80]; 11 | strcpy(acr, ""); 12 | 13 | /* for counting the words */ 14 | int counter = 0; 15 | 16 | /* for position the words */ 17 | int index = 0; 18 | 19 | /* for -loop variable */ 20 | int i = 0; 21 | 22 | /* 23 | counts the empty-characters. 24 | for determine the number of words 25 | */ 26 | while (p_str && (i < 80)) 27 | { 28 | if (*p_str == ' ') 29 | { 30 | counter++; 31 | } 32 | if (i < 80) 33 | { 34 | p_str++; 35 | i++; 36 | } 37 | } 38 | 39 | i = 0; 40 | counter++; 41 | char words[counter][80]; 42 | 43 | /* initalizes words-array with empty strings */ 44 | for (i = 0; i < counter; i++) 45 | { 46 | strcpy(words[i],""); 47 | } 48 | 49 | /* rewind string */ 50 | p_str = str; 51 | 52 | char *p_start = p_str; 53 | 54 | /* collects each word in array 'words' */ 55 | while (p_str && (i <= 80)) 56 | { 57 | if (*p_str == ' ') 58 | { 59 | *p_str = '\0'; 60 | strncat(words[index], p_start, 80); 61 | index++; 62 | p_start = p_str + 1; 63 | } 64 | 65 | if (i <= 80) 66 | { 67 | p_str++; 68 | i++; 69 | } 70 | } 71 | 72 | /* adds the last word */ 73 | *p_str = '\0'; 74 | strncat(words[index], p_start, 80); 75 | index++; 76 | 77 | /* builds the actual acronym */ 78 | for (i = 0; i < index; i++) 79 | { 80 | /* capitalize the first character */ 81 | words[i][0] = toupper(words[i][0]); 82 | words[i][1] = '\0'; 83 | strcat(acr, words[i]); 84 | } 85 | 86 | return acr; 87 | } -------------------------------------------------------------------------------- /misc/sudokusolver.c: -------------------------------------------------------------------------------- 1 | //recursion problem : Sudoku Solver 2 | /*You are given an incomplete N*N Sudoku and asked to solve it using the following recursive algorithm: 3 | (1) Scan the Sudoku from left to right row-wise to search for an empty cell. 4 | (2) If there are no empty cells, print the Sudoku. Go to step 5. 5 | (3) In the empty cell, try putting numbers 1 to N while ensuring that no two numbers in a single row, column, or box are same. Go back to step 1. 6 | (4) Declare that the Sudoku is Invalid. 7 | (5) Exit.*/ 8 | 9 | #include 10 | 11 | const int M=144; 12 | int N, R, C; 13 | 14 | int OKrow(int a[M], int x, int y, int v) { 15 | int j; 16 | for(j=0; j 2 | #include 3 | 4 | int fibMonaccianSearch(int arr[], int x, int n) 5 | { 6 | /* Initialize fibonacci numbers */ 7 | int fibMMm2 = 0; // (m-2)'th Fibonacci No. 8 | int fibMMm1 = 1; // (m-1)'th Fibonacci No. 9 | int fibM = fibMMm2 + fibMMm1; // m'th Fibonacci 10 | 11 | /* fibM is going to store the smallest Fibonacci 12 | Number greater than or equal to n */ 13 | while (fibM < n) 14 | { 15 | fibMMm2 = fibMMm1; 16 | fibMMm1 = fibM; 17 | fibM = fibMMm2 + fibMMm1; 18 | } 19 | 20 | // Marks the eliminated range from front 21 | int offset = -1; 22 | 23 | /* while there are elements to be inspected. Note that 24 | we compare arr[fibMm2] with x. When fibM becomes 1, 25 | fibMm2 becomes 0 */ 26 | while (fibM > 1) 27 | { 28 | // Check if fibMm2 is a valid location 29 | 30 | // sets i to the min. of (offset+fibMMm2) and (n-1) 31 | int i = ((offset+fibMMm2) < (n-1)) ? (offset+fibMMm2) : (n-1); 32 | 33 | /* If x is greater than the value at index fibMm2, 34 | cut the subarray array from offset to i */ 35 | if (arr[i] < x) 36 | { 37 | fibM = fibMMm1; 38 | fibMMm1 = fibMMm2; 39 | fibMMm2 = fibM - fibMMm1; 40 | offset = i; 41 | } 42 | 43 | /* If x is greater than the value at index fibMm2, 44 | cut the subarray after i+1 */ 45 | else if (arr[i] > x) 46 | { 47 | fibM = fibMMm2; 48 | fibMMm1 = fibMMm1 - fibMMm2; 49 | fibMMm2 = fibM - fibMMm1; 50 | } 51 | 52 | /* element found. return index */ 53 | else return i; 54 | } 55 | 56 | /* comparing the last element with x */ 57 | if(fibMMm1 && arr[offset+1]==x)return offset+1; 58 | 59 | /*element not found. return -1 */ 60 | return -1; 61 | } 62 | 63 | 64 | int main(void) 65 | { 66 | int arr[] = {10, 22, 35, 40, 45, 50, 80, 82, 67 | 85, 90, 100}; 68 | int n = sizeof(arr)/sizeof(arr[0]); 69 | int x = 85; 70 | printf("Found at index: %d", 71 | fibMonaccianSearch(arr, x, n)); 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /Data Structures/stack/balanced parenthesis using stack in C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define size 100 5 | 6 | struct node 7 | { 8 | char data; 9 | struct node* link; 10 | }; 11 | int c=0; // c used as counter to check if stack is empty or not 12 | struct node * head; //declaring head pointer globally assigned to NULL 13 | 14 | void push(char x) //function for pushing 15 | { 16 | struct node *p,*temp; 17 | temp=(struct node*)malloc(sizeof(struct node)); 18 | temp->data=x; 19 | if(head==NULL) //will be execute only one time i.e, 1st time push is called 20 | { 21 | head=temp; 22 | p=head; 23 | p->link=NULL; 24 | c++; 25 | } 26 | else 27 | { 28 | temp->link=p; 29 | p=temp; 30 | head=p; 31 | c++; 32 | } 33 | 34 | } 35 | char pop(void) //function for pop 36 | { 37 | char x; 38 | struct node*p=head; 39 | x=p->data; 40 | head=p->link; 41 | free(p); 42 | c--; 43 | return x; 44 | 45 | } 46 | 47 | int isBalanced(char *s) { 48 | int i=0;char x; 49 | while(s[i]!='\0') //loop for covering entire string of brackets 50 | { 51 | if(s[i]=='{'||s[i]=='('||s[i]=='[') //if opening bracket then push 52 | push(s[i]); 53 | else 54 | { 55 | if(c<=0) //i.e, stack is empty as only opening brackets are added to stack 56 | return 0; 57 | 58 | 59 | x=pop(); 60 | if( x=='{'&&s[i]!='}') 61 | return 0; 62 | if(x=='['&&s[i]!=']') 63 | return 0; 64 | if(x=='('&&s[i]!=')') 65 | return 0 ; 66 | }i++; 67 | } 68 | if(c==0) //at end if stack is empy which means whole process has been performed correctly so retuen 1 69 | return 1; 70 | else 71 | return 0; 72 | } 73 | 74 | int main() { 75 | int t; 76 | scanf("%d",&t); 77 | for(int a0 = 0; a0 < t; a0++){ 78 | char s[size]; 79 | int result; 80 | scanf("%s",s); 81 | result = isBalanced(s); 82 | if(result==1) 83 | printf("\nYES"); 84 | else 85 | printf("\nNO"); 86 | 87 | } 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /Project Euler/README.md: -------------------------------------------------------------------------------- 1 | # ProjectEuler 2 | 3 | Problems are taken from https://projecteuler.net/. 4 | 5 | Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical 6 | insights to solve. Project Euler is ideal for mathematicians who are learning to code. 7 | 8 | Here the efficiency of your code is also checked. 9 | I've tried to provide all the best possible solutions. 10 | 11 | PROBLEMS: 12 | 13 | 1. If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3,5,6 and 9. The sum of these multiples is 23. 14 | Find the sum of all the multiples of 3 or 5 below N. 15 | 16 | 2. Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, 17 | the first 10 terms will be: 18 | 1,2,3,5,8,13,21,34,55,89,.. 19 | By considering the terms in the Fibonacci sequence whose values do not exceed n, find the sum of the even-valued terms. 20 | e.g. for n=10, we have {2,8}, sum is 10. 21 | 22 | 3. The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor of a given number N? 23 | e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17. 24 | 25 | 4. A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. 26 | Find the largest palindrome made from the product of two 3-digit numbers which is less than N. 27 | 28 | 5. 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. 29 | What is the smallest positive number that is evenly divisible(divisible with no remainder) by all of the numbers from 1 to N? 30 | 31 | 6. The sum of the squares of the first ten natural numbers is, 32 | 1^2 + 2^2 + ... + 10^2 = 385 33 | The square of the sum of the first ten natural numbers is, 34 | (1 + 2 + ... + 10)^2 = 552 = 3025 35 | Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640. 36 | Find the difference between the sum of the squares of the first N natural numbers and the square of the sum. 37 | 38 | 7. By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. 39 | What is the Nth prime number? 40 | -------------------------------------------------------------------------------- /Data Structures/stack/main.c: -------------------------------------------------------------------------------- 1 | //program for stack using array 2 | 3 | #include 4 | void push(); 5 | void pop(); 6 | void peep(); 7 | void update(); 8 | int main() 9 | { 10 | int n,a[100],top=0; 11 | //function for pushing the element 12 | void push() 13 | { 14 | printf("\nenter the value to insert"); 15 | scanf("%d",&n); 16 | top=top+1; 17 | a[top]=n; 18 | } 19 | //function for poping the element out 20 | void pop() 21 | { 22 | if(top==0) 23 | { 24 | printf("\nstack is empty"); 25 | } 26 | else 27 | { 28 | int item; 29 | item=a[top]; 30 | top=top-1; 31 | printf("\npoped item is %d ",item); 32 | } 33 | } 34 | //function for peeping the element from top of the stack 35 | void peep() 36 | { 37 | int i; 38 | printf("\nenter the element position to view from top"); 39 | scanf("%d",&i); 40 | if(top-i+1<0) 41 | { 42 | printf("\nunderflow condition"); 43 | } 44 | else 45 | { 46 | int x; 47 | x=a[top-i+1]; 48 | printf("\nthe %dth element from top is %d",i,x); 49 | } 50 | } 51 | //function to update the element of stack 52 | 53 | void update() 54 | { 55 | int i,n; 56 | printf("\nenter the position to update"); 57 | scanf("%d",&i); 58 | printf("\nenter the item to insert"); 59 | scanf("%d",&n); 60 | if(top-i+1<0) 61 | { 62 | printf("\nunderflow condition"); 63 | } 64 | else 65 | { 66 | a[top-i+1]=n; 67 | } 68 | } 69 | int x; 70 | while(1) 71 | { 72 | printf("\n1.push"); 73 | printf("\n2.pop"); 74 | printf("\n3.peep"); 75 | printf("\n4.update"); 76 | printf("\nenter your choice"); 77 | scanf("%d",&x); 78 | switch(x) 79 | { 80 | case 1: 81 | push(); 82 | break; 83 | case 2: 84 | pop(); 85 | break; 86 | case 3: 87 | peep(); 88 | break; 89 | case 4: 90 | update(); 91 | break; 92 | default: 93 | printf("\ninvalid choice"); 94 | } 95 | } 96 | return(0); 97 | } 98 | 99 | -------------------------------------------------------------------------------- /computer-oriented-statistical-methods/Gauss_Elimination.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void display(float a[20][20],int n) 4 | { 5 | int i,j; 6 | for(i=0;i>>\n",i+1); 66 | for(j=0;j<=n;j++) 67 | { 68 | printf("r%d%d : ",i,j); 69 | scanf("%f",&m[i][j]); 70 | } 71 | printf("\n"); 72 | } 73 | printf(":::::::::::: Current Matrix ::::::::::::\n\n"); 74 | display(m,n); 75 | 76 | for(i=0;i>>>>>>>>>>>>>>>>>>>>>>>-------- %d\n",i+1); 79 | m[20][20]=interchange(m,i,n); 80 | display(m,n); 81 | printf("\n_______________________________________\n"); 82 | m[20][20]=eliminate(m,i,n); 83 | display(m,n); 84 | } 85 | printf("\n\n Values are : \n"); 86 | for(i=n-1;i>=0;i--) 87 | { 88 | l=n-1; 89 | mul=0; 90 | for(j=0;j 4 | #include 5 | 6 | //////////////////////////////////////////////////////////////////////////////// 7 | //MACROS: CONSTANTS 8 | 9 | //////////////////////////////////////////////////////////////////////////////// 10 | //DATA STRUCTURES 11 | struct node { 12 | int data; 13 | struct node* next; 14 | struct node* pre; 15 | } *head, *tail, *tmp; 16 | 17 | //////////////////////////////////////////////////////////////////////////////// 18 | //GLOBAL VARIABLES 19 | int count; 20 | 21 | //////////////////////////////////////////////////////////////////////////////// 22 | //FORWARD DECLARATIONS 23 | void create(); 24 | void enque(int x); 25 | int deque(); 26 | int peek(); 27 | int size(); 28 | int isEmpty(); 29 | 30 | //////////////////////////////////////////////////////////////////////////////// 31 | //MAIN ENTRY POINT 32 | 33 | int main(int argc, char const *argv[]) { 34 | 35 | create(); 36 | enque(5); 37 | 38 | 39 | return 0; 40 | } 41 | 42 | 43 | void create() { 44 | head = NULL; 45 | tail = NULL; 46 | } 47 | 48 | /** 49 | * Puts an item into the Queue. 50 | */ 51 | void enque(int x) { 52 | if(head == NULL) { 53 | head = (struct node *)malloc(1 * sizeof(struct node)); 54 | head->data = x; 55 | head->pre = NULL; 56 | tail = head; 57 | } else { 58 | tmp = (struct node *)malloc(1 * sizeof(struct node)); 59 | tmp->data = x; 60 | tmp->next = tail; 61 | tail = tmp; 62 | } 63 | } 64 | 65 | /** 66 | * Takes the next item from the Queue. 67 | */ 68 | int deque() { 69 | int returnData; 70 | if(head == NULL) { 71 | printf("ERROR: Deque from empty queue.\n"); 72 | exit(1); 73 | } else { 74 | returnData = head->data; 75 | if(head->pre == NULL) 76 | head = NULL; 77 | else 78 | head = head->pre; 79 | head->next = NULL; 80 | } 81 | } 82 | 83 | /** 84 | * Returns the size of the Queue. 85 | */ 86 | int size() { 87 | return count; 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 | -------------------------------------------------------------------------------- /Sorts/QuickSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /*Displays the array, passed to this method*/ 4 | 5 | void display(int arr[], int n){ 6 | 7 | int i; 8 | for(i = 0; i < n; i++){ 9 | printf("%d ", arr[i]); 10 | } 11 | 12 | printf("\n"); 13 | 14 | } 15 | 16 | /*Swap function to swap two values*/ 17 | void swap(int *first, int *second){ 18 | 19 | int temp = *first; 20 | *first = *second; 21 | *second = temp; 22 | 23 | } 24 | 25 | 26 | /*Partition method which selects a pivot 27 | and places each element which is less than the pivot value to its left 28 | and the elements greater than the pivot value to its right 29 | 30 | arr[] --- array to be partitioned 31 | lower --- lower index 32 | upper --- upper index 33 | */ 34 | int partition(int arr[], int lower, int upper){ 35 | 36 | int i = (lower - 1); 37 | 38 | int pivot = arr[upper]; // Selects last element as the pivot value 39 | 40 | int j ; 41 | for(j = lower; j < upper ; j++){ 42 | 43 | if(arr[j] <= pivot){ // if current element is smaller than the pivot 44 | 45 | i++; // increment the index of smaller element 46 | swap(&arr[i], &arr[j]); 47 | } 48 | } 49 | 50 | swap(&arr[i + 1] , &arr[upper]); // places the last element i.e, the pivot to its correct position 51 | 52 | return (i + 1); 53 | } 54 | 55 | 56 | /*This is where the sorting of the array takes place 57 | arr[] --- Array to be sorted 58 | lower --- Starting index 59 | upper --- Ending index 60 | */ 61 | void quickSort(int arr[], int lower, int upper){ 62 | 63 | if(upper > lower){ 64 | 65 | // partitioning index is returned by the partition method , partition element is at its correct poition 66 | 67 | int partitionIndex = partition(arr, lower, upper); 68 | 69 | 70 | // Sorting elements before and after the partition index 71 | quickSort(arr, lower, partitionIndex - 1); 72 | quickSort(arr, partitionIndex + 1, upper); 73 | } 74 | } 75 | 76 | 77 | int main(){ 78 | 79 | 80 | int n; 81 | printf("Enter size of array:\n"); 82 | scanf("%d", &n); // E.g. 8 83 | 84 | printf("Enter the elements of the array\n"); 85 | int i; 86 | int arr[n]; 87 | for(i = 0; i < n; i++){ 88 | scanf("%d", &arr[i] ); 89 | } 90 | 91 | printf("Original array: "); 92 | display(arr, n); // Original array : 10 11 9 8 4 7 3 8 93 | 94 | quickSort(arr, 0, n-1); 95 | 96 | printf("Sorted array: "); 97 | display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11 98 | 99 | return 0; 100 | } -------------------------------------------------------------------------------- /Data Structures/linked_list/singly_link_list_deletion.c: -------------------------------------------------------------------------------- 1 | /*Includes structure for a node which can be use to make new nodes of the Linked List. 2 | It is assumed that the data in nodes will be an integer, though 3 | function can be modified according to the data type, easily. 4 | deleteNode deletes a node when passed with a key of the node. 5 | */ 6 | #include 7 | struct node 8 | {int info; 9 | struct node *link; 10 | }; 11 | struct node *start=NULL; 12 | /////////////////////////////////////////////////////////// 13 | struct node * createnode()//function to create node 14 | { 15 | struct node *t; 16 | t=(struct node*)malloc(sizeof(struct node)); 17 | return(t); 18 | } 19 | //////////////////////////////////////////////////////// 20 | void insert()//function to insert at first location 21 | { 22 | struct node *p; 23 | p=createnode(); 24 | printf("\nenter the number to insert"); 25 | scanf("%d",&p->info); 26 | p->link=NULL; 27 | if(start==NULL) 28 | { 29 | start=p; 30 | } 31 | else 32 | { 33 | p->link=start; 34 | start=p; 35 | } 36 | } 37 | /////////////////////////////////////////////////////////// 38 | void deleteion()//function to delete from first position 39 | { 40 | struct node *t; 41 | if(start==NULL) 42 | { 43 | printf("\nlist is empty"); 44 | } 45 | else 46 | { 47 | struct node *p; 48 | p=start; 49 | start=start->link; 50 | free(p); 51 | } 52 | } 53 | /////////////////////////////////////////////////////// 54 | void viewlist()//function to display values 55 | { 56 | struct node *p; 57 | if(start==NULL) 58 | { 59 | printf("\nlist is empty"); 60 | } 61 | else 62 | { p=start; 63 | while(p!=NULL) 64 | { 65 | printf("%d ",p->info); 66 | p=p->link; 67 | } 68 | } 69 | } 70 | ////////////////////////////////////////////////////////////////////// 71 | 72 | int main() 73 | { 74 | int n; 75 | while(1) 76 | { 77 | printf("\n1.add value at first location"); 78 | printf("\n2.delete value from first location"); 79 | printf("\n3.view value"); 80 | printf("\nenter your choice"); 81 | scanf("%d",&n); 82 | switch(n) 83 | { 84 | case 1: 85 | insert(); 86 | break; 87 | case 2: 88 | deleteion(); 89 | break; 90 | case 3: 91 | viewlist(); 92 | break; 93 | default: 94 | printf("\ninvalid choice"); 95 | } 96 | } 97 | return(0); 98 | } 99 | -------------------------------------------------------------------------------- /Data Structures/Array/CArray.h: -------------------------------------------------------------------------------- 1 | /* 2 | * CArray.h 3 | * 4 | * Author: Leonardo Vencovsky 5 | * Created on 18/03/2018 6 | * 7 | * Modified by: Leonardo Vencovsky 8 | * Last modified: 19/03/2018 9 | * 10 | * Header for Array in C 11 | * 12 | * Compiled in Visual Studio 2017 13 | * 14 | */ 15 | 16 | #pragma once 17 | 18 | #ifdef __cplusplus 19 | extern "C" { 20 | #endif 21 | 22 | #define ARRAY_ERASED -1 23 | #define SUCCESS 0 24 | #define INVALID_POSITION 1 25 | #define POSITION_INIT 2 26 | #define POSITION_NOT_INIT 3 27 | #define POSITION_EMPTY 4 28 | #define ARRAY_FULL 5 29 | 30 | typedef struct CArray { 31 | int *array; 32 | int size; 33 | } CArray; 34 | 35 | // +-------------------------------------+ 36 | // | Returns array | 37 | // +-------------------------------------+ 38 | CArray * getCArray(int size); 39 | CArray * getCopyCArray(CArray *array); 40 | 41 | // +-------------------------------------+ 42 | // | Input / Output | 43 | // +-------------------------------------+ 44 | int insertValueCArray(CArray *array, int position, int value); 45 | int removeValueCArray(CArray *array, int position); 46 | int pushValueCArray(CArray *array, int value); 47 | int updateValueCArray(CArray *array, int position, int value); 48 | 49 | // +-------------------------------------+ 50 | // | Erase | 51 | // +-------------------------------------+ 52 | int eraseCArray(CArray *array); 53 | 54 | // +-------------------------------------+ 55 | // | Switching | 56 | // +-------------------------------------+ 57 | int switchValuesCArray(CArray *array, int position1, int position2); 58 | int reverseCArray(CArray *array); 59 | 60 | // +-------------------------------------+ 61 | // | Sorting | 62 | // +-------------------------------------+ 63 | int bubbleSortCArray(CArray *array); 64 | int selectionSortCArray(CArray *array); 65 | int insertionSortCArray(CArray *array); 66 | int blenderCArray(CArray *array); 67 | 68 | // +-------------------------------------+ 69 | // | Searching | 70 | // +-------------------------------------+ 71 | int valueOcurranceCArray(CArray *array, int value); 72 | CArray * valuePositionsCArray(CArray *array, int value); 73 | int findMaxCArray(CArray *array); 74 | int findMinCArray(CArray *array); 75 | 76 | // +-------------------------------------+ 77 | // | Display | 78 | // +-------------------------------------+ 79 | int displayCArray(CArray *array); 80 | 81 | 82 | #ifdef __cplusplus 83 | } 84 | #endif 85 | -------------------------------------------------------------------------------- /Data Structures/dictionary/dict.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "dict.h" 4 | 5 | 6 | /* simple constructor */ 7 | Dictionary * create_dict(void) 8 | { 9 | Dictionary * p_dic = malloc(sizeof (Dictionary)); 10 | if (p_dic) 11 | { 12 | p_dic->number_of_elements = 0; 13 | 14 | /* initializes the elemens of the array with NULL-pointer */ 15 | for (int i = 0; i < MAXELEMENTS; i++) 16 | { 17 | p_dic->elements[i] = NULL; 18 | } 19 | 20 | return p_dic; 21 | } 22 | else 23 | { 24 | printf("unable to create a dictionary\n"); 25 | return NULL; 26 | } 27 | } 28 | 29 | 30 | /* 31 | utility function 32 | sdbm hash algorithm 33 | returns a hashcode for the given string 's' 34 | */ 35 | int get_hash(char s[]) 36 | { 37 | unsigned int hash_code = 0; 38 | 39 | /* iterates over string at each character */ 40 | for (int counter = 0; s[counter]!='\0'; counter++) 41 | { 42 | /* actual computing of the hash code */ 43 | hash_code = s[counter] + (hash_code << 6) + (hash_code << 16) - hash_code; 44 | } 45 | 46 | /* % modulo is for fitting the index in array. */ 47 | return hash_code % MAXELEMENTS; 48 | } 49 | 50 | 51 | int add_item_label(Dictionary * dic,char label[],void * item) 52 | { 53 | unsigned int index = get_hash(label); 54 | 55 | /* make sure index is fitting */ 56 | if (index < MAXELEMENTS) 57 | { 58 | dic->elements[index] = item; 59 | return 0; 60 | } 61 | 62 | /* error case */ 63 | return -1; 64 | } 65 | 66 | 67 | int add_item_index(Dictionary * dic , int index, void * item) 68 | { 69 | /* make sure whether this place is already given */ 70 | if (!dic->elements[index]) 71 | { 72 | dic->elements[index] = item; 73 | return 0; 74 | } 75 | 76 | /* error case */ 77 | return -1; 78 | } 79 | 80 | 81 | void * get_element_label(Dictionary * dict, char s[]) 82 | { 83 | int index = get_hash(s); 84 | if (dict->elements[index]) 85 | { 86 | return dict->elements[index]; 87 | } 88 | 89 | printf("None entry at given label\n"); 90 | return NULL; 91 | } 92 | 93 | 94 | void * get_element_index(Dictionary * dict, int index) 95 | { 96 | if (index >= 0 && index < MAXELEMENTS) 97 | { 98 | return dict->elements[index]; 99 | } 100 | 101 | printf("index out of bounds!\n"); 102 | return NULL; 103 | } 104 | 105 | 106 | void destroy(Dictionary * dict) 107 | { 108 | free(dict); 109 | } -------------------------------------------------------------------------------- /exercism/word-count/word_count.c: -------------------------------------------------------------------------------- 1 | #include "word_count.h" 2 | #include 3 | 4 | /* 5 | word_count: returns the full number of words in the input_text, 6 | otherwise an error code: (see below) 7 | 8 | error codes: EXCESSIVE_LENGTH_WORD -1 9 | EXCESSIVE_NUMBER_OF_WORDS -2 10 | 11 | The function manipulates the given structure of type word_count_word_t 12 | After that process the member count contains the number of occures. 13 | */ 14 | int word_count(const char *input_text, word_count_word_t *words) 15 | { 16 | char word_list[MAX_WORDS][MAX_WORD_LENGTH]; 17 | char input[1000]; 18 | strcpy(input, input_text); 19 | char *p_str = input; 20 | 21 | /* index for iteration over input string */ 22 | int index = 0; 23 | 24 | /* index for word_list */ 25 | int index_list = 0; 26 | 27 | /* counts all words */ 28 | int count_all = 0; 29 | 30 | /* for controlling the while loop */ 31 | int loop = 1; 32 | 33 | /* for the for-loop */ 34 | int i = 0; 35 | 36 | /* collects all words in the word_list */ 37 | while (input[index] && loop) 38 | { 39 | if (input[index] == ' ') 40 | { 41 | input[index] = '\0'; 42 | if (strlen(p_str) <= MAX_WORD_LENGTH) 43 | { 44 | 45 | if (index_list <= MAX_WORDS) 46 | { 47 | strcpy(word_list[index_list], p_str); 48 | 49 | /* sets pointer to the next position */ 50 | p_str = input + index + 1; 51 | index_list++; 52 | 53 | /* counts the word */ 54 | count_all++; 55 | } 56 | else /* error case too many words */ 57 | { 58 | count_all = EXCESSIVE_NUMBER_OF_WORDS; 59 | loop = 0; 60 | } 61 | } 62 | else /* error case: word is too long */ 63 | { 64 | count_all = EXCESSIVE_LENGTH_WORD; 65 | loop = 0; 66 | } 67 | } 68 | 69 | index++; 70 | } 71 | 72 | words->count = 0; 73 | 74 | /* make sure none error is occured */ 75 | if (loop) 76 | { 77 | /* collects the last word up to the \0-character. and counts it.*/ 78 | strcpy(word_list[index_list], p_str); 79 | count_all++; 80 | 81 | for (i = 0; i <= index_list; i++) 82 | { 83 | if (strcmp(word_list[i],words->text) == 0) 84 | { 85 | words->count++; 86 | } 87 | } 88 | } 89 | 90 | /* returns the number of words or an error code */ 91 | return count_all; 92 | } -------------------------------------------------------------------------------- /misc/LongestSubSequence.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | void longestSub(int* ARRAY,int ARRAY_LENGTH, int** RESULT,int* RESULT_LENGTH){ //RESULT and RESULT_LENGTH will be modified by their pointers 6 | 7 | if(ARRAY_LENGTH <= 1){ 8 | *RESULT=ARRAY; 9 | *RESULT_LENGTH = ARRAY_LENGTH; 10 | } 11 | else{ 12 | int PIVOT = ARRAY[0]; 13 | int *LONGEST_SUB = NULL; 14 | int i, j, LONGEST_SUB_LENGTH = 0; 15 | int TEMPORARY_ARRAY_LENGTH = 0, *TEMPORARY_ARRAY = NULL; 16 | 17 | for(i = 1; i < ARRAY_LENGTH; i++){ 18 | if (ARRAY[i] < PIVOT){ 19 | 20 | TEMPORARY_ARRAY_LENGTH = 0; 21 | TEMPORARY_ARRAY = NULL; 22 | 23 | for(j = i+1;j < ARRAY_LENGTH; j++){ 24 | if(ARRAY[j] >= ARRAY[i]){ 25 | 26 | TEMPORARY_ARRAY_LENGTH++; 27 | TEMPORARY_ARRAY = (int *)realloc(TEMPORARY_ARRAY, TEMPORARY_ARRAY_LENGTH*sizeof(int)); 28 | TEMPORARY_ARRAY[TEMPORARY_ARRAY_LENGTH-1] = ARRAY[j]; 29 | } 30 | } 31 | 32 | longestSub(TEMPORARY_ARRAY, TEMPORARY_ARRAY_LENGTH, &TEMPORARY_ARRAY, &TEMPORARY_ARRAY_LENGTH); 33 | if(LONGEST_SUB_LENGTH < TEMPORARY_ARRAY_LENGTH + 1){ 34 | 35 | LONGEST_SUB_LENGTH = TEMPORARY_ARRAY_LENGTH + 1; 36 | LONGEST_SUB = (int *)realloc(LONGEST_SUB, LONGEST_SUB_LENGTH*sizeof(int)); 37 | LONGEST_SUB[0] = ARRAY[i]; 38 | 39 | for(i = 1;i < LONGEST_SUB_LENGTH; i++) 40 | LONGEST_SUB[i] = TEMPORARY_ARRAY[i-1]; 41 | } 42 | } 43 | } 44 | 45 | TEMPORARY_ARRAY = NULL; 46 | TEMPORARY_ARRAY_LENGTH = 0; 47 | for(i = 1;i < ARRAY_LENGTH; i++){ 48 | 49 | if(ARRAY[i] >= PIVOT){ 50 | TEMPORARY_ARRAY_LENGTH++; 51 | TEMPORARY_ARRAY = (int *)realloc(TEMPORARY_ARRAY, TEMPORARY_ARRAY_LENGTH*sizeof(int)); 52 | TEMPORARY_ARRAY[TEMPORARY_ARRAY_LENGTH-1] = ARRAY[i]; 53 | } 54 | } 55 | 56 | longestSub(TEMPORARY_ARRAY, TEMPORARY_ARRAY_LENGTH, &TEMPORARY_ARRAY, &TEMPORARY_ARRAY_LENGTH); 57 | if(TEMPORARY_ARRAY_LENGTH + 1 > LONGEST_SUB_LENGTH){ 58 | 59 | LONGEST_SUB_LENGTH = TEMPORARY_ARRAY_LENGTH + 1; 60 | LONGEST_SUB = (int *)realloc(LONGEST_SUB, LONGEST_SUB_LENGTH*sizeof(int)); 61 | LONGEST_SUB[0] = PIVOT; 62 | for(i = 1;i < LONGEST_SUB_LENGTH; i++) 63 | LONGEST_SUB[i] = TEMPORARY_ARRAY[i-1]; 64 | } 65 | *RESULT = LONGEST_SUB; 66 | *RESULT_LENGTH = LONGEST_SUB_LENGTH; 67 | } 68 | 69 | 70 | } 71 | 72 | int main(){ 73 | 74 | int EXAMPLE_LENGTH = 8; 75 | int EXAMPLE[] = {18, 2, 15, 4, 30, 0, 11, 12}; 76 | 77 | int *RESULT = NULL; 78 | int RESULT_LENGTH, i; 79 | 80 | longestSub(EXAMPLE, EXAMPLE_LENGTH, &RESULT, &RESULT_LENGTH); 81 | 82 | printf("Longest Sub Sequence length: %d and it's:\n", RESULT_LENGTH); 83 | for(i = 0;i < RESULT_LENGTH; i++) 84 | printf("%d ",RESULT[i]); 85 | printf("\n"); 86 | 87 | return 0; 88 | } -------------------------------------------------------------------------------- /Searches/modifiedBinarySearch.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int n,m; //size of the matrix 4 | 5 | // This function does Binary search for x in i-th row from j_low to j_high. 6 | void binarySearch(int mat[n][m], int i, int j_low,int j_high, int x) 7 | { 8 | while (j_low <= j_high) 9 | { 10 | int j_mid = (j_low + j_high) / 2; 11 | 12 | // Element found 13 | if (mat[i][j_mid] == x){ 14 | printf("Found at (%d,%d)\n",i,j_mid); 15 | return ; 16 | } 17 | else if (mat[i][j_mid] > x) 18 | j_high = j_mid - 1; 19 | else 20 | j_low = j_mid + 1; 21 | } 22 | // element not found 23 | printf("element not found\n"); 24 | } 25 | 26 | // Function to perform binary search on the mid values of row to get the desired pair of rows 27 | // where the element can be found 28 | void modifiedBinarySearch(int mat[n][m], int n, int m, int x) 29 | { // If Single row matrix 30 | if (n == 1){ 31 | binarySearch(mat, 0, 0, m-1, x); 32 | return; 33 | } 34 | 35 | // Do binary search in middle column. 36 | // Condition to terminate the loop when the 2 desired rows are found. 37 | int i_low = 0, i_high = n-1, j_mid = m/2; 38 | while ((i_low+1) < i_high) 39 | { 40 | int i_mid = (i_low + i_high) / 2; 41 | // element found 42 | if (mat[i_mid][j_mid] == x){ 43 | printf("Found at (%d,%d)\n",i_mid,j_mid); 44 | return; 45 | } 46 | else if (mat[i_mid][j_mid] > x) 47 | i_high = i_mid; 48 | else 49 | i_low = i_mid; 50 | } 51 | // If element is present on the mid of the two rows 52 | if (mat[i_low][j_mid] == x) 53 | printf("Found at (%d,%d)\n",i_low,j_mid); 54 | else if (mat[i_low+1][j_mid] == x) 55 | printf("Found at (%d,%d)\n",i_low+1,j_mid); 56 | 57 | // Search element on 1st half of 1st row 58 | else if (x <= mat[i_low][j_mid-1]) 59 | binarySearch(mat, i_low, 0, j_mid-1, x); 60 | 61 | // Search element on 2nd half of 1st row 62 | else if (x >= mat[i_low][j_mid+1] && x <= mat[i_low][m-1]) 63 | binarySearch(mat, i_low, j_mid+1, m-1, x); 64 | 65 | // Search element on 1st half of 2nd row 66 | else if (x <= mat[i_low+1][j_mid-1]) 67 | binarySearch(mat, i_low+1, 0, j_mid-1, x); 68 | 69 | // search element on 2nd half of 2nd row 70 | else 71 | binarySearch(mat, i_low+1, j_mid+1, m-1, x); 72 | } 73 | 74 | int main() 75 | { 76 | int x; //element to be searched 77 | scanf("%d %d %d\n",&n,&m,&x); 78 | int mat[n][m]; 79 | for(int i=0; i 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | //Structure for storing a graph 8 | struct Graph{ 9 | int vertexNum; 10 | int** edges; 11 | }; 12 | 13 | //Constructs a graph with V vertices and E edges 14 | void createGraph(struct Graph* G,int V){ 15 | G->vertexNum = V; 16 | G->edges =(int**) malloc(V * sizeof(int*)); 17 | for(int i=0; iedges[i] = (int*) malloc(V * sizeof(int)); 19 | for(int j=0; jedges[i][j] = INT_MAX; 21 | G->edges[i][i] = 0; 22 | } 23 | } 24 | 25 | //Adds the given edge to the graph 26 | void addEdge(struct Graph* G, int src, int dst, int weight){ 27 | G->edges[src][dst] = weight; 28 | } 29 | 30 | 31 | //Utility function to print distances 32 | void print(int dist[], int V){ 33 | printf("\nThe Distance matrix for Floyd - Warshall\n"); 34 | for(int i = 0; i < V; i++){ 35 | for(int j=0; jvertexNum; 50 | int dist[V][V]; 51 | 52 | //Initialise distance array 53 | for(int i=0; iedges[i][j]; 56 | 57 | 58 | //Calculate distances 59 | for(int k=0; k 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | //Structure for storing a graph 8 | struct Graph{ 9 | int vertexNum; 10 | int** edges; 11 | }; 12 | 13 | //Constructs a graph with V vertices and E edges 14 | void createGraph(struct Graph* G,int V){ 15 | G->vertexNum = V; 16 | G->edges =(int**) malloc(V * sizeof(int*)); 17 | for(int i=0; iedges[i] = (int*) malloc(V * sizeof(int)); 19 | for(int j=0; jedges[i][j] = INT_MAX; 21 | G->edges[i][i] = 0; 22 | } 23 | } 24 | 25 | //Adds the given edge to the graph 26 | void addEdge(struct Graph* G, int src, int dst, int weight){ 27 | G->edges[src][dst] = weight; 28 | } 29 | 30 | 31 | //Utility function to find minimum distance vertex in mdist 32 | int minDistance(int mdist[], int vset[], int V){ 33 | int minVal = INT_MAX, minInd ; 34 | for(int i=0; ivertexNum; 59 | int mdist[V]; //Stores updated distances to vertex 60 | int vset[V]; // vset[i] is true if the vertex i included 61 | // in the shortest path tree 62 | 63 | //Initialise mdist and vset. Set distance of source as zero 64 | for(int i=0; iedges[u][v]!=INT_MAX && mdist[u] + graph->edges[u][v] < mdist[v]) 76 | mdist[v] = mdist[u] + graph->edges[u][v]; 77 | 78 | } 79 | } 80 | 81 | print(mdist, V); 82 | 83 | return; 84 | } 85 | 86 | 87 | 88 | //Driver Function 89 | int main(){ 90 | int V,E,gsrc; 91 | int src,dst,weight; 92 | struct Graph G; 93 | printf("Enter number of vertices: "); 94 | scanf("%d",&V); 95 | printf("Enter number of edges: "); 96 | scanf("%d",&E); 97 | createGraph(&G,V); 98 | for(int i=0; i 10 | #include 11 | #include 12 | 13 | #include "stack.h" 14 | 15 | /* 16 | actual stack data structure 17 | This pointer will pointing at the actual field (of void * pointers) 18 | that represents the stack. 19 | */ 20 | void **array; 21 | 22 | /* the current capacity of the stack */ 23 | int max = 10; 24 | 25 | /* counter variable for counting the elements of the stack. */ 26 | int counter = 0; 27 | 28 | /* 29 | offset address 30 | points at the top element of the stack. 31 | */ 32 | int offset = -1; 33 | 34 | void initStack() 35 | { 36 | 37 | array = malloc(sizeof(void *) * max); 38 | assert(array); /* tests whether pointer is assigned to memory. */ 39 | } 40 | 41 | /* 42 | grow: increases the stack by 10 elements. 43 | This utility function isn't part of the public interface 44 | */ 45 | void grow() 46 | { 47 | max += 10; /* increases the capacity */ 48 | 49 | int i; // for the loop 50 | void **tmp = malloc(sizeof(void *) * max); 51 | 52 | /* copies the elements from the origin array in the new one. */ 53 | for (i = 0; i < max - 10; i++) 54 | { 55 | *(tmp + i) = *(array + i); 56 | } 57 | 58 | array = tmp; /* setups the new one as basis */ 59 | } 60 | 61 | /* push: pushs the argument onto the stack */ 62 | void push(void *object) 63 | { 64 | 65 | assert(object); /* tests whether pointer isn't null */ 66 | 67 | if (counter < max) 68 | { 69 | 70 | offset++; /* increases the element-pointer */ 71 | 72 | /* 73 | moves pointer by the offset address 74 | pushs the object onto stack 75 | */ 76 | *(array + offset) = object; 77 | 78 | /* increases the inner counter */ 79 | counter++; 80 | } 81 | else /* stack is full */ 82 | { 83 | 84 | grow(); /* lets grow stack */ 85 | push(object); /* recursive call */ 86 | } 87 | } 88 | 89 | /* 90 | pop: pops the top element of the stack from the stack. 91 | */ 92 | void *pop() 93 | { 94 | 95 | void *top = *(array + offset); 96 | 97 | /* check pointers */ 98 | assert(top); 99 | 100 | /* if use the pop-function, stack must not empty. */ 101 | assert(!isEmpty()); 102 | 103 | /* decreases the offset address for pointing of 104 | the new top element */ 105 | offset--; 106 | 107 | /* decreases the inner counter */ 108 | counter--; 109 | 110 | return top; 111 | } 112 | 113 | /* 114 | size: gets the number of elements of the stack. 115 | */ 116 | int size() 117 | { 118 | return counter; 119 | } 120 | 121 | /* 122 | isEmpty(): returns 1 if stack is empty otherwise 0. 123 | */ 124 | int isEmpty() 125 | { 126 | return counter == 0; 127 | } 128 | 129 | /* 130 | top: returns the top element from the stack without removing it. 131 | */ 132 | void *top() 133 | { 134 | /* offset address points to the top element */ 135 | return array[offset]; 136 | } -------------------------------------------------------------------------------- /Data Structures/linked_list/mergeLinkedLists.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node{ 4 | int data; 5 | struct node *next; 6 | }; 7 | 8 | struct node *head1 = NULL; 9 | struct node *head2 = NULL; 10 | 11 | ///// MAIN ALGORITHMIC FUNCTION to MERGE the two input linked lists /////// 12 | 13 | void merge() 14 | { 15 | struct node *temp1 = head1; 16 | struct node *temp2 = head2; 17 | 18 | struct node *holder1 = NULL; 19 | struct node *holder2 = NULL; 20 | //Temporary pointer variables to store the address of next node of the two input linked list 21 | 22 | while(temp1!=NULL && temp2!=NULL) 23 | { 24 | holder1 = temp1 -> next; 25 | //Storing the address of next node of first linked list 26 | temp1->next=temp2; 27 | //Making the first node of first linked list point to first node of second linked list 28 | 29 | if(holder1!=NULL) { 30 | //Making the first node of second linked list point to second node of first linked list 31 | holder2 = temp2 -> next; 32 | temp2 -> next = holder1; 33 | } 34 | temp1=holder1; 35 | temp2=holder2; 36 | //Updating the address location of two pointer variables temp1 and temp2 37 | } 38 | } 39 | 40 | void printlist(struct node *temp){ 41 | printf("%d",temp->data); 42 | temp=temp->next; 43 | while(temp!=NULL){ 44 | printf("->%d",temp->data); 45 | temp=temp->next; 46 | } 47 | printf("\n"); 48 | } 49 | 50 | int main() 51 | { 52 | // Linked List 1: 1->3->5->7 : Linked List 2: 2->4->6 53 | // making lists 54 | struct node *one = (struct node*)malloc(sizeof(struct node)); 55 | struct node *two = (struct node*)malloc(sizeof(struct node)); 56 | struct node *three = (struct node*)malloc(sizeof(struct node)); 57 | struct node *four = (struct node*)malloc(sizeof(struct node)); 58 | struct node *five = (struct node*)malloc(sizeof(struct node)); 59 | struct node *six = (struct node*)malloc(sizeof(struct node)); 60 | struct node *seven = (struct node*)malloc(sizeof(struct node)); 61 | //Seven nodes are created 62 | 63 | head1=one; 64 | head2=two; 65 | //head1 points to first node of first linked list 66 | //head2 points to first node of second linked list 67 | 68 | one->data=1; 69 | one->next=three; 70 | 71 | two->data=2; 72 | two->next=four; 73 | 74 | three->data=3; 75 | three->next=five; 76 | 77 | four->data=4; 78 | four->next=six; 79 | 80 | five->data=5; 81 | five->next=seven; 82 | 83 | six->data=6; 84 | six->next=NULL; 85 | //Last node of second input linked list 86 | 87 | seven->data=7; 88 | seven->next=NULL; 89 | //Last node of first input linked list 90 | 91 | printf("Linked List 1: "); 92 | printlist(head1); 93 | printf("\nLinked List 2: "); 94 | printlist(head2); 95 | 96 | //Merging the two linked list into single linked list 97 | merge(); 98 | 99 | printf("\nMerged Linked List: "); 100 | printlist(head1); //list one has been modified 101 | 102 | return 0; 103 | } 104 | -------------------------------------------------------------------------------- /Computer Oriented Statistical Methods/statistic/README.md: -------------------------------------------------------------------------------- 1 | # Statistic-library for C 2 | 3 | This repository contains a statistic library for the C programming language which prepare useful functions for dealing with average, standard deviation etc. The library is platform-independent. So you can use this library with any C-compiler. 4 | 5 | ### Usage 6 | 7 | You needed to put in the files ```statistic.h``` and ```statistic.c``` in your project directory. After that you include the header file ```statistic.h``` 8 | in your project. Then you can use the functions of this library. You will find the files ```statistic.h``` and ```statistic.c``` in the directory **src**. 9 | 10 | ### Overview about the functions 11 | 12 | The first int-argument represents the size of the sample (double-array). 13 | 14 | ```c 15 | /* 16 | Computes the average of a given sample. 17 | The sample is a set of double values. 18 | The average-function gets a variable number of arguments. 19 | The first argument must be the number of arguments! 20 | The averageArray-function instead gets a double-array of values and a int-number that 21 | represents the size of the double-array. 22 | */ 23 | double average_Array(int,const double[]); 24 | double average(int,...); 25 | ``` 26 | 27 | ```c 28 | /* 29 | Computes the standard deviation (n-1) 30 | */ 31 | double standard_deviation(int,...); 32 | double standard_deviation_array(int, const double[]); 33 | 34 | /* 35 | Computes the standard deviation (n) 36 | */ 37 | double standard_deviation_N(int,...); 38 | double standard_deviation_N_array(int, const double[]); 39 | ``` 40 | 41 | ```c 42 | /* 43 | variance: computes the variance (n-1) 44 | variance_N: computes the variance (n) 45 | */ 46 | double variance(int, const double[]); 47 | double variance_N(int, const double[]); 48 | ``` 49 | 50 | ```c 51 | /* 52 | gets the max (min) element of the sample 53 | */ 54 | double max(int, const double[]); 55 | double min(int , const double[]); 56 | ``` 57 | 58 | ```c 59 | /* 60 | computes the median of the sample 61 | */ 62 | double median(int, const double[]); 63 | ``` 64 | 65 | ```c 66 | /* 67 | adds up all values of the sample. 68 | */ 69 | double sum(int,const double[]); 70 | ``` 71 | 72 | ```c 73 | /* 74 | computes the range of the sample. 75 | */ 76 | double range(int, const double[]); 77 | ``` 78 | 79 | ```c 80 | /* 81 | gets the frequency of the last argument (double) of that sample. 82 | */ 83 | double frequency_of(int, const double[], double); 84 | ``` 85 | 86 | ```c 87 | /* 88 | quartile_I: computes the first quartile. 89 | quartile_III: computes the third quartile. 90 | The second quartile is the median! 91 | */ 92 | double quartile_I(int, const double[]); 93 | double quartile_III(int, const double[]); 94 | ``` 95 | 96 | ```c 97 | /* 98 | computes the quartile distance 99 | */ 100 | double quartile_distance(int, const double[]); 101 | ``` 102 | 103 | 104 | ### Running the tests 105 | 106 | You navigate in the directory of this repository and type in the console: 107 | 108 | ```bash 109 | gcc -o myTests test/test.c src/statistic.c -lcunit -lm && ./myTests 110 | ``` 111 | 112 | #### Dependencies for tests 113 | 114 | * CUnit 115 | * gcc 116 | 117 | -------------------------------------------------------------------------------- /data_structures/graphs/Bellman-Ford.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | //Structure for storing edge 7 | struct Edge{ 8 | int src,dst,weight; 9 | }; 10 | 11 | //Structure for storing a graph 12 | struct Graph{ 13 | int vertexNum; 14 | int edgeNum; 15 | struct Edge* edges; 16 | }; 17 | 18 | //Constructs a graph with V vertices and E edges 19 | void createGraph(struct Graph* G,int V,int E){ 20 | G->vertexNum = V; 21 | G->edgeNum = E; 22 | G->edges = (struct Edge*) malloc(E * sizeof(struct Edge)); 23 | } 24 | 25 | //Adds the given edge to the graph 26 | void addEdge(struct Graph* G, int src, int dst, int weight){ 27 | static int ind; 28 | struct Edge newEdge; 29 | newEdge.src = src; 30 | newEdge.dst = dst; 31 | newEdge.weight = weight; 32 | G->edges[ind++]= newEdge; 33 | } 34 | 35 | 36 | //Utility function to find minimum distance vertex in mdist 37 | int minDistance(int mdist[], int vset[], int V){ 38 | int minVal = INT_MAX, minInd ; 39 | for(int i=0; ivertexNum; 64 | int E = graph->edgeNum; 65 | int dist[V]; 66 | 67 | //Initialize distances array as INF for all except source 68 | //Intialize source as zero 69 | for(int i=0; iedges[j].src; 78 | int v = graph->edges[j].dst; 79 | int w = graph->edges[j].weight; 80 | 81 | if(dist[u]!=INT_MAX && dist[u] + w < dist[v]) 82 | dist[v] = dist[u] + w; 83 | } 84 | 85 | //Iterate inner loop once more to check for negative cycle 86 | for(int j = 0; jedges[j].src; 88 | int v = graph->edges[j].dst; 89 | int w = graph->edges[j].weight; 90 | 91 | if(dist[u]!=INT_MAX && dist[u] + w < dist[v]){ 92 | printf("Graph contains negative weight cycle. Hence, shortest distance not guaranteed."); 93 | return; 94 | } 95 | } 96 | 97 | print(dist, V); 98 | 99 | return; 100 | } 101 | 102 | 103 | 104 | //Driver Function 105 | int main(){ 106 | int V,E,gsrc; 107 | int src,dst,weight; 108 | struct Graph G; 109 | printf("Enter number of vertices: "); 110 | scanf("%d",&V); 111 | printf("Enter number of edges: "); 112 | scanf("%d",&E); 113 | createGraph(&G,V,E); 114 | for(int i=0; i 9 | #include 10 | 11 | //////////////////////////////////////////////////////////////////////////////// 12 | //MACROS: CONSTANTS 13 | 14 | //////////////////////////////////////////////////////////////////////////////// 15 | //DATA STRUCTURES 16 | struct node { 17 | int data; 18 | struct node* next; 19 | struct node* pre; 20 | } *head, *tmp; 21 | 22 | //////////////////////////////////////////////////////////////////////////////// 23 | //GLOBAL VARIABLES 24 | int count = 0; 25 | 26 | //////////////////////////////////////////////////////////////////////////////// 27 | //FUNCTION PROTOTYPES 28 | void create(); 29 | void push(int x); 30 | int pop(); 31 | int peek(); 32 | int size(); 33 | int isEmpty(); 34 | 35 | //////////////////////////////////////////////////////////////////////////////// 36 | //MAIN ENTRY POINT 37 | 38 | int main(int argc, char const *argv[]) { 39 | 40 | int x, y, z; 41 | 42 | create(); 43 | push(4); 44 | x = pop(); 45 | // 4. Count: 0. Empty: 1. 46 | printf("%d.\t\tCount: %d.\tEmpty: %d.\n", x, size(), isEmpty()); 47 | 48 | push(1); 49 | push(2); 50 | push(3); 51 | x = pop(); 52 | y = pop(); 53 | // 3, 2. Count: 1. Empty: 0; 54 | printf("%d, %d.\t\tCount: %d.\tEmpty: %d.\n", x, y, size(), isEmpty()); 55 | pop(); // Empty the stack. 56 | 57 | push(5); 58 | push(6); 59 | x = peek(); 60 | push(7); 61 | y = pop(); 62 | push(8); 63 | z = pop(); 64 | // 1, 6, 7, 8. Count: 2. Empty: 0. 65 | printf("%d, %d, %d.\tCount: %d.\tEmpty: %d.\n", x, y, z, size(), isEmpty()); 66 | 67 | return 0; 68 | } 69 | 70 | /** 71 | * Initialize the stack to NULL. 72 | */ 73 | void create() { 74 | head = NULL; 75 | } 76 | 77 | /** 78 | * Push data onto the stack. 79 | */ 80 | void push(int x) { 81 | if(head == NULL) { 82 | head = (struct node *)malloc(1 * sizeof(struct node)); 83 | head->next = NULL; 84 | head->pre = NULL; 85 | head->data = x; 86 | } else { 87 | tmp = (struct node *)malloc(1 * sizeof(struct node)); 88 | tmp->data = x; 89 | tmp->next = NULL; 90 | tmp->pre = head; 91 | head = tmp; 92 | } 93 | ++count; 94 | } 95 | 96 | /** 97 | * Pop data from the stack 98 | */ 99 | int pop() { 100 | int returnData; 101 | if(head == NULL) { 102 | printf("ERROR: Pop from empty stack.\n"); 103 | exit(1); 104 | } else { 105 | returnData = head->data; 106 | 107 | if(head->pre == NULL) 108 | head = NULL; 109 | else { 110 | head = head->pre; 111 | free(head->next); 112 | } 113 | } 114 | --count; 115 | return returnData; 116 | } 117 | 118 | /** 119 | * Returns the next value to be popped. 120 | */ 121 | int peek() { 122 | if(head != NULL) 123 | return head->data; 124 | else { 125 | printf("ERROR: Peeking from empty stack."); 126 | exit(1); 127 | } 128 | } 129 | 130 | /** 131 | * Returns the size of the stack. 132 | */ 133 | int size() { 134 | return count; 135 | } 136 | 137 | /** 138 | * Returns 1 if stack is empty, returns 0 if not empty. 139 | */ 140 | int isEmpty() { 141 | if(count == 0) 142 | return 1; 143 | return 0; 144 | } 145 | -------------------------------------------------------------------------------- /Data Structures/Array/CArrayTests.c: -------------------------------------------------------------------------------- 1 | /* 2 | * CArrayTests.c 3 | * 4 | * Author: Leonardo Vencovsky 5 | * Created on 19/03/2018 6 | * 7 | * Modified by: Leonardo Vencovsky 8 | * Last modified: 19/03/2018 9 | * 10 | * Test Cases for Array Implementations in C 11 | * 12 | * Compiled in Visual Studio 2017 13 | * 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include "CArray.h" 20 | 21 | int CArrayTests() 22 | { 23 | printf("\n"); 24 | printf(" +-------------------------------------+\n"); 25 | printf(" | |\n"); 26 | printf(" | C Array |\n"); 27 | printf(" | |\n"); 28 | printf(" +-------------------------------------+\n"); 29 | printf("\n"); 30 | 31 | CArray *array = getCArray(10); 32 | 33 | int i; 34 | for (i = 0; i < array->size; i++) { 35 | insertValueCArray(array, i, i+1); 36 | } 37 | 38 | displayCArray(array); 39 | printf("\nCode: %d\n", pushValueCArray(array, 11)); // 5 40 | 41 | for (i = 0; i < array->size; i++) { 42 | removeValueCArray(array, i); 43 | } 44 | 45 | displayCArray(array); 46 | 47 | printf("\nCode: %d", removeValueCArray(array, -1)); // 1 48 | printf("\nCode: %d\n", insertValueCArray(array, -1, 1)); // 1 49 | 50 | // Erase 51 | for (i = 0; i < array->size; i++) { 52 | insertValueCArray(array, i, i + 1); 53 | } 54 | eraseCArray(array); 55 | displayCArray(array); // Should give all 0s 56 | 57 | // Switching 58 | CArray *arr = getCArray(13); 59 | for (i = 0; i < arr->size; i++) { 60 | insertValueCArray(arr, i, i + 1); 61 | } 62 | displayCArray(arr); 63 | for (i = 0; i < arr->size / 2; i++) { 64 | switchValuesCArray(arr, i, arr->size - i - 1); 65 | } 66 | 67 | displayCArray(arr); 68 | 69 | // Or simply... 70 | reverseCArray(arr); 71 | 72 | displayCArray(arr); 73 | 74 | // Sorting 75 | srand(time(NULL)); 76 | CArray *barray = getCArray(20); 77 | for (i = 0; i < barray->size; i++) { 78 | insertValueCArray(barray, i, rand()); 79 | } 80 | CArray *carray = getCopyCArray(barray); 81 | CArray *darray = getCopyCArray(barray); 82 | printf("\nNot sorted Array:"); 83 | displayCArray(barray); 84 | 85 | printf("\nBubble Sort:"); 86 | clock_t begin1 = clock(); 87 | // Timing bubble sort 88 | bubbleSortCArray(barray); 89 | clock_t end1 = clock(); 90 | double time_spent1 = (double)(end1 - begin1) / CLOCKS_PER_SEC; 91 | displayCArray(barray); 92 | 93 | printf("\nSelection Sort:"); 94 | clock_t begin2 = clock(); 95 | // Timing selection sort 96 | selectionSortCArray(carray); 97 | clock_t end2 = clock(); 98 | double time_spent2 = (double)(end2 - begin2) / CLOCKS_PER_SEC; 99 | displayCArray(carray); 100 | 101 | printf("\nInsertion Sort:"); 102 | clock_t begin3 = clock(); 103 | // Timing insertion sort 104 | insertionSortCArray(darray); 105 | clock_t end3 = clock(); 106 | double time_spent3 = (double)(end3 - begin3) / CLOCKS_PER_SEC; 107 | displayCArray(carray); 108 | 109 | // Descending order 110 | reverseCArray(barray); 111 | // displayCArray(barray); 112 | 113 | // printf("\nBlender:"); 114 | // blenderCArray(barray); 115 | // displayCArray(barray); 116 | 117 | printf("\nTotal time spent for bubble sort: %lf seconds", time_spent1); 118 | printf("\nTotal time spent for selection sort: %lf seconds", time_spent2); 119 | printf("\nTotal time spent for insertion sort: %lf seconds", time_spent3); 120 | 121 | // Searching 122 | CArray *aarray = getCArray(1000); 123 | for (i = 0; i < aarray->size; i++) { 124 | insertValueCArray(aarray, i, rand() % 100); 125 | } 126 | 127 | int j = 24; 128 | printf("\nOccurrences of the number %d in the array: %d", j, 129 | valueOcurranceCArray(aarray, j)); 130 | printf("\nAnd its positions:\n"); 131 | CArray *positions = valuePositionsCArray(aarray, j); 132 | displayCArray(positions); 133 | // This should all give value of j 134 | printf("\nAll %d s", j); 135 | for (i = 0; i < positions->size; i++) { 136 | printf("\nPosition %d has a value of %d", 137 | positions->array[i], aarray->array[positions->array[i]]); 138 | } 139 | printf("\nThe list has a minimum value of %d and a maximum value of %d", 140 | findMinCArray(aarray), findMaxCArray(aarray)); 141 | insertionSortCArray(aarray); 142 | // displayCArray(aarray); 143 | 144 | free(arr); 145 | free(array); 146 | free(aarray); 147 | free(barray); 148 | free(carray); 149 | free(darray); 150 | printf("\n"); 151 | return 0; 152 | } -------------------------------------------------------------------------------- /Data Structures/trie/trie.c: -------------------------------------------------------------------------------- 1 | /*------------------Trie Data Structure----------------------------------*/ 2 | /*-------------Implimented for search a word in dictionary---------------*/ 3 | 4 | /*-----character - 97 used for get the character from the ASCII value-----*/ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define ALPHABET_SIZE 26 12 | 13 | /*--Node in the Trie--*/ 14 | typedef struct TrieNode 15 | { 16 | struct TrieNode *children[ALPHABET_SIZE]; 17 | char character; 18 | bool isEndOfWord; 19 | 20 | } TrieNode; 21 | 22 | /*--Create new node--*/ 23 | TrieNode *createTrieNode() 24 | { 25 | TrieNode *node; 26 | node = malloc(sizeof(TrieNode)); 27 | node->isEndOfWord = false; 28 | int i = 0; 29 | while(ichildren[i] = NULL; 31 | i++; 32 | } 33 | return node; 34 | } 35 | 36 | /*--Insert new word to Trie--*/ 37 | void insert(TrieNode *root,char *word) 38 | { 39 | /*----Addition of the word done by recurcively----*/ 40 | 41 | //Check wheather word character pointer is NULL 42 | if((strlen(word)-1) != 0) 43 | { 44 | char character = *word; 45 | if(root->children[character-97] == NULL) 46 | { 47 | TrieNode *node = NULL; 48 | node = createTrieNode(); 49 | node->character = character; 50 | root->children[character-97] = node; 51 | } 52 | word++; 53 | insert(root->children[character-97],word); 54 | } 55 | else 56 | { 57 | root->isEndOfWord = true; 58 | } 59 | return; 60 | } 61 | 62 | /*--Search a word in the Trie--*/ 63 | TrieNode *search( TrieNode *root, char *word) 64 | { 65 | TrieNode *temp; 66 | while(*word != '\0') 67 | { 68 | char character = *word; 69 | if(root->children[character - 97] != NULL) 70 | { 71 | temp = root->children[character-97]; 72 | word++; 73 | root = temp; 74 | } 75 | else 76 | { 77 | printf("No possible words!!\n"); 78 | return NULL; 79 | } 80 | } 81 | return root; 82 | } 83 | 84 | /*---Print a word in the array--*/ 85 | void printArray(char chars[], int len) 86 | { 87 | int i; 88 | for (i=0; icharacter; 101 | filledLen++; 102 | 103 | if (node->isEndOfWord) 104 | { 105 | printArray(prefix, filledLen); 106 | } 107 | 108 | int i ; 109 | for(i=0;ichildren[i], prefix, filledLen); 112 | } 113 | } 114 | 115 | /*--Travel through the Trie and return words from it--*/ 116 | void traverse(char prefix[], TrieNode *root) 117 | { 118 | TrieNode *temp = NULL; 119 | temp = search(root,prefix); 120 | int j=0; 121 | while(prefix[j]!='\0') 122 | { 123 | j++; 124 | } 125 | printPathsRecur(temp,prefix,j-1); 126 | } 127 | 128 | /*------Demonstrate purposes uses text file called dictionary -------*/ 129 | 130 | #define NUMBER_OF_WORDS (354935) 131 | #define INPUT_WORD_SIZE (100) 132 | 133 | /*----Get input from the user------*/ 134 | char *receiveInput(char *s) 135 | { 136 | scanf("%99s", s); 137 | return s; 138 | } 139 | 140 | int main() 141 | { 142 | //Read the file dictionary 143 | int word_count = 0; 144 | char* words[NUMBER_OF_WORDS]; 145 | FILE *fp = fopen("dictionary.txt", "r"); 146 | 147 | if (fp == 0) 148 | { 149 | fprintf(stderr, "Error while opening dictionary file"); 150 | exit(1); 151 | } 152 | 153 | words[word_count] = malloc(INPUT_WORD_SIZE); 154 | 155 | while (fgets(words[word_count], INPUT_WORD_SIZE, fp)) 156 | { 157 | word_count++; 158 | words[word_count] = malloc(INPUT_WORD_SIZE); 159 | } 160 | 161 | 162 | //Push the words in to Trie 163 | TrieNode *root = NULL; 164 | root = createTrieNode(); 165 | int i; 166 | for(i=0;i 6 | #include 7 | #include 8 | 9 | #define NARRAY 8 /* array size */ 10 | #define NBUCKET 5 /* bucket size */ 11 | #define INTERVAL 10 /* bucket range */ 12 | 13 | struct Node 14 | { 15 | int data; 16 | struct Node *next; 17 | }; 18 | 19 | void BucketSort(int arr[]); 20 | struct Node *InsertionSort(struct Node *list); 21 | void print(int arr[]); 22 | void printBuckets(struct Node *list); 23 | int getBucketIndex(int value); 24 | 25 | void BucketSort(int arr[]) 26 | { 27 | int i,j; 28 | struct Node **buckets; 29 | 30 | /* allocate memory for array of pointers to the buckets */ 31 | buckets = (struct Node **)malloc(sizeof(struct Node*) * NBUCKET); 32 | 33 | /* initialize pointers to the buckets */ 34 | for(i = 0; i < NBUCKET; ++i) 35 | { 36 | buckets[i] = NULL; 37 | } 38 | 39 | /* put items into the buckets */ 40 | /* creates a link list in each bucket slot */ 41 | for(i = 0; i < NARRAY; ++i) 42 | { 43 | struct Node *current; 44 | int pos = getBucketIndex(arr[i]); 45 | current = (struct Node *) malloc(sizeof(struct Node)); 46 | current->data = arr[i]; 47 | current->next = buckets[pos]; 48 | buckets[pos] = current; 49 | } 50 | 51 | /* check what's in each bucket */ 52 | for(i = 0; i < NBUCKET; i++) 53 | { 54 | printf("Bucket[\"%d\"] : ", i); 55 | printBuckets(buckets[i]); 56 | printf("\n"); 57 | } 58 | 59 | /* sorting bucket using Insertion Sort */ 60 | for(i = 0; i < NBUCKET; ++i) 61 | { 62 | buckets[i] = InsertionSort(buckets[i]); 63 | } 64 | 65 | /* check what's in each bucket */ 66 | printf("--------------\n"); 67 | printf("Buckets after sorted\n"); 68 | for(i = 0; i < NBUCKET; i++) 69 | { 70 | printf("Bucket[\"%d\"] : ", i); 71 | printBuckets(buckets[i]); 72 | printf("\n"); 73 | } 74 | 75 | /* put items back to original array */ 76 | for(j =0, i = 0; i < NBUCKET; ++i) 77 | { 78 | struct Node *node; 79 | node = buckets[i]; 80 | while(node) 81 | { 82 | 83 | // precondition for avoiding out of bounds by the array 84 | assert(j < NARRAY); 85 | arr[j++] = node->data; 86 | node = node->next; 87 | } 88 | } 89 | 90 | /* free memory */ 91 | for(i = 0; i < NBUCKET; ++i) 92 | { 93 | struct Node *node; 94 | node = buckets[i]; 95 | while(node) 96 | { 97 | struct Node *tmp; 98 | tmp = node; 99 | node = node->next; 100 | free(tmp); 101 | } 102 | } 103 | free(buckets); 104 | return; 105 | } 106 | 107 | /* Insertion Sort */ 108 | struct Node *InsertionSort(struct Node *list) 109 | { 110 | struct Node *k,*nodeList; 111 | /* need at least two items to sort */ 112 | if(list == NULL || list->next == NULL) 113 | { 114 | return list; 115 | } 116 | 117 | nodeList = list; 118 | k = list->next; 119 | nodeList->next = NULL; /* 1st node is new list */ 120 | while(k != NULL) 121 | { 122 | struct Node *ptr; 123 | /* check if insert before first */ 124 | if(nodeList->data > k->data) 125 | { 126 | struct Node *tmp; 127 | tmp = k; 128 | k = k->next; // important for the while 129 | tmp->next = nodeList; 130 | nodeList = tmp; 131 | continue; 132 | } 133 | 134 | // from begin up to end 135 | // finds [i] > [i+1] 136 | for(ptr = nodeList; ptr->next != NULL; ptr = ptr->next) 137 | { 138 | if(ptr->next->data > k->data) break; 139 | } 140 | 141 | // if found (above) 142 | if(ptr->next != NULL) 143 | { 144 | struct Node *tmp; 145 | tmp = k; 146 | k = k->next; // important for the while 147 | tmp->next = ptr->next; 148 | ptr->next = tmp; 149 | continue; 150 | } 151 | else 152 | { 153 | ptr->next = k; 154 | k = k->next; // important for the while 155 | ptr->next->next = NULL; 156 | continue; 157 | } 158 | } 159 | return nodeList; 160 | } 161 | 162 | int getBucketIndex(int value) 163 | { 164 | return value/INTERVAL; 165 | } 166 | 167 | void print(int ar[]) 168 | { 169 | int i; 170 | for(i = 0; i < NARRAY; ++i) 171 | { 172 | printf("%d ", ar[i]); 173 | } 174 | printf("\n"); 175 | } 176 | 177 | void printBuckets(struct Node *list) 178 | { 179 | struct Node *cur = list; 180 | while(cur) 181 | { 182 | printf("%d ", cur->data); 183 | cur = cur->next; 184 | } 185 | } 186 | 187 | int main(void) 188 | { 189 | int array[NARRAY] = {29,25,-1,49,9,37,21,43}; 190 | 191 | printf("Initial array\n"); 192 | print(array); 193 | printf("------------\n"); 194 | 195 | BucketSort(array); 196 | printf("------------\n"); 197 | printf("Sorted array\n"); 198 | print(array); 199 | return 0; 200 | } 201 | -------------------------------------------------------------------------------- /Data Structures/Array/CArray.c: -------------------------------------------------------------------------------- 1 | /* 2 | * CArray.c 3 | * 4 | * Author: Leonardo Vencovsky 5 | * Created on 19/03/2018 6 | * 7 | * Modified by: Leonardo Vencovsky 8 | * Last modified: 19/03/2018 9 | * 10 | * Array Implementations in C 11 | * 12 | * Compiled in Visual Studio 2017 13 | * 14 | */ 15 | 16 | /* 17 | Return Codes 18 | 19 | -1 - Array Erased 20 | 0 - Success 21 | 1 - Invalid Position 22 | 2 - Position already initialized (use update function) 23 | 3 - Position not initialized (use insert function) 24 | 4 - Position already empty 25 | 5 - Array is full 26 | 27 | */ 28 | 29 | #include 30 | #include 31 | #include 32 | #include "CArray.h" 33 | 34 | void swap(CArray *array, int position1, int position2); 35 | 36 | CArray * getCArray(int size) 37 | { 38 | CArray *array = (CArray *) malloc(sizeof(CArray)); 39 | array->array = (int *) malloc(sizeof(int) * size); 40 | array->size = size; 41 | int i; 42 | for (i = 0; i < size; i++) { 43 | array->array[i] = 0; 44 | } 45 | return array; 46 | } 47 | 48 | int insertValueCArray(CArray *array, int position, int value) 49 | { 50 | if (position >= 0 && position < array->size) { 51 | if (array->array[position] == 0) { 52 | array->array[position] = value; 53 | return SUCCESS; 54 | } 55 | else return POSITION_INIT; 56 | } 57 | return INVALID_POSITION; 58 | } 59 | 60 | int removeValueCArray(CArray *array, int position) 61 | { 62 | if (position >= 0 && position < array->size) { 63 | if (array->array[position] != 0) { 64 | array->array[position] = 0; 65 | } 66 | else return POSITION_EMPTY; 67 | } 68 | return INVALID_POSITION; 69 | } 70 | 71 | int pushValueCArray(CArray *array, int value) 72 | { 73 | int i; 74 | int ok = 0; 75 | for (i = 0; i < array->size; i++) { 76 | if (array->array[i] == 0) { 77 | array->array[i] = value; 78 | ok = 1; 79 | break; 80 | } 81 | } 82 | if (ok == 1) return SUCCESS; 83 | else return ARRAY_FULL; 84 | } 85 | 86 | int updateValueCArray(CArray *array, int position, int value) 87 | { 88 | if (position >= 0 && position < array->size) { 89 | if (array->array[position] != 0) { 90 | 91 | } 92 | 93 | else return POSITION_NOT_INIT; 94 | } 95 | return INVALID_POSITION; 96 | } 97 | 98 | int eraseCArray(CArray *array) 99 | { 100 | int i; 101 | for (i = 0; i < array->size; i++) { 102 | array->array[i] = 0; 103 | } 104 | return 0; 105 | } 106 | 107 | int switchValuesCArray(CArray *array, int position1, int position2) 108 | { 109 | if (position1 >= 0 && position1 < array->size 110 | && position2 >= 0 && position2 < array->size) { 111 | int temp = array->array[position1]; 112 | array->array[position1] = array->array[position2]; 113 | array->array[position2] = temp; 114 | } 115 | return INVALID_POSITION; 116 | } 117 | 118 | int reverseCArray(CArray *array) 119 | { 120 | int i; 121 | for (i = 0; i < array->size / 2; i++) { 122 | swap(array, i, array->size - i - 1); 123 | } 124 | return SUCCESS; 125 | } 126 | 127 | int displayCArray(CArray *array) 128 | { 129 | int i; 130 | printf("\nC ARRAY\n"); 131 | for (i = 0; i < array->size; i++) { 132 | printf("%d ", array->array[i]); 133 | } 134 | printf("\n"); 135 | return 0; 136 | } 137 | 138 | int blenderCArray(CArray *array) 139 | { 140 | srand(time(NULL) * array->size); 141 | int i; 142 | int total = array->size * 100; 143 | for (i = 0; i < total; i++) { 144 | swap(array, rand() % array->size, rand() % array->size); 145 | } 146 | return 0; 147 | } 148 | 149 | CArray * getCopyCArray(CArray *arr) 150 | { 151 | CArray *array = (CArray *)malloc(sizeof(CArray)); 152 | array->array = (int *)malloc(sizeof(int) * arr->size); 153 | array->size = arr->size; 154 | int i; 155 | for (i = 0; i < arr->size; i++) { 156 | array->array[i] = arr->array[i]; 157 | } 158 | return array; 159 | } 160 | 161 | void swap(CArray *array, int position1, int position2) 162 | { 163 | int temp = array->array[position1]; 164 | array->array[position1] = array->array[position2]; 165 | array->array[position2] = temp; 166 | } 167 | 168 | int bubbleSortCArray(CArray *array) 169 | { 170 | int i, j; 171 | for (i = 0; i < array->size - 1; i++) { 172 | for (j = 0; j < array->size - i - 1; j++) { 173 | if (array->array[j] > array->array[j + 1]) { 174 | swap(array, j, j + 1); 175 | } 176 | } 177 | } 178 | return 0; 179 | } 180 | 181 | int selectionSortCArray(CArray *array) 182 | { 183 | int i, j, min; 184 | for (i = 0; i < array->size - 1; i++) { 185 | min = i; 186 | for (j = i + 1; j < array->size; j++) 187 | if (array->array[j] < array->array[min]) min = j; 188 | swap(array, min, i); 189 | } 190 | return 0; 191 | } 192 | 193 | int insertionSortCArray(CArray *array) 194 | { 195 | int i, j, num; 196 | for (i = 1; i < array->size; i++) { 197 | num = array->array[i]; 198 | j = i - 1; 199 | while (j >= 0 && array->array[j] > num) 200 | { 201 | array->array[j + 1] = array->array[j]; 202 | j--; 203 | } 204 | array->array[j + 1] = num; 205 | } 206 | return 0; 207 | } 208 | 209 | int valueOcurranceCArray(CArray *array, int value) 210 | { 211 | int i, total = 0; 212 | for (i = 0; i < array->size; i++) { 213 | if (array->array[i] == value) total++; 214 | } 215 | return total; 216 | } 217 | 218 | CArray * valuePositionsCArray(CArray *array, int value) 219 | { 220 | int i, j = 0; 221 | int total = valueOcurranceCArray(array, value); 222 | CArray *resultArray = getCArray(total); 223 | for (i = 0; i < array->size; i++) { 224 | if (array->array[i] == value) { 225 | // Hopefully this won't overflow 226 | resultArray->array[j] = i; 227 | j++; 228 | } 229 | } 230 | return resultArray; 231 | } 232 | 233 | int findMinCArray(CArray *array) 234 | { 235 | int i; 236 | int min = array->array[0]; 237 | for (i = 1; i < array->size; i++) { 238 | if (array->array[i] < min) { 239 | min = array->array[i]; 240 | } 241 | } 242 | return min; 243 | } 244 | 245 | int findMaxCArray(CArray *array) 246 | { 247 | int i; 248 | int max = array->array[0]; 249 | for (i = 1; i < array->size; i++) { 250 | if (array->array[i] > max) { 251 | max = array->array[i]; 252 | } 253 | } 254 | return max; 255 | } 256 | -------------------------------------------------------------------------------- /Computer Oriented Statistical Methods/statistic/test/test.c: -------------------------------------------------------------------------------- 1 | /* 2 | author: Christian Bender 3 | This file contains a CUnit test suit for the statistic-library 4 | */ 5 | 6 | #include 7 | #include 8 | #include "CUnit/Basic.h" 9 | 10 | #include "../src/statistic.h" 11 | 12 | /* test for function average(...) */ 13 | void test_average(void) 14 | { 15 | CU_ASSERT_DOUBLE_EQUAL(average(3,1.0,2.5,3.5),2.333,0.01); 16 | } 17 | 18 | /* test for function averageArray(...) */ 19 | void test_average_Array(void) 20 | { 21 | double values[] = {1.0, 2.5, 3.5}; 22 | CU_ASSERT_DOUBLE_EQUAL(average_Array(3, values), 2.333, 0.01); 23 | } 24 | 25 | /* test for function standard_deviation(...) */ 26 | void test_standard_deviation(void) 27 | { 28 | CU_ASSERT_DOUBLE_EQUAL(standard_deviation(4, 15.0, 70.0, 25.0, 50.0), 24.8328, 0.01); 29 | } 30 | 31 | /* test for function standard_deviation_array() */ 32 | void test_standard_deviation_array(void) 33 | { 34 | double values[] = {15.0, 70.0, 25.0, 50.0}; 35 | CU_ASSERT_DOUBLE_EQUAL(standard_deviation_array(4, values), 24.8328, 0.01); 36 | } 37 | 38 | /* test for function standard_deviation_N(...) */ 39 | void test_standard_deviation_N(void) 40 | { 41 | CU_ASSERT_DOUBLE_EQUAL(standard_deviation_N(4, 15.0, 70.0, 25.0, 50.0), 21.5058, 0.01); 42 | } 43 | 44 | /* test for function standard_deviation_N_array() */ 45 | void test_standard_deviation_N_array(void) 46 | { 47 | double values[] = {15.0, 70.0, 25.0, 50.0}; 48 | CU_ASSERT_DOUBLE_EQUAL(standard_deviation_N_array(4, values), 21.5058, 0.01); 49 | } 50 | 51 | /* test for the function variance(...) */ 52 | void test_variance(void) 53 | { 54 | double values[] = {15.0, 70.0, 25.0, 50.0}; 55 | CU_ASSERT_DOUBLE_EQUAL(variance(4, values), 616.6667, 0.01); 56 | } 57 | 58 | /* test for the function variance(...) */ 59 | void test_variance_N(void) 60 | { 61 | double values[] = {15.0, 70.0, 25.0, 50.0}; 62 | CU_ASSERT_DOUBLE_EQUAL(variance_N(4, values), 462.5, 0.01); 63 | } 64 | 65 | /* test for the max(...) function */ 66 | void test_max(void) 67 | { 68 | double values[] = {15.0, 70.0, 25.0, 50.0}; 69 | CU_ASSERT_DOUBLE_EQUAL(max(4, values), 70.0, 0.01); 70 | } 71 | 72 | /* test for the min(...) function */ 73 | void test_min(void) 74 | { 75 | double values[] = {15.0, 70.0, 25.0, 50.0}; 76 | CU_ASSERT_DOUBLE_EQUAL(min(4, values), 15.0, 0.01); 77 | } 78 | 79 | /* test for the median(...)-function */ 80 | void test_median(void) 81 | { 82 | double values[] = {15.0, 70.0, 25.0, 50.0}; 83 | CU_ASSERT_DOUBLE_EQUAL(median(4, values), 37.5, 0.01); 84 | } 85 | 86 | 87 | /* test for the sum(...)-function */ 88 | void test_sum(void) 89 | { 90 | double values[] = {15.0, 70.0, 25.0, 50.0}; 91 | CU_ASSERT_DOUBLE_EQUAL(sum(4, values), 160, 0.01); 92 | } 93 | 94 | 95 | /* test for the range(...)-function */ 96 | void test_range(void) 97 | { 98 | double values[] = {15.0, 70.0, 25.0, 50.0}; 99 | CU_ASSERT_DOUBLE_EQUAL(range(4, values), 55, 0.01); 100 | } 101 | 102 | 103 | /* test of frequency_of(...)-function */ 104 | void test_frequency_of(void) 105 | { 106 | double values[] = {1.0,7.0,2.5,2.5,6.0}; 107 | CU_ASSERT_DOUBLE_EQUAL(frequency_of(5, values,2.5), 0.4, 0.01); 108 | CU_ASSERT_DOUBLE_EQUAL(frequency_of(5, values,6.0), 0.2, 0.01); 109 | } 110 | 111 | 112 | /* test of quartile_I(...) and quartile_III(...)-function */ 113 | void test_quartile(void) 114 | { 115 | double values[] = {3.0,4.0,5.0,7.0,7.0,7.0,8.0,9.0,11.0,13.0,13.0,13.0,15.0,16.0}; 116 | double sample[] = {1600.0,2300.0,2300.0,2400.0,2900.0,3200,3500,4500,4600,5200,6500,12000}; 117 | CU_ASSERT_DOUBLE_EQUAL(quartile_I(14, values), 7.0, 0.01); 118 | CU_ASSERT_DOUBLE_EQUAL(quartile_III(14, values), 13.0, 0.01); 119 | CU_ASSERT_DOUBLE_EQUAL(quartile_III(12, sample), 4900.0, 0.01); 120 | } 121 | 122 | 123 | /* test for quartile_distance(...)-function */ 124 | void test_quartile_distance(void) 125 | { 126 | double values[] = {3.0,4.0,5.0,7.0,7.0,7.0,8.0,9.0,11.0,13.0,13.0,13.0,15.0,16.0}; 127 | CU_ASSERT_DOUBLE_EQUAL(quartile_distance(14, values), 6.0, 0.01); 128 | } 129 | 130 | /* 131 | init suite 132 | */ 133 | int init_suite1(void) 134 | { 135 | return 0; 136 | } 137 | 138 | /* 139 | clean suite 140 | */ 141 | int clean_suite1(void) 142 | { 143 | return 0; 144 | } 145 | 146 | /* test runner */ 147 | int main(void) 148 | { 149 | CU_pSuite pSuite = NULL; 150 | 151 | /* initializes CUnit */ 152 | if (CUE_SUCCESS != CU_initialize_registry()) 153 | return CU_get_error(); 154 | 155 | /* adds the suit "Test for statistic" to the registry */ 156 | pSuite = CU_add_suite("Test for statistic", init_suite1, clean_suite1); 157 | if (NULL == pSuite) 158 | { 159 | CU_cleanup_registry(); 160 | return CU_get_error(); 161 | } 162 | 163 | /* registers the individual tests to the test-suite */ 164 | if ((NULL == CU_add_test(pSuite, "test of average()", test_average)) 165 | || (NULL == CU_add_test(pSuite, "test of average_Array()", test_average_Array)) 166 | || (NULL == CU_add_test(pSuite, "test of standard_deviation()", test_standard_deviation)) 167 | || (NULL == CU_add_test(pSuite, "test of standard_deviation_array()", test_standard_deviation_array)) 168 | || (NULL == CU_add_test(pSuite, "test of standard_deviation_N_array()", test_standard_deviation_N_array)) 169 | || (NULL == CU_add_test(pSuite, "test of standard_deviation_N()", test_standard_deviation_N)) 170 | || (NULL == CU_add_test(pSuite, "test of variance()", test_variance)) 171 | || (NULL == CU_add_test(pSuite, "test of variance_N()", test_variance_N)) 172 | || (NULL == CU_add_test(pSuite, "test of max()", test_max)) 173 | || (NULL == CU_add_test(pSuite, "test of min()", test_min)) 174 | || (NULL == CU_add_test(pSuite, "test of median()", test_median)) 175 | || (NULL == CU_add_test(pSuite, "test of sum()", test_sum)) 176 | || (NULL == CU_add_test(pSuite, "test of range()", test_range)) 177 | || (NULL == CU_add_test(pSuite, "test of frequency_of()", test_frequency_of)) 178 | || (NULL == CU_add_test(pSuite, "test of quartile_I() and quartile_III()", test_quartile)) 179 | || (NULL == CU_add_test(pSuite, "test of quartile_distance()", test_quartile_distance))) 180 | { 181 | CU_cleanup_registry(); 182 | return CU_get_error(); 183 | } 184 | 185 | /* runs tests */ 186 | CU_basic_set_mode(CU_BRM_VERBOSE); 187 | CU_basic_run_tests(); 188 | CU_cleanup_registry(); 189 | return CU_get_error(); 190 | } -------------------------------------------------------------------------------- /data_structures/binary_trees/binary_search_tree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* A basic unbalanced binary search tree implementation in C, with the following functionalities implemented: 5 | - Insertion 6 | - Deletion 7 | - Search by key value 8 | - Listing of node keys in order of value (from left to right) 9 | */ 10 | 11 | // Node, the basic data structure in the tree 12 | typedef struct node{ 13 | 14 | // left child 15 | struct node* left; 16 | 17 | // right child 18 | struct node* right; 19 | 20 | // data of the node 21 | int data; 22 | } node; 23 | 24 | // The node constructor, which receives the key value input and returns a node pointer 25 | node* newNode(int data){ 26 | 27 | // creates a slug 28 | node* tmp = (node*)malloc(sizeof(node)); 29 | 30 | // initializes the slug 31 | tmp->data = data; 32 | tmp->left = NULL; 33 | tmp->right = NULL; 34 | 35 | return tmp; 36 | } 37 | 38 | // Insertion procedure, which inserts the input key in a new node in the tree 39 | node* insert(node* root, int data){ 40 | // If the root of the subtree is null, insert key here 41 | if (root == NULL) 42 | root = newNode(data); 43 | // If it isn't null and the input key is greater than the root key, insert in the right leaf 44 | else if (data > root->data) 45 | root->right = insert(root->right, data); 46 | // If it isn't null and the input key is lower than the root key, insert in the left leaf 47 | else if (data < root->data) 48 | root->left = insert(root->left, data); 49 | // Returns the modified tree 50 | return root; 51 | } 52 | 53 | // Utilitary procedure to find the greatest key in the left subtree 54 | node* getMax(node* root){ 55 | // If there's no leaf to the right, then this is the maximum key value 56 | if (root->right == NULL) 57 | return root; 58 | else 59 | root->right = getMax(root->right); 60 | } 61 | 62 | // Deletion procedure, which searches for the input key in the tree and removes it if present 63 | node* delete(node* root, int data){ 64 | // If the root is null, nothing to be done 65 | if (root == NULL) 66 | return root; 67 | // If the input key is greater than the root's, search in the right subtree 68 | else if (data > root->data) 69 | root->right = delete(root->right, data); 70 | // If the input key is lower than the root's, search in the left subtree 71 | else if (data < root->data) 72 | root->left = delete(root->left, data); 73 | // If the input key matches the root's, check the following cases 74 | // termination condition 75 | else if (data == root->data){ 76 | // Case 1: the root has no leaves, remove the node 77 | if ((root->left == NULL) && (root->right == NULL)){ 78 | free(root); 79 | return NULL; 80 | } 81 | // Case 2: the root has one leaf, make the leaf the new root and remove the old root 82 | else if (root->left == NULL){ 83 | node* tmp = root; 84 | root = root->right; 85 | free(tmp); 86 | return root; 87 | } 88 | else if (root->right == NULL){ 89 | node* tmp = root; 90 | root = root->left; 91 | free(tmp); 92 | return root; 93 | } 94 | // Case 3: the root has 2 leaves, find the greatest key in the left subtree and switch with the root's 95 | else { 96 | 97 | // finds the biggest node in the left branch. 98 | node* tmp = getMax(root->left); 99 | 100 | // sets the data of this node equal to the data of the biggest node (lefts) 101 | root->data = tmp->data; 102 | root->left = delete(root->left, tmp->data); 103 | } 104 | } 105 | return root; 106 | } 107 | 108 | // Search procedure, which looks for the input key in the tree and returns 1 if it's present or 0 if it's not in the tree 109 | int find(node* root, int data){ 110 | // If the root is null, the key's not present 111 | if (root == NULL) 112 | return 0; 113 | // If the input key is greater than the root's, search in the right subtree 114 | else if (data > root->data) 115 | return find(root->right, data); 116 | // If the input key is lower than the root's, search in the left subtree 117 | else if (data < root->data) 118 | return find(root->left, data); 119 | // If the input and the root key match, return 1 120 | else if (data == root->data) 121 | return 1; 122 | } 123 | 124 | // Utilitary procedure to measure the height of the binary tree 125 | int height(node* root){ 126 | // If the root is null, this is the bottom of the tree (height 0) 127 | if (root == NULL) 128 | return 0; 129 | else{ 130 | // Get the height from both left and right subtrees to check which is the greatest 131 | int right_h = height(root->right); 132 | int left_h = height(root->left); 133 | 134 | // The final height is the height of the greatest subtree(left or right) plus 1(which is the root's level) 135 | if (right_h > left_h) 136 | return (right_h + 1); 137 | else 138 | return (left_h + 1); 139 | } 140 | } 141 | 142 | // Utilitary procedure to free all nodes in a tree 143 | void purge(node* root){ 144 | if (root != NULL){ 145 | if (root->left != NULL) 146 | purge(root->left); 147 | if (root->right != NULL) 148 | purge(root->right); 149 | free(root); 150 | } 151 | } 152 | 153 | // Traversal procedure to list the current keys in the tree in order of value (from the left to the right) 154 | void inOrder(node* root){ 155 | if(root != NULL){ 156 | inOrder(root->left); 157 | printf("\t[ %d ]\t", root->data); 158 | inOrder(root->right); 159 | } 160 | } 161 | 162 | void main(){ 163 | 164 | // this reference don't change. 165 | // only the tree changes. 166 | node* root = NULL; 167 | int opt = -1; 168 | int data = 0; 169 | 170 | // event-loop. 171 | while (opt != 0){ 172 | printf("\n\n[1] Insert Node\n[2] Delete Node\n[3] Find a Node\n[4] Get current Height\n[5] Print Tree in Crescent Order\n[0] Quit\n"); 173 | scanf("%d",&opt); // reads the choice of the user 174 | 175 | // processes the choice 176 | switch(opt){ 177 | case 1: printf("Enter the new node's value:\n"); 178 | scanf("%d",&data); 179 | root = insert(root,data); 180 | break; 181 | 182 | case 2: printf("Enter the value to be removed:\n"); 183 | if (root != NULL){ 184 | scanf("%d",&data); 185 | root = delete(root,data); 186 | } 187 | else 188 | printf("Tree is already empty!\n"); 189 | break; 190 | 191 | case 3: printf("Enter the searched value:\n"); 192 | scanf("%d",&data); 193 | find(root,data) ? printf("The value is in the tree.\n") : printf("The value is not in the tree.\n"); 194 | break; 195 | 196 | case 4: printf("Current height of the tree is: %d\n", height(root)); 197 | break; 198 | 199 | case 5: inOrder(root); 200 | break; 201 | } 202 | } 203 | 204 | // deletes the tree from the heap. 205 | purge(root); 206 | } 207 | -------------------------------------------------------------------------------- /Computer Oriented Statistical Methods/statistic/src/statistic.c: -------------------------------------------------------------------------------- 1 | /* 2 | author: Christian Bender 3 | This file contains the implementation part of the statistic-library. 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "statistic.h" 12 | 13 | double average(int n, ...) 14 | { 15 | va_list valist; 16 | double sum = 0; 17 | int i; 18 | 19 | /* initializes valist for num number of arguments */ 20 | va_start(valist, n); 21 | 22 | /* adds up all double values of the sample. */ 23 | for (i = 0; i < n; i++) 24 | { 25 | 26 | sum += va_arg(valist, double); 27 | } 28 | 29 | /* cleans memory reserved for valist */ 30 | va_end(valist); 31 | 32 | return sum / n; 33 | } 34 | 35 | double average_Array(int n, const double values[]) 36 | { 37 | int i; 38 | double sum = 0; 39 | 40 | /* adds up all double values of the sample. */ 41 | for (i = 0; i < n; i++) 42 | { 43 | sum += values[i]; 44 | } 45 | 46 | return sum / n; 47 | } 48 | 49 | double standard_deviation(int n, ...) 50 | { 51 | va_list valist; 52 | double var = 0; 53 | double avg = 0; 54 | double value = 0; 55 | double values[n]; 56 | int i; 57 | 58 | /* initializes valist for num number of arguments */ 59 | va_start(valist, n); 60 | 61 | for (i = 0; i < n; i++) 62 | { 63 | values[i] = va_arg(valist, double); 64 | } 65 | 66 | va_end(valist); 67 | va_start(valist, n); 68 | 69 | /* fetches the average */ 70 | avg = average_Array(n, values); 71 | 72 | /* adds up all double values of the sample. */ 73 | for (i = 0; i < n; i++) 74 | { 75 | value = va_arg(valist, double); 76 | var += (value - avg) * (value - avg); 77 | } 78 | 79 | var /= (double)(n - 1); 80 | 81 | /* cleans memory reserved for valist */ 82 | va_end(valist); 83 | 84 | return sqrt(var); 85 | } 86 | 87 | double standard_deviation_array(int n, const double values[]) 88 | { 89 | 90 | double var = 0; 91 | double avg = 0; 92 | int i; 93 | 94 | /* fetches the average */ 95 | avg = average_Array(n, values); 96 | 97 | /* adds up all double values of the sample. */ 98 | for (i = 0; i < n; i++) 99 | { 100 | var += (values[i] - avg) * (values[i] - avg); 101 | } 102 | 103 | var /= (double)(n - 1); 104 | 105 | return sqrt(var); 106 | } 107 | 108 | double standard_deviation_N(int n, ...) 109 | { 110 | va_list valist; 111 | double var = 0; 112 | double avg = 0; 113 | double value = 0; 114 | double values[n]; 115 | int i; 116 | 117 | /* initializes valist for num number of arguments */ 118 | va_start(valist, n); 119 | 120 | for (i = 0; i < n; i++) 121 | { 122 | values[i] = va_arg(valist, double); 123 | } 124 | 125 | va_end(valist); 126 | va_start(valist, n); 127 | 128 | /* fetches the average */ 129 | avg = average_Array(n, values); 130 | 131 | /* adds up all double values of the sample. */ 132 | for (i = 0; i < n; i++) 133 | { 134 | value = va_arg(valist, double); 135 | var += (value - avg) * (value - avg); 136 | } 137 | 138 | var /= (double)n; 139 | 140 | /* cleans memory reserved for valist */ 141 | va_end(valist); 142 | 143 | return sqrt(var); 144 | } 145 | 146 | double standard_deviation_N_array(int n, const double values[]) 147 | { 148 | double var = 0; 149 | double avg = 0; 150 | int i; 151 | 152 | /* fetches the average */ 153 | avg = average_Array(n, values); 154 | 155 | /* adds up all double values of the sample. */ 156 | for (i = 0; i < n; i++) 157 | { 158 | var += (values[i] - avg) * (values[i] - avg); 159 | } 160 | 161 | var /= (double)n; 162 | 163 | return sqrt(var); 164 | } 165 | 166 | double variance(int n, const double values[]) 167 | { 168 | double var = 0; 169 | double avg = 0; 170 | int i; 171 | 172 | /* fetches the average */ 173 | avg = average_Array(n, values); 174 | 175 | /* adds up all double values of the sample. */ 176 | for (i = 0; i < n; i++) 177 | { 178 | var += (values[i] - avg) * (values[i] - avg); 179 | } 180 | 181 | var /= (double)(n - 1); 182 | 183 | return var; 184 | } 185 | 186 | double variance_N(int n, const double values[]) 187 | { 188 | double var = 0; 189 | double avg = 0; 190 | int i; 191 | 192 | /* fetches the average */ 193 | avg = average_Array(n, values); 194 | 195 | /* adds up all double values of the sample. */ 196 | for (i = 0; i < n; i++) 197 | { 198 | var += (values[i] - avg) * (values[i] - avg); 199 | } 200 | 201 | var /= (double)n; 202 | 203 | return var; 204 | } 205 | 206 | double max(int n, const double values[]) 207 | { 208 | double max = values[0]; 209 | int i; 210 | 211 | /* iterates over all elements in 'values' */ 212 | for (i = 1; i < n; i++) 213 | { 214 | if (values[i] > max) 215 | { 216 | max = values[i]; 217 | } 218 | } 219 | 220 | return max; 221 | } 222 | 223 | double min(int n, const double values[]) 224 | { 225 | double min = values[0]; 226 | int i; 227 | 228 | /* iterates over all elements in 'values' */ 229 | for (i = 1; i < n; i++) 230 | { 231 | if (values[i] < min) 232 | { 233 | min = values[i]; 234 | } 235 | } 236 | 237 | return min; 238 | } 239 | 240 | /* 241 | private helper-function for comparing two double values 242 | */ 243 | int cmp(const void *a, const void *b) 244 | { 245 | return (*(double *)a - *(double *)b); 246 | } 247 | 248 | double median(int n, const double values[]) 249 | { 250 | double tmp[n]; 251 | int i; 252 | 253 | /* clones the array 'values' to array 'tmp' */ 254 | for (i = 0; i < n; i++) 255 | { 256 | tmp[i] = values[i]; 257 | } 258 | 259 | /* sorts the array 'tmp' with quicksort from stdlib.h */ 260 | qsort(tmp, n, sizeof(double), cmp); 261 | 262 | if (n % 2 != 0) /* n is odd */ 263 | { 264 | /* integer division */ 265 | return tmp[n / 2]; 266 | } 267 | else 268 | { /* n is even */ 269 | 270 | /* uses the average(...) function, above. */ 271 | return average(2, tmp[n / 2], tmp[(n / 2) - 1]); 272 | } 273 | } 274 | 275 | double sum(int n, const double values[]) 276 | { 277 | double sum = 0; 278 | int i; 279 | 280 | /* actual adding procedure */ 281 | for (i = 0; i < n; i++) 282 | { 283 | sum += values[i]; 284 | } 285 | 286 | return sum; 287 | } 288 | 289 | double range(int n, const double values[]) 290 | { 291 | return max(n, values) - min(n, values); 292 | } 293 | 294 | double frequency_of(int n, const double values[], double val) 295 | { 296 | int i; 297 | double counter = 0; 298 | 299 | /* counts the number of occurs */ 300 | for (i = 0; i < n; i++) 301 | { 302 | if (values[i] == val) 303 | { 304 | counter++; 305 | } 306 | } 307 | 308 | return counter / n; 309 | } 310 | 311 | double quartile_I(int n, const double values[]) 312 | { 313 | double sum = 0; 314 | double freq = 0; 315 | int i; 316 | int d = 1; 317 | double tmp[n]; 318 | 319 | for (i = 0; i < n; i++) 320 | { 321 | tmp[i] = values[i]; 322 | } 323 | 324 | /* sorts the array 'tmp' with quicksort from stdlib.h */ 325 | qsort(tmp, n, sizeof(double), cmp); 326 | 327 | double lastVal = tmp[0]; 328 | 329 | freq = frequency_of(n, values, lastVal); 330 | sum += freq; 331 | 332 | for (i = 1; i < n; i++) 333 | { 334 | if (tmp[i] != lastVal) 335 | { 336 | freq = frequency_of(n, values, tmp[i]); 337 | sum += freq; 338 | lastVal = tmp[i]; 339 | if (sum >= 0.25) 340 | { 341 | if (n % 2 != 0) 342 | { 343 | return values[i]; 344 | } 345 | else 346 | { 347 | 348 | return average(2, values[i], values[i + 1]); 349 | } 350 | } 351 | } 352 | } 353 | } 354 | 355 | double quartile_III(int n, const double values[]) 356 | { 357 | double sum = 0; 358 | double freq = 0; 359 | int i; 360 | double tmp[n]; 361 | 362 | for (i = 0; i < n; i++) 363 | { 364 | tmp[i] = values[i]; 365 | } 366 | 367 | /* sorts the array 'tmp' with quicksort from stdlib.h */ 368 | qsort(tmp, n, sizeof(double), cmp); 369 | 370 | double lastVal = tmp[0]; 371 | 372 | freq = frequency_of(n, values, lastVal); 373 | sum += freq; 374 | 375 | for (i = 1; i < n; i++) 376 | { 377 | if (tmp[i] != lastVal) 378 | { 379 | freq = frequency_of(n, values, tmp[i]); 380 | sum += freq; 381 | lastVal = tmp[i]; 382 | if (sum >= 0.75) 383 | { 384 | if (n % 2 != 0) 385 | { 386 | return values[i]; 387 | } 388 | else 389 | { 390 | return average(2, values[i], values[i + 1]); 391 | } 392 | } 393 | } 394 | } 395 | } 396 | 397 | double quartile_distance(int n, const double values[]) 398 | { 399 | return quartile_III(n, values) - quartile_I(n, values); 400 | } --------------------------------------------------------------------------------