summaryrefslogtreecommitdiff
path: root/3808/CH5
diff options
context:
space:
mode:
authorprashantsinalkar2017-10-10 12:27:19 +0530
committerprashantsinalkar2017-10-10 12:27:19 +0530
commit7f60ea012dd2524dae921a2a35adbf7ef21f2bb6 (patch)
treedbb9e3ddb5fc829e7c5c7e6be99b2c4ba356132c /3808/CH5
parentb1f5c3f8d6671b4331cef1dcebdf63b7a43a3a2b (diff)
downloadScilab-TBC-Uploads-7f60ea012dd2524dae921a2a35adbf7ef21f2bb6.tar.gz
Scilab-TBC-Uploads-7f60ea012dd2524dae921a2a35adbf7ef21f2bb6.tar.bz2
Scilab-TBC-Uploads-7f60ea012dd2524dae921a2a35adbf7ef21f2bb6.zip
initial commit / add all books
Diffstat (limited to '3808/CH5')
-rw-r--r--3808/CH5/EX5.1/Ex5_1.sce18
-rw-r--r--3808/CH5/EX5.2/Ex5_2.sce17
-rw-r--r--3808/CH5/EX5.3/Ex5_3.sce18
-rw-r--r--3808/CH5/EX5.4/Ex5_4.sce23
-rw-r--r--3808/CH5/EX5.5/Ex5_5.sce52
5 files changed, 128 insertions, 0 deletions
diff --git a/3808/CH5/EX5.1/Ex5_1.sce b/3808/CH5/EX5.1/Ex5_1.sce
new file mode 100644
index 000000000..0e73efa02
--- /dev/null
+++ b/3808/CH5/EX5.1/Ex5_1.sce
@@ -0,0 +1,18 @@
+//Chapter 05: Induction and Recursion
+
+clc;
+clear;
+
+function f = my_f(n)
+if n == 0
+ f = 3
+else
+ f = 2* my_f(n-1) +3 //making a recursive call
+end
+return f
+endfunction
+
+for n=0:4
+re=my_f(n)
+mprintf("The value of f(%d) is %d\n",n,re)
+end
diff --git a/3808/CH5/EX5.2/Ex5_2.sce b/3808/CH5/EX5.2/Ex5_2.sce
new file mode 100644
index 000000000..e7b9b38f6
--- /dev/null
+++ b/3808/CH5/EX5.2/Ex5_2.sce
@@ -0,0 +1,17 @@
+//Chapter 05: Induction and Recursion
+
+clc;
+clear;
+
+function fact = my_factorial(n)
+if n == 0
+ fact = 1
+else
+ fact = n * my_factorial(n-1)//recursive function call
+end
+return fact
+endfunction
+
+num=input("Enter the number whose factorial is to be found:")
+f=my_factorial(num)
+mprintf("The factorial of %d is %d",num,f)
diff --git a/3808/CH5/EX5.3/Ex5_3.sce b/3808/CH5/EX5.3/Ex5_3.sce
new file mode 100644
index 000000000..cff1ffb1d
--- /dev/null
+++ b/3808/CH5/EX5.3/Ex5_3.sce
@@ -0,0 +1,18 @@
+//Chapter 05: Induction and Recursion
+
+clc;
+clear;
+
+function pow = power(i,n)
+if n == 0
+ pow = 1
+else
+ pow = i * power(i,n-1)//recursive function call
+end
+return pow
+endfunction
+
+n=input("Enter the number whose power is to be found:")
+po=input("Enter the power:")
+p=power(n,po)
+mprintf("%d to the power %d is %d",n,po,p)
diff --git a/3808/CH5/EX5.4/Ex5_4.sce b/3808/CH5/EX5.4/Ex5_4.sce
new file mode 100644
index 000000000..14e171890
--- /dev/null
+++ b/3808/CH5/EX5.4/Ex5_4.sce
@@ -0,0 +1,23 @@
+//Chapter 05: Induction and Recursion
+
+clc;
+clear;
+
+function res=greatestcommondivisior(a,b)
+ if a==0 then
+res=b
+ else
+res=greatestcommondivisior(modulo(b,a),a)
+ end
+return res
+endfunction
+
+num1=input("Enter the first number:")
+num2=input("Enter the second number:")
+res_gcd=greatestcommondivisior(num1,num2)
+mprintf("The gcd of %d , %d is %d",num1,num2,res_gcd)
+
+//By Using the inbuilt function,that is provided by Scilab
+p=[num1,num2]
+res=gcd(p)
+mprintf("\nThe gcd of %d , %d is %d",num1,num2,res)
diff --git a/3808/CH5/EX5.5/Ex5_5.sce b/3808/CH5/EX5.5/Ex5_5.sce
new file mode 100644
index 000000000..855d902a7
--- /dev/null
+++ b/3808/CH5/EX5.5/Ex5_5.sce
@@ -0,0 +1,52 @@
+//Chapter 05: Induction and Recursion
+
+clc;
+clear;
+
+//Function to merge & sort
+function [ a1 ]= mergesort (a ,p , r )
+if (p < r )
+q = int (( p + r ) /2) ;
+a = mergesort (a ,p , q ) ;
+a = mergesort (a , q +1 , r ) ;
+a = merge (a ,p ,q , r ) ;
+else
+a1 = a ;
+return ;
+end
+a1 = a ;
+endfunction
+
+//Function to merge
+function [ a1 ]= merge (a ,p ,q , r )
+n1 =q - p +1;
+n2 =r - q ;
+left = zeros ( n1 +1) ;
+right = zeros ( n2 +1) ;
+for i =1: n1
+left ( i ) = a ( p +i -1) ;
+end
+for i1 =1: n2
+right ( i1 ) = a ( q + i1 ) ;
+end
+left ( n1 +1) =111111111;
+right ( n2 +1) =111111111;
+i =1;
+j =1;
+k=p;
+for k = p : r
+if ( left ( i ) <= right ( j ) )
+a ( k ) = left ( i ) ;
+i = i +1;
+else
+a ( k ) = right ( j ) ;
+j = j +1;
+end
+end
+a1 = a ;
+endfunction
+
+arr =[8 2 4 6 9 7 10 1 5 3]
+disp(arr," Given Array:" ) ;
+arr_s =mergesort (arr ,1 ,10)
+disp(arr_s , " Sorted Array:" );