From 12e16bb66dc3aa9411c108bbff02b0b9a9e99aaa Mon Sep 17 00:00:00 2001 From: Greg Skinner Date: Sun, 4 Apr 2021 10:49:40 -0700 Subject: [PATCH 01/15] Update Factorial.c --- Recursion programs/Factorial.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/Recursion programs/Factorial.c b/Recursion programs/Factorial.c index 4ff1776..14e684a 100644 --- a/Recursion programs/Factorial.c +++ b/Recursion programs/Factorial.c @@ -1,23 +1,23 @@ -/*Program to find the factorial of a number by recursive method*/ +/*Program to find the factorial of a number by recursive and iterative methods*/ #include +#include long int fact(int n); long int Ifact(int n); -main( ) +int main( ) { int num; printf("Enter a number : "); scanf("%d", &num); - if(num<0) + if(num<0) { printf("No factorial for negative number\n"); - else - printf("Factorial of %d is %ld\n", num, fact(num) ); + exit(1); + } - if(num<0) - printf("No factorial for negative number\n"); - else - printf("Factorial of %d is %ld\n", num, Ifact(num) ); + printf("Factorial of %d is %ld\n", num, fact(num) ); + + printf("Factorial of %d is %ld\n", num, Ifact(num) ); }/*End of main()*/ /*Recursive*/ @@ -39,4 +39,3 @@ long int Ifact(int n) } return fact; }/*End of ifact()*/ - From f2581e13b1eeb5cac1d6fc4ccc29dccfb34ad1bf Mon Sep 17 00:00:00 2001 From: Greg Skinner Date: Sun, 4 Apr 2021 22:41:12 -0700 Subject: [PATCH 02/15] Delete Sort Bubble.c updated version coming --- Sorting/Sort Bubble.c | 42 ------------------------------------------ 1 file changed, 42 deletions(-) delete mode 100644 Sorting/Sort Bubble.c diff --git a/Sorting/Sort Bubble.c b/Sorting/Sort Bubble.c deleted file mode 100644 index 7b4a1eb..0000000 --- a/Sorting/Sort Bubble.c +++ /dev/null @@ -1,42 +0,0 @@ -/*Program of sorting using bubble sort*/ - -#include -#define MAX 100 - -main() -{ - int arr[MAX],i,j,temp,n,xchanges; - - printf("Enter the number of elements : "); - scanf("%d",&n); - - for(i=0; i arr[j+1]) - { - temp = arr[j]; - arr[j] = arr[j+1]; - arr[j+1] = temp; - xchanges++; - } - } - if(xchanges==0) /*If list is sorted*/ - break; - } - - printf("Sorted list is :\n"); - for(i=0; i Date: Sun, 4 Apr 2021 22:54:20 -0700 Subject: [PATCH 03/15] Add files via upload --- Sorting/Sort Bubble.c | 49 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Sorting/Sort Bubble.c diff --git a/Sorting/Sort Bubble.c b/Sorting/Sort Bubble.c new file mode 100644 index 0000000..4a9c93d --- /dev/null +++ b/Sorting/Sort Bubble.c @@ -0,0 +1,49 @@ +/*Program of sorting using bubble sort*/ + +#include +#include +#define MAX 100 + +int main() +{ + int arr[MAX],i,j,temp,n,xchanges; + + printf("Enter the number of elements : "); + scanf("%d",&n); + + if(n > MAX) + { + printf("number of elements must not exceed %d\n", MAX); + exit(1); + } + + for(i=0; i arr[j+1]) + { + temp = arr[j]; + arr[j] = arr[j+1]; + arr[j+1] = temp; + xchanges++; + } + } + if(xchanges==0) /*If list is sorted*/ + break; + } + + printf("Sorted list is :\n"); + for(i=0; i Date: Sun, 4 Apr 2021 23:02:44 -0700 Subject: [PATCH 04/15] Add files via upload From 2a030591e160d0380d056abc832ef9ac1268fb06 Mon Sep 17 00:00:00 2001 From: Greg Skinner Date: Sun, 4 Apr 2021 23:04:01 -0700 Subject: [PATCH 05/15] Delete Sort Bubble.c prepare to undo previous delete --- Sorting/Sort Bubble.c | 49 ------------------------------------------- 1 file changed, 49 deletions(-) delete mode 100644 Sorting/Sort Bubble.c diff --git a/Sorting/Sort Bubble.c b/Sorting/Sort Bubble.c deleted file mode 100644 index 4a9c93d..0000000 --- a/Sorting/Sort Bubble.c +++ /dev/null @@ -1,49 +0,0 @@ -/*Program of sorting using bubble sort*/ - -#include -#include -#define MAX 100 - -int main() -{ - int arr[MAX],i,j,temp,n,xchanges; - - printf("Enter the number of elements : "); - scanf("%d",&n); - - if(n > MAX) - { - printf("number of elements must not exceed %d\n", MAX); - exit(1); - } - - for(i=0; i arr[j+1]) - { - temp = arr[j]; - arr[j] = arr[j+1]; - arr[j+1] = temp; - xchanges++; - } - } - if(xchanges==0) /*If list is sorted*/ - break; - } - - printf("Sorted list is :\n"); - for(i=0; i Date: Mon, 5 Apr 2021 00:00:17 -0700 Subject: [PATCH 06/15] Create Sort Bubble.c --- Sorting/Sort Bubble.c | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Sorting/Sort Bubble.c diff --git a/Sorting/Sort Bubble.c b/Sorting/Sort Bubble.c new file mode 100644 index 0000000..37f70e9 --- /dev/null +++ b/Sorting/Sort Bubble.c @@ -0,0 +1,48 @@ +/*Program of sorting using bubble sort*/ + +#include +#include +#define MAX 100 + +int main() +{ + int arr[MAX],i,j,temp,n,xchanges; + + printf("Enter the number of elements : "); + scanf("%d",&n); + + if(n > MAX) + { + printf("number of elements must not exceed %d\n", MAX); + exit(1); + } + + for(i=0; i arr[j+1]) + { + temp = arr[j]; + arr[j] = arr[j+1]; + arr[j+1] = temp; + xchanges++; + } + } + if(xchanges==0) /*If list is sorted*/ + break; + } + + printf("Sorted list is :\n"); + for(i=0; i Date: Mon, 5 Apr 2021 14:48:12 -0700 Subject: [PATCH 07/15] Update Input and add n numbers.c --- Recursion programs/Input and add n numbers.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Recursion programs/Input and add n numbers.c b/Recursion programs/Input and add n numbers.c index e58b548..670171a 100644 --- a/Recursion programs/Input and add n numbers.c +++ b/Recursion programs/Input and add n numbers.c @@ -1,21 +1,26 @@ /* Input and add n numbers*/ #include +#include int InputAndAdd(int n); -main() +int main() { int n; printf("Enter n :"); scanf("%d",&n); + if (n < 1) + { + printf("Input must be a positive integer\n"); + exit(1); + } printf("%d\n",InputAndAdd(n)); } int InputAndAdd(int n) { int a; printf("Enter a number : "); - scanf("%d",&a); + scanf("%d",&a); if (n == 1) return a; else return a + InputAndAdd(n-1); } - From fcbeb3d60c370aa048f3e03d61ae214c8cb3f0f4 Mon Sep 17 00:00:00 2001 From: Greg Skinner Date: Tue, 6 Apr 2021 22:53:52 -0700 Subject: [PATCH 08/15] Update Even Sum.C --- Recursion programs/Even Sum.C | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Recursion programs/Even Sum.C b/Recursion programs/Even Sum.C index badc4eb..fcb5210 100644 --- a/Recursion programs/Even Sum.C +++ b/Recursion programs/Even Sum.C @@ -1,10 +1,10 @@ /* Sum of all even numbers in an array */ #include int sumEven(int arr[], int size); -main( ) +int main( ) { int arr[6]={1,2,3,4,8,10}; - printf("%d\n",sumEven(arr,6)); + printf("%d\n",sumEven(arr,sizeof(arr)/sizeof(int))); } int sumEven(int arr[], int size) { From a9fc2d9fdce7502b08c8413cabb6fcf6eecbdf55 Mon Sep 17 00:00:00 2001 From: Greg Skinner Date: Wed, 7 Apr 2021 23:09:37 -0700 Subject: [PATCH 09/15] Update Number in Words.C --- Recursion programs/Number in Words.C | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/Recursion programs/Number in Words.C b/Recursion programs/Number in Words.C index b0c3716..59ef743 100644 --- a/Recursion programs/Number in Words.C +++ b/Recursion programs/Number in Words.C @@ -3,6 +3,8 @@ #include void f(int n); +const char* nums[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; + int main() { int num = 12340; @@ -16,17 +18,5 @@ void f(int n) f(n/10); - switch(n%10) - { - case 0: printf("zero ");break; - case 1: printf("one ");break; - case 2: printf("two ");break; - case 3: printf("three ");break; - case 4: printf("four ");break; - case 5: printf("five ");break; - case 6: printf("six ");break; - case 7: printf("seven ");break; - case 8: printf("eight ");break; - case 9: printf("nine ");break; - } + printf("%s ", nums[n%10]); } From e708c1aaed60a566b679423bbb49bc35241bb1ee Mon Sep 17 00:00:00 2001 From: Greg Skinner Date: Thu, 8 Apr 2021 00:48:48 -0700 Subject: [PATCH 10/15] Update Number in Words.C --- Recursion programs/Number in Words.C | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Recursion programs/Number in Words.C b/Recursion programs/Number in Words.C index 59ef743..96d690c 100644 --- a/Recursion programs/Number in Words.C +++ b/Recursion programs/Number in Words.C @@ -3,7 +3,7 @@ #include void f(int n); -const char* nums[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; +const char* words[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; int main() { @@ -18,5 +18,5 @@ void f(int n) f(n/10); - printf("%s ", nums[n%10]); + printf("%s ", words[n%10]); } From 704f3aeb12ac6071f1611fc14bb5aa74b440901a Mon Sep 17 00:00:00 2001 From: Greg Skinner Date: Sun, 18 Apr 2021 14:17:21 -0700 Subject: [PATCH 11/15] Update Reverse of text line.C --- Recursion programs/Reverse of text line.C | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/Recursion programs/Reverse of text line.C b/Recursion programs/Reverse of text line.C index c92aeaa..1bc52ac 100644 --- a/Recursion programs/Reverse of text line.C +++ b/Recursion programs/Reverse of text line.C @@ -1,16 +1,31 @@ /* Enter a line of text and Reverse it*/ #include +#include + void func(void); -main( ) + +int main( ) { printf("Enter text :\n"); func(); printf("\n"); }/*End of main()*/ + void func(void) { - char c; - if((c=getchar())!='\n') + int c; + + c = getchar(); + switch (c) + { + case EOF: + case '\n': + break; + default: func(); - putchar(c); + break; + } + + if (c != EOF) + putchar(c); } From f3cf249882df36113a4faa8951fea702354a5aa8 Mon Sep 17 00:00:00 2001 From: Greg Skinner Date: Thu, 29 Apr 2021 10:57:57 -0700 Subject: [PATCH 12/15] Update Divisibility by 11 and 9.c --- Recursion programs/Divisibility by 11 and 9.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Recursion programs/Divisibility by 11 and 9.c b/Recursion programs/Divisibility by 11 and 9.c index 3ac77f1..db985ef 100644 --- a/Recursion programs/Divisibility by 11 and 9.c +++ b/Recursion programs/Divisibility by 11 and 9.c @@ -1,21 +1,22 @@ /*Program that tests whether a number is divisible by 11 and 9 or not */ #include +#include int divisibleBy9(long int x); int divisibleBy11(long int x); -main( ) +int main( ) { long int num; printf("Enter the number to be tested : "); scanf("%ld", &num); - if(divisibleBy9(num)) + if((num == 0) || divisibleBy9(labs(num))) printf("The number is divisible by 9\n"); else printf("The number is not divisible by 9\n"); - if(divisibleBy11(num)) + if(divisibleBy11(labs(num))) printf("The number is divisible by 11\n"); else printf("The number is not divisible by 11\n"); @@ -34,12 +35,12 @@ int divisibleBy9( long int n ) sumofDigits += n%10; n/=10; } - divisibleBy9(sumofDigits); + return divisibleBy9(sumofDigits); }/*End of divisibleBy9()*/ int divisibleBy11( long int n ) { - int s1=0, s2=0,diff; + int s1=0, s2=0; if(n == 0) return 1; @@ -53,7 +54,7 @@ int divisibleBy11( long int n ) s2 += n%10; n /= 10; } - diff = s1>s2 ? (s1-s2) : (s2-s1); - divisibleBy11(diff); + return divisibleBy11(labs(s1-s2)); }/*End of divisibleBy11()*/ + From 15d22d57c0b0f065a019a2ee8d3b35d27a4012e2 Mon Sep 17 00:00:00 2001 From: Greg Skinner Date: Wed, 5 May 2021 13:13:18 -0700 Subject: [PATCH 13/15] Create Stack using Array.c --- Stacks and Queues/Stack using Array.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Stacks and Queues/Stack using Array.c b/Stacks and Queues/Stack using Array.c index 9a84e2c..f38e9f1 100644 --- a/Stacks and Queues/Stack using Array.c +++ b/Stacks and Queues/Stack using Array.c @@ -44,7 +44,7 @@ main() display(); break; case 5: - exit(1); + exit(0); default: printf("Wrong choice\n"); }/*End of switch*/ From 9a68c88c41c84084d06ce1c4d2c071fa20a2ea5b Mon Sep 17 00:00:00 2001 From: Greg Skinner Date: Wed, 5 May 2021 20:33:49 -0700 Subject: [PATCH 14/15] Update Stack using Linked List.c --- Stacks and Queues/Stack using Linked List.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Stacks and Queues/Stack using Linked List.c b/Stacks and Queues/Stack using Linked List.c index fc79800..6986c89 100644 --- a/Stacks and Queues/Stack using Linked List.c +++ b/Stacks and Queues/Stack using Linked List.c @@ -46,7 +46,7 @@ main() display(); break; case 5: - exit(1); + exit(0); default : printf("Wrong choice\n"); }/*End of switch */ From af40228caf2e0c0078c76d18554510672396fbdf Mon Sep 17 00:00:00 2001 From: Greg Skinner Date: Thu, 13 Jan 2022 22:37:18 -0800 Subject: [PATCH 15/15] Update IsBST.c add test data, refactor some printf() statements, and check for malloc() errors --- Trees/IsBST.c | 50 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/Trees/IsBST.c b/Trees/IsBST.c index d60aba8..b0ddc7e 100644 --- a/Trees/IsBST.c +++ b/Trees/IsBST.c @@ -1,7 +1,10 @@ -/* Find whether a binary tree is binary search tree or not*/ +/* Find out whether a binary tree is a binary search tree or not*/ #include #include +#include #include +#include +#define SLEN 5 struct node { @@ -15,31 +18,44 @@ void inorder(struct node *ptr); struct node *Node(int item); int IsBST(struct node *ptr, int MIN, int MAX); -main( ) +int main( ) { - struct node *root1, *root2; - + struct node *root1, *root2, *root3; + char str[SLEN]; + + srandom(getpid()); + + /* BST */ root1 = Node(32); root1->lchild = Node(23); root1->rchild = Node(36); root1->lchild->rchild = Node(25); root1->rchild->lchild = Node(33); + /* not a BST */ root2 = Node(42); root2->lchild = Node(60); root2->rchild = Node(19); root2->lchild->rchild = Node(36); root2->rchild->lchild = Node(41); + /* random test data */ + root3 = Node(random() % INT_MAX); + root3->lchild = Node(random() % INT_MAX); + root3->rchild = Node(random() % INT_MAX); + root3->lchild->rchild = Node(random() % INT_MAX); + root3->rchild->lchild = Node(random() % INT_MAX); + display(root1,1); printf("\n\n"); inorder(root1); printf("\n\n"); - if( IsBST(root1,INT_MIN,INT_MAX) ) - printf("Tree 1 is a BST\n"); + if( IsBST(root1,INT_MIN,INT_MAX) ) + str[0] = '\0'; else - printf("Tree 1 is not a BST\n"); + strncpy(str, "not ", SLEN); + printf("Tree 1 is %sa BST\n", str); display(root2,1); printf("\n\n"); @@ -47,9 +63,21 @@ main( ) printf("\n\n"); if( IsBST(root2,INT_MIN,INT_MAX) ) - printf("Tree 2 is a BST\n"); + str[0] = '\0'; else - printf("Tree 2 is not a BST\n"); + strncpy(str, "not ", SLEN); + printf("Tree 2 is %sa BST\n", str); + + display(root3,1); + printf("\n\n"); + inorder(root3); + printf("\n\n"); + + if( IsBST(root3,INT_MIN,INT_MAX) ) + str[0] = '\0'; + else + strncpy(str, "not ", SLEN); + printf("Tree 3 is %sa BST\n", str); }/*End of main( )*/ int IsBST(struct node *ptr, int MIN, int MAX) @@ -64,6 +92,10 @@ int IsBST(struct node *ptr, int MIN, int MAX) struct node *Node(int item) { struct node* tmp = (struct node *)malloc(sizeof(struct node)); + if (tmp == NULL) { + perror("malloc"); + exit(1); + } tmp->info = item; tmp->lchild = tmp->rchild = NULL; return tmp;